Java ResultSet how to check if there are any results
That's correct, initially the ResultSet
's cursor is pointing to before the first row, if the first call to next()
returns false
then there was no data in the ResultSet
.
If you use this method, you may have to call beforeFirst()
immediately after to reset it, since it has positioned itself past the first row now.
It should be noted however, that Seifer's answer below is a more elegant solution to this question.
Check if ResultSet is empty in Java
If I understand your objective, you could use do while
loop
if (!results.next()) {
System.out.println("empty");
} else {
//display results
do {
String data = results.getString("first_name");
//name.setText(data);
System.out.println(data);
} while (results.next());
}
Or, you could just keep a count
like so,
int count = 0;
//display results
while (results.next()) {
String data = results.getString("first_name");
//name.setText(data);
System.out.println(data);
count++;
}
if (count < 1) {
// Didn't even read one row
}
How to check if resultset has one row or more?
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
boolean isMoreThanOneRow = rs.first() && rs.next();
You didn't ask this one, but you may need it:
boolean isEmpty = ! rs.first();
Normally, we don't need the row count because we use a WHILE loop to iterate through the result set instead of a FOR loop:
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
// retrieve and print the values for the current row
int i = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
System.out.println("ROW = " + i + " " + s + " " + f);
}
However, in some cases, you might want to window the results, and you need the record count ahead of time to display to the user something like Row 1 to 10 of 100
. You can do a separate query with SELECT COUNT(*)
first, to get the record count, but note that the count is only approximate, since rows can be added or removed between the time it takes to execute the two queries.
Sample from ResultSet Overview
How to find out if a Java ResultSet obtained is empty?
The pattern I normally use is as follows:
boolean empty = true;
while( rs.next() ) {
// ResultSet processing here
empty = false;
}
if( empty ) {
// Empty result set
}
How to find whether a ResultSet is empty or not in Java?
Immediately after your execute statement you can have an if statement. For example
ResultSet rs = statement.execute();
if (!rs.next()){
//ResultSet is empty
}
How do I check for value existence / why is ResultSet closed in SQLite?
resultSet.next()
returns a boolean which tells you whether there is a next record or not. Ignoring this returned value is like shooting your own foot with a rocket launcher.
So, if the value you are looking for is not in the result set, (either because it really isn't, or because you did not ask for it properly,) resultSet.next()
will return false
, and your code will blow up with the exception that you are receiving.
Your resultSet.getString(1)
clause will fetch the first column in the result set, (if there is a current row,) and it will return it to you assuming that it is in fact a string. The first column of most tables is usually an integer, or some other kind of key data type, which means that it is rarely a string. If you are lucky, it will be something that the JDBC driver can convert to a string, but you are tempting your fate by assuming that.
If you are only going to check the value of a single column, then your query statement must select that column only. This means that instead of
"select * from " + tableName + " where " + columnName + "=? limit 1"
you must do
"select " + columnName + " from " + tableName + " where " + columnName + "=? limit 1"
However, if I understand correctly what you are trying to achieve, you do not even need to fetch the field and check its value. Simply the true
or false
result of resultSet.next()
should suffice.
How to check if Resultset empty or not
Your could use a boolean variable :
boolean isEmpty = true;
while (resSet.next()) {
isEmpty = false;
Log.d(TAG, "selectAll", "ID: "+resSet.getString(ID_COL));
Log.d(TAG, "selectAll", "Node: "+resSet.getString(NODE_ID_COL));
Log.d(TAG, "selectAll", "Lat: "+resSet.getString(LAT_COL));
Log.d(TAG, "selectAll", "Lng: "+resSet.getString(LNG_COL));
Log.d(TAG, "selectAll", "xmlPath: "+resSet.getString(XML_PATH_COL));
}
resultset in jdbc is returning true
Use :
if (!rs3.next() ) {
System.out.println("no data");
}
initially the ResultSet's cursor is pointing to before the first row, if the first call to next() returns false then there was no data in the ResultSet.
if you are working with a newly returned ResultSet whose cursor is pointing before the first row, an easier way to check this is to just call isBeforeFirst().
if (!rs3.isBeforeFirst() ) {
System.out.println("No data");
}
it's better to use prepared statment to avoid sql injection :
PreparedStatement updateemp = connnection.prepareStatement
("SELECT * from phone_model WHERE phone_type =? and manufacturer=?");
updateemp.setString(1,phone_type_choice);
updateemp.setString(2, phone_manufacturer);
How To use prepared statement.
Related Topics
Java Error: Implicit Super Constructor Is Undefined for Default Constructor
Illegalmonitorstateexception on Wait() Call
Difference Between File.Separator and Slash in Paths
Using Setdate in Preparedstatement
Can't Add Value to the Java Collection with Wildcard Generic Type
Centering a Jlabel on a JPAnel
Combineddomainxyplot Not Rescaling Domain Axis
Antlr: Is There a Simple Example
How to Import a .Cer Certificate into a Java Keystore
How to Use Java Property Files
How to Deal with a Slow Securerandom Generator
Instantiating Generics Type in Java
Http Servlet Request Lose Params from Post Body After Read It Once