我有一个表'admin'的数据库。但是目前只是创建没有任何值的表。表列是user_name和password.My的动机是检查表是否为空。我正在使用mysql数据库。我尝试了代码及其失败。请帮助我。
public void nullCheck() {
PreparedStatement stmt = null;
ResultSet rs = null;
String qry = "SELECT * From admin ";
try {
stmt = (PreparedStatement) conn.prepareStatement(qry);
rs = stmt.executeQuery();
boolean empty = true;
while( rs.next() ) {
// ResultSet processing here
empty = false;
}
if( empty ) {
Util.showWarningMessageDialog("null");
}
} catch (SQLException ex) {
Logger.getLogger(RemoveFaculty.class.getName()).log(Level.SEVERE, null, ex);
}
}
答案 0 :(得分:0)
如果您只需要查看表格,那么您应该使用查询:
String qry = "SELECT count(*) From admin ";
以获得更好的表现。 并从ResultSet获取行计数以检查表是否为空。
int count=0;
while( rs.next() )
{
count=rs.getInt("count");
}
答案 1 :(得分:-1)
尝试此代码,我认为这可以做到。我更新了它,因为我在这里发布的第一篇文章根本没有用,我的错。
public void nullCheck() {
PreparedStatement stmt = null;
ResultSet rs = null;
String qry = "SELECT * From admin ";
try {
stmt = (PreparedStatement) conn.prepareStatement(qry);
rs = stmt.executeQuery();
int count = 0;
while(rs.next()){
count++;
}
if(count == 0){ // if equal to 0 then the table is null
bla bla bla
}
} catch (SQLException ex) {
Logger.getLogger(RemoveFaculty.class.getName()).log(Level.SEVERE, null, ex);
}
}