I'm trying to display an sqlite data table in a jtable but i have an error " sqlite is type forward only"
how could I display it in a jtable
try {
long start = System.currentTimeMillis();
Statement state = ConnectionBd.getInstance().createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
开发者_开发技巧 ResultSet.CONCUR_READ_ONLY
);
ResultSet res = state.executeQuery("SELECT * FROM data");
ResultSetMetaData meta = res.getMetaData();
Object[] column = new Object[meta.getColumnCount()];
for(int i = 1 ; i <= meta.getColumnCount(); i++){
column[i-1] = meta.getColumnName(i);
}
res.last();
int rowCount = res.getRow();
Object[][] data = new Object[res.getRow()][meta.getColumnCount()];
res.beforeFirst();
int j = 1;
while(res.next()){
for(int i = 1 ; i <= meta.getColumnCount(); i++)
data[j-1][i-1] = res.getObject(i);
j++;
}
res.close();
state.close();
long totalTime = System.currentTimeMillis() - start;
result.removeAll();
result.add(new JScrollPane(new JTable(data, column)), BorderLayout.CENTER);
result.add(new JLabel("execute in " + totalTime + " ms and has " + rowCount + " ligne(s)"), BorderLayout.SOUTH);
result.revalidate();
} catch (SQLException e) {
result.removeAll();
result.add(new JScrollPane(new JTable()), BorderLayout.CENTER);
result.revalidate();
JOptionPane.showMessageDialog(null, e.getMessage(), "ERREUR ! ", JOptionPane.ERROR_MESSAGE);
}
thank you
The call to res.last()
is what is causing the trouble. If you want to know how many rows there are, then you can either issue first a SELECT count(*) FROM (<your-query> ) base
or simpler, use an ArrayList rather than an object array to hold the rows. (You can still use Object[] for each row, since the number of columns is known ahead of time.)
精彩评论