Results 1 to 2 of 2
  1. #1
    steme is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default JTable Not Updating in Real Time

    I am working on a JTable where you enter the information into the table and it is saved in a SQL database. I have it setup so when you double click on a cell a joptionpane appears allowing you to enter the information. I got everything working fine, it is just the information you entered does not appear into the JTable until you restart the program... I thought all I needed was fireTableDataChanged(); to dynamically update my Jtable.. But it seems that is not the case... I have been reading and I think I need something called a TableModelEvent class but I am not sure where to put it or how to implement it.

    .
    (THIS IS NOT THE FULL CODE I DELETED A BUNCH OF METHODS IRRELEVANT TO MY PROBLEM TO MAKE IT EASIER TO READ)

    Here you can see a simple Main method with a Jtable and a MouseListener that calles my AbstractTableMethod.

    Java Code:
    public class Query extends JFrame {
    	
    	private static final long serialVersionUID = 1L;
    	public static final String JDBC_URL = "jdbc:derby:zadb;create=true";
    	public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
    	private JPanel contentPane;
    	private JTable table;
    	private JMenuBar menuBar;
    	private JMenu mnProgram;
    	private JMenuItem mntmGenerateInvoice;
    	private JMenuItem mntmCloseProgram;
    	private JSeparator separator;
    	private int mrow,mcol;
    	private ResultSetTableModel tableModel;
    
    
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Query frame = new Query();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	public Query() throws SQLException, ClassNotFoundException {
    		
    		tableModel = new ResultSetTableModel(DRIVER,JDBC_URL,"select * from table2");
    
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 815, 505);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(contentPane);
    		
    		JScrollPane scrollPane = new JScrollPane();
    		contentPane.add(scrollPane, BorderLayout.CENTER);
    		
    		table = new JTable (tableModel);
    		scrollPane.setViewportView(table);
    		
    		table.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent e) {
    				
    			mrow = table.getSelectedRow();
    			mcol = table.getSelectedColumn();
    			
    			if(e.getClickCount() == 2){
    				
    				String h = JOptionPane.showInputDialog("Data");
    				
    				try {
    					tableModel.update(h, mcol, mrow);
    
    					
    				} catch (SQLException e1) {
    
    					e1.printStackTrace();
    				}	
    			}
    			}
    			});
    	}
    }


    My AbstractTableModelClass
    Java Code:
    public class ResultSetTableModel extends AbstractTableModel 
    {
    
    	private static final long serialVersionUID = 1L;
    	private Connection connection;
        private Statement statement;
        private ResultSet resultSet;
        private ResultSetMetaData metaData;
        private int numberOfRows;
       private boolean connectedToDatabase = false;
       
       public ResultSetTableModel( String driver, String url,  String query ) 
          throws SQLException, ClassNotFoundException
       {         
          Class.forName( driver );
          connection = DriverManager.getConnection( url );
          statement = connection.createStatement( 
             ResultSet.TYPE_SCROLL_INSENSITIVE,
             ResultSet.CONCUR_UPDATABLE );
          connectedToDatabase = true;
          setQuery( query );
       }
    
       public void setQuery( String query ) 
          throws SQLException, IllegalStateException 
       {
          if ( !connectedToDatabase ) 
             throw new IllegalStateException( "Not Connected to Database" );
    
          resultSet = statement.executeQuery( query );
          metaData = resultSet.getMetaData();
          resultSet.last();
          numberOfRows = resultSet.getRow();   
    
          fireTableStructureChanged();
       }  
     
       public void update(String a ,int b, int c) throws SQLException{
    
    	   switch(b){
    		case 1: connection.createStatement().execute("UPDATE table2 SET MAKE = '"+ a +"' WHERE ID=" + (c + 1));
    		break;
    		case 2: connection.createStatement().execute("UPDATE table2 SET MODEL = '"+ a +"' WHERE ID=" + (c + 1));
    		break; 
    		case 3: connection.createStatement().execute("UPDATE table2 SET ESN = '"+ a +"' WHERE ID=" + (c + 1));
    		break;
    		case 4: connection.createStatement().execute("UPDATE table2 SET DATE = '"+ a +"' WHERE ID=" + (c + 1));
    		break;
    	   }
    	   fireTableDataChanged();
       } 
       
      }

  2. #2
    steme is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Re: JTable Not Updating in Real Time

    You may be wondering why I don't run my sql statements through the setQuery() method.... It is because whenever I try to access it I get this error.
    "java.sql.SQLException: Statement.executeQuery() cannot be called with a statement that returns a row count."

Similar Threads

  1. How real-time is java?
    By StateMachine in forum New To Java
    Replies: 12
    Last Post: 12-27-2011, 05:41 PM
  2. Real Time?
    By RaustBD in forum New To Java
    Replies: 1
    Last Post: 01-01-2011, 03:14 AM
  3. How to update in real time
    By darius_dada in forum AWT / Swing
    Replies: 1
    Last Post: 12-14-2010, 11:05 PM
  4. real-time validation
    By atomz4peace in forum Eclipse
    Replies: 0
    Last Post: 01-06-2009, 08:31 PM
  5. how get real-time output
    By tOpach in forum New To Java
    Replies: 5
    Last Post: 12-17-2008, 09:41 PM

Posting Permissions

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