从ResultSet填充JTable的最简单代码
getnPrintAllData()
public class DatabaseSQLiteConnection { Connection conn = null; PreparedStatement statement = null; ResultSet res = null; public DatabaseSQLiteConnection(){ try{ Class.forName("org.sqlite.JDBC"); conn = DriverManager.getConnection("jdbc:sqlite:test.sqlite"); statement = conn.prepareStatement("SELECT * from product_info;"); } catch(Exception e){ e.printStackTrace(); } } public void getnPrintAllData(){ String name, supplier, id; DefaultTableModel dtm = new DefaultTableModel(); Window gui = new Window(); //My JPanel class try{ res = statement.executeQuery(); testResultSet(res); ResultSetMetaData meta = res.getMetaData(); int numberOfColumns = meta.getColumnCount(); while (res.next()) { Object [] rowData = new Object[numberOfColumns]; for (int i = 0; i < rowData.length; ++i) { rowData[i] = res.getObject(i+1); } dtm.addRow(rowData); } gui.jTable1.setModel(dtm); dtm.fireTableDataChanged(); ////////////////////////// } catch(Exception e){ System.err.println(e); e.printStackTrace(); } finally{ try{ res.close(); statement.close(); conn.close(); } catch(Exception e){ e.printStackTrace(); } } } } } }}
testResultSet()
DefaultTableModel
ResultSet
java.lang.IllegalStateException: SQLite JDBC: inconsistent internal state
HUWWW
料青山看我应如是
相关分类