Click here to Skip to main content
15,887,302 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Finally I success the populate a data in jtable. But no refresh when I was add new data. Could you help me ? Thanks..

Java
final void FillList() throws ClassNotFoundException {
       try {
           Class.forName("org.sqlite.JDBC");
           Connection con = DriverManager.getConnection("jdbc:sqlite:lib/mydb.db");

           String[] tableColumnsName = {"name", "city","date"};
           DefaultTableModel aModel = (DefaultTableModel) tablo.getModel();
           aModel.setColumnIdentifiers(tableColumnsName);

           Statement stmt = con.createStatement();
           ResultSet rs = stmt.executeQuery("select name,city,date from mytable");

           java.sql.ResultSetMetaData rsmd = rs.getMetaData();
           int colNo = rsmd.getColumnCount();
           while (rs.next()) {
               Object[] objects = new Object[colNo];
               for (int i = 0; i < colNo; i++) {
                   objects[i] = rs.getObject(i + 1);
               }
               aModel.addRow(objects);
           }
           tablo.setModel(aModel);
           con.close();
           stmt.close();
           rs.close();

       } catch (ClassNotFoundException ex) {
           Logger.getLogger(buny.class.getName()).log(Level.SEVERE, null, ex);
       } catch (SQLException ex) {
           Logger.getLogger(buny.class.getName()).log(Level.SEVERE, null, ex);
       }
   }


And another problem, this code is work but I don't see new data in table same time.(I see new data when I was restart my app) And ide show this error msg :
SEVERE: null
java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (Connection is closed)
at org.sqlite.core.DB.newSQLException(DB.java:890)
at org.sqlite.core.CoreStatement.internalClose(CoreStatement.java:109)
at org.sqlite.jdbc3.JDBC3Statement.close(JDBC3Statement.java:35)


Java
private void addActionPerformed(java.awt.event.ActionEvent evt) {

       try {
           Class.forName("org.sqlite.JDBC");
           Connection con = DriverManager.getConnection("jdbc:sqlite:lib/mydb.db");
           Statement stmt = con.createStatement();
           String Query = "insert into mytable (name,city,date) values ('" +name.getText() + "','" + city.getText() + "','" + date.getText() + "')";

           stmt.executeQuery(Query);
           stmt.close();
           con.close();
           FillList();

           JOptionPane.showMessageDialog(null, "OK..");
       } catch (ClassNotFoundException ex) {
           Logger.getLogger(buny.class.getName()).log(Level.SEVERE, null, ex);
       } catch (SQLException ex) {
           Logger.getLogger(buny.class.getName()).log(Level.SEVERE, null, ex);
       }

   }
Posted
Updated 6-Jan-16 14:36pm
v3

 
Share this answer
 
Comments
bugsoul 6-Jan-16 14:58pm    
Thanks but I did't success again. I can add data and I can show with db browser. What is the problem in my code?
You didn't specify how the code isn't working but one thing that catches eye is that you have two while loops where you advance in the result set
Java
...
            while (rs.next()) {
                Object[] row = new Object[colcount];
                for (int i = 1; i <= colcount; i++) {
                    row[i - 1] = rs.getObject(i);
                }
                tm.addRow(row);
            }
            while (rs.next()) { /* is this needed or just causing problems */
            }
            jtable.setModel(tm);
...

Another thing is that jtable is not defined nor initialized in this code. Could it be uninitialized?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900