我的Java插件出现问题 - 如果没有找到结果,则说结果集为空。有人能告诉我如何检测结果集是否为空?
String url = "jdbc:mysql://" + host + ":" + port + "/" + database + "";
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
ResultSet rs;
String p = pp.getName();
rs = stmt.executeQuery("SELECT * FROM " + table + " WHERE username='"
+ p + "' AND recieved=0 ORDER BY id DESC");
rs = stmt.getResultSet();
答案 0 :(得分:2)
标准方法是尝试使用next()
方法获取下一行,并检查它是否返回false
(意味着没有下一行,因此它为空)。像这样:
ResultSet rs;
// run query on database
if (!rs.next()) {
// no row(s) found from database
} else {
// row(s) found from database OK
}
编写do...while
循环代码很方便:
if (!rs.next()) {
// handle no rows found
} else {
// process all rows
do {
// process row
} while (rs.next());
}