Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Update a database table from JTable

843806Mar 6 2008 — edited Mar 6 2008
Hello everyone,

I am new to the world of Java. I am using NetBeans and MySQL. I am trying to update a table in a MySQL database through a jTable. I have gone through the questions posted by other people about similar problems. But this one, I felt could be different.

Given Situation :

There is an employee table in a database which is viewed by the user through the jTable. The code I used for displaying the data from the table into jTable is given below. The code is written in the constructor of the class which holds the jPanel (And this in turn holds the jTable).
jTable1.setAutoCreateRowSorter(true);
        
        String q1 = "SELECT * from employees;";   
        int rowCount = jTable1.getRowCount();
                    int t;
                    for(t=rowCount-1; t>=0 ; t--) {
                    ((DefaultTableModel)jTable1.getModel()).removeRow(t);
                    }
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/hr", "root", "siddhartha1986");
            Statement stmnt = con.createStatement();
            String query = q1;
            ResultSet rs = stmnt.executeQuery(query);
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();
            while(rs.next()) {  
              
                Vector row = new Vector(columns); 
                for (int i = 1; i <= columns; i++)
                {
                  row.addElement(rs.getObject(i));
                } 
               ((DefaultTableModel)jTable1.getModel()).addRow(row);  
            }
                rs.close();
                stmnt.close();
         }
        catch(java.lang.ClassNotFoundException e)
		{
        System.out.println("1   " +e.getMessage());
 		}
		catch(java.lang.InstantiationException e)
		{
	System.out.println("2" +e.getMessage());
                }
		catch(java.sql.SQLException e)
		{
	System.out.println("3" +e.getMessage());
                }
        catch(java.lang.IllegalAccessException e)
		{
	System.out.println("4" +e.getMessage());
                }  
To do

User double clicks on any field in the jTable, edits the contents and repeats the steps for multiple cells in various rows and columns. Now, there is a button called, "Update Database". When the user clicks that button in the jPanel, all the entries in the jTable which are edited must be updated in the employee table in the database.

Question

HOW TO DO IT???

My work so far

I am currently with the logic that since it is multiple cell editing, go with a for loop through each and every cell and update the result set and then update the table.
My code so far is given below
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
int p, j=0;
        String q1 = "SELECT * from employees;";
        
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/hr", "root", "siddhartha1986");
            Statement stmnt = con.createStatement();
            String query = q1;
            ResultSet rs = stmnt.executeQuery(query);
            int max = jTable1.getRowCount();        
            Object value = new Object();
            for(p=0;p<=max;p++){                
                rs.absolute(p+1);
                rs.updateObject(j+1,value);
                rs.updateRow();
            }
            rs.close();
            stmnt.close();
        }
        catch(java.lang.ClassNotFoundException e)
		{
        System.out.println("1   " +e.getMessage());
 		}
		catch(java.lang.InstantiationException e)
		{
	System.out.println("2" +e.getMessage());
                }
		catch(java.sql.SQLException e)
		{
	System.out.println("3" +e.getMessage());
                }
        catch(java.lang.IllegalAccessException e)
		{
	System.out.println("4" +e.getMessage());
                }
}
Note

I could have included firetablerowsupdated* event, but it shows an error "Variable not found" in NetBeans. And I do not know how to trigger a TableModelEvent. I hope somebody in this forum would be kind enough to consider this ignorant question and answer me as soon as possible.

Many thanks in advance,

Bipin
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 3 2008
Added on Mar 6 2008
6 comments
3,019 views