Failed to get results from H2 db

I try to get values ​​from h2 db, but always get this error

org.h2.jdbc.JdbcSQLException: No data is available [2000-171] at org.h2.message.DbException.getJdbcSQLException(DbException.java:329) at org.h2.message.DbException.get(DbException.java:169) at org.h2.message.DbException.get(DbException.java:146) at org.h2.message.DbException.get(DbException.java:135) at org.h2.jdbc.JdbcResultSet.checkOnValidRow(JdbcResultSet.java:2956) at org.h2.jdbc.JdbcResultSet.get(JdbcResultSet.java:2962) at org.h2.jdbc.JdbcResultSet.getInt(JdbcResultSet.java:306)

I googled for an answer

Be sure to call rs.next (); before using any of the getter methods.

But I call rs.next () ... I just can not understand

Here is my code:

 public User getUser(int userId) throws SQLException { User u = new User(userId); try { Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM invited_users WHERE user_id=" + userId); rs.next(); u.setName(rs.getString("user_name")); } catch (SQLException except) { JOptionPane.showMessageDialog(null, "Unable to load user! " + except); } return u; } 
+6
source share
1 answer

The problem was the result set; it was empty.

Just replace this code

 rs.next(); u.setName(rs.getString("user_name")); 

with

 if (rs.next()) { u.setName(rs.getString("user_name")); } 
+9
source

Source: https://habr.com/ru/post/978123/


All Articles