[英]Why are my rows in my Jtable not being displayed?
I don't understand why, when I execute my request, only the headers of my jtable are correct but the row aren't display. 我不明白为什么,当我执行请求时,只有jtable的标头是正确的,而行却没有显示。
Here is my code 这是我的代码
private void jButtonRequeteSqlExecuterActionPerformed(java.awt.event.ActionEvent evt) {
try {
//Taking the query from a txt
String query = jTextPaneRequeteSql.getText();
// Initialisation request time
long start = System.currentTimeMillis();
//Creating a statement
Connection con = getConnection();
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = st.executeQuery(query);
//Little manipulation to get the number of row
rs.last();
int rowCount = rs.getRow();
//Calculate the time
long totalTime = System.currentTimeMillis() - start;
//Get the model
jTableRequeteSql.setModel(buildTableModel(rs));
//display the time
jLabelRequeteSql.setText("La requête à été exécuter en " + totalTime + " ms et a retourné " + rowCount + " ligne(s)");
System.out.println("La requête à été exécuter en " + totalTime + " ms et a retourné " + rowCount + " ligne(s)");
//Refresh the display
jTableRequeteSql.revalidate();
jTableRequeteSql.repaint();
//Closing connection
rs.close();
st.close();
con.close();
}
catch (SQLException e) {
//Dans le cas d'une exception, on affiche une pop-up et on efface le contenu
JOptionPane.showMessageDialog(null, e.getMessage(), "ERREUR ! ", JOptionPane.ERROR_MESSAGE);
}
}
To understand better my code you should know : getConnection () is a function that returns the con (simple connection to database function). 为了更好地理解我的代码,您应该知道:getConnection()是一个返回con的函数(与数据库函数的简单连接)。
The buildTableModel(rs) function is a function to make a dynamic table with number of columns and their names dynamically : buildTableModel(rs)函数是用于动态创建具有列数及其名称的动态表的函数:
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
} }
Ahhh, I can see clearly now ... 啊,我现在看清楚了...
rs.last();
int rowCount = rs.getRow();
you've moved the cursor to the end of the ResultSet
, but you've not reset it, so when you call ResultSet#next
, it fails, as it's already at the end. 您已将光标移动到ResultSet
的末尾,但尚未重置,因此,当您调用ResultSet#next
,它会失败,因为它已经在末尾了。
You need to use ResultSet#beforeFirst
to reset the cursor to the start of the ResultSet
您需要使用ResultSet#beforeFirst
将光标重置为ResultSet
的开始
And because your code is annoying me... 而且因为您的代码让我很烦...
//Taking the query from a txt
String query = jTextPaneRequeteSql.getText();
try (Connection con = getConnection();
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = st.executeQuery(query)) {
// Initialisation request time
long start = System.currentTimeMillis();
//Little manipulation to get the number of row
rs.last();
int rowCount = rs.getRow();
//Calculate the time
long totalTime = System.currentTimeMillis() - start;
rs.beforeFirst();
//Get the model
jTableRequeteSql.setModel(buildTableModel(rs));
//display the time
jLabelRequeteSql.setText("La requête à été exécuter en " + totalTime + " ms et a retourné " + rowCount + " ligne(s)");
System.out.println("La requête à été exécuter en " + totalTime + " ms et a retourné " + rowCount + " ligne(s)");
//Refresh the display
jTableRequeteSql.revalidate();
jTableRequeteSql.repaint();
} catch (SQLException e) {
//Dans le cas d'une exception, on affiche une pop-up et on efface le contenu
JOptionPane.showMessageDialog(null, e.getMessage(), "ERREUR ! ", JOptionPane.ERROR_MESSAGE);
}
See try-with-resources for more details 有关更多详细信息,请参见尝试资源
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.