I've got a problem with my Java plugin - when there's no results found it says that result set is empty. Could someone tell me how to detect if result set is empty?
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();
The standard approach is to attempt to get the next row using the next()
method, and check if it returned false
(meaning there is no next row, so it's empty). Like this:
ResultSet rs;
// run query on database
if (!rs.next()) {
// no row(s) found from database
} else {
// row(s) found from database OK
}
It can be convenient to code a do...while
loop:
if (!rs.next()) {
// handle no rows found
} else {
// process all rows
do {
// process row
} while (rs.next());
}