我在Java中的表已连接到mysql。它检索表中的数据。但是当我指定getrowcount()时,它在system.out.print中显示0行。请帮助,我们的项目截止日期为6月3日。
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","tiger");
stmt = conn.createStatement();
if(ch1.isSelected()){
String query = "Select * from details where name like '"+d2.getText()+"%';";
rs = stmt.executeQuery(query);
System.out.print(table.getRowCount());
}
if(table.getRowCount() == 0){
JOptionPane.showMessageDialog(null,"No records found");
}
while(rs.next()){
int id;
id = rs.getInt("userid");
String name = rs.getString("name");
String gender = rs.getString("gender");
String dob = rs.getString("dob");
String phno = rs.getString("phoneno");
String doj = rs.getString("joindate");
String doe = rs.getString("exitdate");
model.addRow(new Object[]{id,name,gender,dob,phno,doj,doe});
}
rs.close();
stmt.close();
conn.close();
}
catch(ClassNotFoundException | SQLException e){
JOptionPane.showMessageDialog(null,"Error");
}
d1.setText("");
d2.setText("");
d3.setSelectedItem("");
d4.setText("");
d5.setText("");
d6.setText("");
d7.setText("");
update.setEnabled(false);
delete.setEnabled(false);
table.getRowCount()
不能提供行数,因为结果集中有数据。
使用单独的计数查询和检查。
Statement s = conn.createStatement();
String countQuery = "SELECT COUNT(*) AS rowcount from details where name like '"+d2.getText()+"%';";
ResultSet r = s.executeQuery(countQuery);
r.next();
int count = r.getInt("rowcount");
请检查下面的getRowCount()给您的内容
您可以参考下面的链接进行更多说明https://docs.oracle.com/javase/8/docs/api/javax/swing/JTable.html#getRowCount--