[HELP] JTable AbstractTableModel connected to database not refreshing

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3557 Views - Last Post: 24 May 2013 - 12:15 PM Rate Topic: -----

#1 mr.nacho   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 208
  • Joined: 28-December 12

[HELP] JTable AbstractTableModel connected to database not refreshing

Posted 21 May 2013 - 08:14 PM

Please help me. I'm stuck again in refreshing the JTable after I add, edit, delete or search.
I separate the database connection in other class and I am using JDialog to add or edit a record.

Here's my code for database:

 import java.sql.*;

public class DatabaseConnection
{
	public Connection con;
	public Statement st;
	public ResultSet rs;
	public ResultSetMetaData rsmd;

	public int count = 0;
	public int recordCount = 0;
	private int x = 0;
	public String columnNames[];
	public Object sendData[][];


	public DatabaseConnection()
	{
		connect();
	}

	public void connect()
	{
		try
		{
			String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
			Class.forName(driver);

			String db = "jdbc:odbc:DatabaseWorkers";
			con = DriverManager.getConnection(db);
			st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

			String sql = "Select * from EmpRecords";
			rs = st.executeQuery(sql);
			rsmd = rs.getMetaData();

			count = rsmd.getColumnCount();
			columnNames = new String[count];

			while(x != count)
			{
				x++;
				columnNames[x-1] = rsmd.getColumnName(x);

			}

			rs.last();
			recordCount = rs.getRow();

			sendData = new Object[recordCount][count];

			rs.beforeFirst();

			int a = 0;

			while(rs.next())
			{
				for(int b = 1;b<=count;b++)
				{
					sendData[a][b-1] = rs.getString(B)/>;
				}
				a++;

			}



		}

		catch(Exception e)
		{
			e.printStackTrace();
		}

	} 


for adding(in JDialog):

private class ButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == btnSave)
			{
				DatabaseConnection dc = new DatabaseConnection();
				DatabaseTable dt = new DatabaseTable();


				if(getTitle() == "Add")
				{
					EmpNo = txtEmpNo.getText().toString();
					LastName = txtLastName.getText().toString();
					FirstName = txtFirstName.getText().toString();
					Position = txtPosition.getText().toString();
					Salary = txtSalary.getText().toString();

					try
					{

						sql = "INSERT INTO EmpRecords VALUES ('" + EmpNo + "','" + LastName + "','" + FirstName + "','" + Position + "','"+ Salary + "')";
						dc.st.executeUpdate(sql);

						// I've used here tableChanged or model.firetabledatachanged but still nothing happens
						
						JOptionPane.showMessageDialog(null, "New Record Saved", "Added Successfully", 1);
						dispose();


					}
					catch (Exception ex)
					{

					}


				}

				else if(getTitle() == "Edit")
				{

				}
			}



Thanks in advance for the help.

Is This A Good Question/Topic? 0
  • +

Replies To: [HELP] JTable AbstractTableModel connected to database not refreshing

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3561
  • View blog
  • Posts: 16,230
  • Joined: 20-September 08

Re: [HELP] JTable AbstractTableModel connected to database not refreshing

Posted 22 May 2013 - 08:22 AM

Before you go any further, you need to tidy up your code: it does not do what is says it does - it does more (which is the same thing)

A method called connect() should do that and nothing more. Yours is trying to get a ResultSet, which is wrong.
Your instance variable 'x' will just get higher and higher as you run your app and therefore quickly be very wrong. Why is it not a local variable?

I suggest you look at the following too

http://technojeeves....t-to-tablemodel
and possibly
http://technojeeves....-to-nested-list
Was This Post Helpful? 0
  • +
  • -

#3 mr.nacho   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 208
  • Joined: 28-December 12

Re: [HELP] JTable AbstractTableModel connected to database not refreshing

Posted 22 May 2013 - 08:55 AM

I fixed the variable x. Now, what should I need to do to fix the wrong with getting the ResultSet?

Do I really need to use List or ArrayLists or Vectors???
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3561
  • View blog
  • Posts: 16,230
  • Joined: 20-September 08

Re: [HELP] JTable AbstractTableModel connected to database not refreshing

Posted 22 May 2013 - 09:26 AM

Yes, you need to use Collection classes. Doing first and last on a ResultSet is expensive and unnecessary
Was This Post Helpful? 0
  • +
  • -

#5 mr.nacho   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 208
  • Joined: 28-December 12

Re: [HELP] JTable AbstractTableModel connected to database not refreshing

Posted 22 May 2013 - 09:32 AM

Can you tell me what steps should I really need to follow to successfully update JTable with AbstractTableModel after adding, editing, deleting and searching database records? So that I can start all over again.

I'm really confused right now. Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3561
  • View blog
  • Posts: 16,230
  • Joined: 20-September 08

Re: [HELP] JTable AbstractTableModel connected to database not refreshing

Posted 22 May 2013 - 09:50 AM

I'm not sure you need an AbstractTableModel actually. Things could be simpler if you use a DefaultTableModel. Begin with a fill() method and use the code i linked to. You can have an instance variable of type TableModel then the implementation can be changed later
Was This Post Helpful? 0
  • +
  • -

#7 mr.nacho   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 208
  • Joined: 28-December 12

Re: [HELP] JTable AbstractTableModel connected to database not refreshing

Posted 22 May 2013 - 09:57 AM

Yes, I know that. But actually, I want to practice them both.

I'm already at AbstractTableModel and I want to pursue it first. :)
Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: [HELP] JTable AbstractTableModel connected to database not refreshing

Posted 22 May 2013 - 06:03 PM

BTW that won't work

else if(getTitle() == "Edit")

you can't compare String using the == operator
You need tp use the String class equals() or equalsIgnoreCase() methods

Wher is the class that implements the TabelModel ?
Was This Post Helpful? 1
  • +
  • -

#9 mr.nacho   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 208
  • Joined: 28-December 12

Re: [HELP] JTable AbstractTableModel connected to database not refreshing

Posted 22 May 2013 - 06:14 PM

Here:

class DatabaseTable extends AbstractTableModel // ----------------------- ABSTRACT TABLE MODEL --------------------------------\\
	{
		DatabaseConnection dc = new DatabaseConnection();


		String fieldNames[] = dc.columnNames;
		Object data[][] = dc.sendData;


		public int getColumnCount()
		{
			return fieldNames.length;
		}

		public String getColumnName(int column)
		{
			return fieldNames[column];
		}

		public int getRowCount()
		{
			return data.length;
		}


	    public Object getValueAt(int row, int column)
	    {
	       return data[row][column];
	    }

	    public void setValueAt(Object value, int row, int column)
	    {

	        data[row][column] = value;
	        fireTableCellUpdated(row, column);


		}

		public boolean isCellEditable(int row, int column)
	    {
	       	return false;
	    }


	}




I get the resultset from the class databaseconnection which is dc.sendData.
Is it not advisable? Thanks.
Was This Post Helpful? 0
  • +
  • -

#10 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: [HELP] JTable AbstractTableModel connected to database not refreshing

Posted 22 May 2013 - 07:09 PM

So where do you update data[][]in your model ?
Was This Post Helpful? 0
  • +
  • -

#11 mr.nacho   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 208
  • Joined: 28-December 12

Re: [HELP] JTable AbstractTableModel connected to database not refreshing

Posted 23 May 2013 - 07:05 AM

I tried it in the JDialog where I add new records.

						sql = "INSERT INTO EmpRecords VALUES ('" + EmpNo + "','" + LastName + "','" + FirstName + "','" + Position + "','"+ Salary + "')";
						dc.st.executeUpdate(sql);

						dt.data = dc.sendData;

                                                new DBWorkers().model.fireTableDataChanged();
                                                new DBWorkers().tableWorkers.tableChanged(new TableModelEvent(new DBWorkers().model)); // or even this. Not working.


Was This Post Helpful? 0
  • +
  • -

#12 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: [HELP] JTable AbstractTableModel connected to database not refreshing

Posted 23 May 2013 - 05:54 PM

don't se where you modified data[][]

actually not a good idea to use an Array.. they arr a bit complicated to extend if you add a row
Was This Post Helpful? 0
  • +
  • -

#13 mr.nacho   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 208
  • Joined: 28-December 12

Re: [HELP] JTable AbstractTableModel connected to database not refreshing

Posted 23 May 2013 - 07:01 PM

List or ArrayLists or Vectors would be the replacement?

is this line the modification? dt.data = dc.sendData;
Was This Post Helpful? 0
  • +
  • -

#14 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: [HELP] JTable AbstractTableModel connected to database not refreshing

Posted 23 May 2013 - 07:05 PM

because dt is a variable pointing to your TableModel ? And we are suppose to know that ?
Ok so shoud be

dt.data = ...
dt.fireTableDataChanged();

What are new DBWorkers() supposed to do ?
Was This Post Helpful? 0
  • +
  • -

#15 mr.nacho   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 208
  • Joined: 28-December 12

Re: [HELP] JTable AbstractTableModel connected to database not refreshing

Posted 24 May 2013 - 05:38 AM

Yes, dt = databasetable. With new DBWorkers(), I called there the already used table model from the constructor. Well, I actually did all the possible firing events or tableChanged but still it does nothing. I don't really know what to do.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2