Making a JTable editable
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Making a JTable editable

  1. #1
    Join Date
    Feb 2010
    Posts
    121

    Making a JTable editable

    Hi,

    I am displaying the results of sql query (using com.mysql.jdbc.Driver) in a jtable. Here is the main part of the code

    Code:
    package guidatabase1;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Vector;
    
    import javax.swing.table.AbstractTableModel;
    
    //QueryTableModel.java
    //A basic implementation of the TableModel interface that fills out a Vector of
    //String[] structures from a query's result set.
    //
    
    class QueryTableModel extends AbstractTableModel {
    Vector cache; // will hold String[] objects . . .
    
    int colCount;
    
    String[] headers;
    
    Connection db;
    
    Statement statement;
    
    String currentURL;
    
    Connection conn;
    connection c = new connection();
    ResultSet rs;
    
    public QueryTableModel() {
    	    cache = new Vector();
    	    try {
    			new com.mysql.jdbc.Driver();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	  }
    
    public String getColumnName(int i) {
      return headers[i];
    }
    
    public int getColumnCount() {
      return colCount;
    }
    
    public int getRowCount() {
      return cache.size();
    }
    
    public Object getValueAt(int row, int col) {
      return ((String[]) cache.elementAt(row))[col];
    }
    
    
    
    
    // All the real work happens here; in a real application,
    // we'd probably perform the query in a separate thread.
    public void setQuery(String q) {
      cache = new Vector();
      try {
    	  //System.out.println(q);
        // Execute the query and store the result set and its metadata
    		conn = c.createConnection();
    		rs = conn.createStatement().executeQuery(q);
       // ResultSet rs = statement.executeQuery(q);
        ResultSetMetaData meta = rs.getMetaData();
        colCount = meta.getColumnCount();
    
        // Now we must rebuild the headers array with the new column names
        headers = new String[colCount];
        for (int h = 1; h <= colCount; h++) {
          headers[h - 1] = meta.getColumnName(h);
        }
    
        // and file the cache with the records from our query. This would
        // not be
        // practical if we were expecting a few million records in response
        // to our
        // query, but we aren't, so we can do this.
        while (rs.next()) {
          String[] record = new String[colCount];
          for (int i = 0; i < colCount; i++) {
            record[i] = rs.getString(i + 1);
          }
          cache.addElement(record);
        }
        fireTableChanged(null); // notify everyone that we have a new table.
      } catch (Exception e) {
        cache = new Vector(); // blank it out and keep going.
        e.printStackTrace();
      }
    }
    
    }
    I am trying to make the jtable editable by implementing this method

    Code:
    public boolean isCellEditable(int row, int col)
    {
        return true;	
    }
    However, I can't work out how to save the changes. Here is the method I am using.


    Code:
    public void setValueAt(Object aValue, int row, int col){
        try
           {  
              rs.absolute(row + 1);
              System.out.println("I got here... row: "+ row + ", " + col);
              rs.updateObject(col+1, (String)aValue);
              rs.updateRow();
           }
           catch(SQLException e)
           {  
              e.printStackTrace();
           }
    }
    and the error returned is:

    "com.mysql.jdbc.NotUpdatable: Result Set not updatable.This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, the query must select only one table, can not use functions and must select all primary keys from that table. See the JDBC 2.1 API Specification, section 5.6 for more details."

    I can't figure out how to return the 'aValue' object

    Can someone help? Thanks
    Last edited by peahead; April 1st, 2012 at 01:23 PM. Reason: typo in code

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Making a JTable editable

    Not all result sets support the update method, it depends on how they were created. Try reading the java tutorial on using result sets: http://docs.oracle.com/javase/tutori...etrieving.html
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Feb 2010
    Posts
    121

    Re: Making a JTable editable

    Quote Originally Posted by keang View Post
    Not all result sets support the update method, it depends on how they were created. Try reading the java tutorial on using result sets: http://docs.oracle.com/javase/tutori...etrieving.html

    Thanks, figured it out. I had to add ResultSet.CONCUR_UPDATABLE to the createStatement method.

    Code:
    rs = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE ).executeQuery(q);

    and called method 'fireTableCellUpdated(row, col);' to save changes to the database
    Last edited by peahead; April 4th, 2012 at 07:26 AM. Reason: update

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center