JDBC driver throws "ResultSet Closed" exception on empty ResultSet
Solution 1:
Empty or not, but doing the following is always faulty:
resultSet = statement.executeQuery(sql);
string = resultSet.getString(1); // Epic fail. The cursor isn't set yet.
This is not a bug. This is documented behaviour. Every decent JDBC tutorial mentions it. You need to set the ResultSet's cursor using next()
before being able to access any data.
If you're actually interested whether the supposedly unique row exist or not, then just check the outcome of next()
. For example in a fictive UserDAO
class:
public boolean exist(String username, String password) throws SQLException {
boolean exist = false;
try (
Connection connection = database.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT id FROM user WHERE username = ? AND password = MD5(?)");
) {
statement.setString(1, username);
statement.setString(2, password);
try (ResultSet resultSet = statement.executeQuery()) {
exist = resultSet.next();
}
}
return exist;
}
If you actually expect only zero or one row, then just do something like:
public User find(String username, String password) throws SQLException {
User user = null;
try (
Connection connection = database.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT id, username, email, birthdate FROM user WHERE username = ? AND password = MD5(?)");
) {
statement.setString(1, username);
statement.setString(2, password);
try (resultSet = statement.executeQuery()) {
if (resultSet.next()) {
user = new User(
resultSet.getLong("id"),
resultSet.getString("username"),
resultSet.getString("email"),
resultSet.getDate("birthdate"));
}
}
}
return user;
}
and then just handle it accordingly in the business/domain object, e.g.
User user = userDAO.find(username, password);
if (user != null) {
// Login?
}
else {
// Show error?
}
If you actually expect only zero or many rows, then just do something like:
public List<User> list() throws SQLException {
List<User> users = new ArrayList<User>();
try (
Connection connection = database.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT id, username, email, birthdate FROM user");
ResultSet resultSet = statement.executeQuery();
) {
while (resultSet.next()) {
users.add(new User(
resultSet.getLong("id"),
resultSet.getString("username"),
resultSet.getString("email"),
resultSet.getDate("birthdate")));
}
}
return users;
}
and then just handle it accordingly in the business/domain object, e.g.
List<User> users = userDAO.list();
if (!users.isEmpty()) {
int count = users.size();
// ...
}
else {
// Help, no users?
}
Solution 2:
while (rs.next()) {
// process the row
}
Solution 3:
From the JavaDocs for ResultSet:
A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.
You'll need to position the ResultSet
on a row, e.g. by calling calling next()
, before attempting to read any data. If the call to next()
returns false
then the result set is empty.
Solution 4:
For such kind of of problem you might use like that
while(rs.next())
{
String name=rs.getString("Name");
int roll_no=Integer.parseInt(rs.getString("Roll"));
}
finally
{
try
{
rs.close();
pst.close();
}
catch(Exception ee)
{
}
}
Joptionpane.showmessahedialog(this,ee);