Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java. sql. SQLException: Before Start Of Result Set. how Can I Solve IT? [duplicate]

Tags:

java

jdbc

How can I solve this error and what is the problem?

Here is my code:

     String sql = "select min(pressure),max(pressure),avg(pressure) from userinfo";
    pst = conn.prepareStatement(sql);
    rs = pst.executeQuery();
    String add1 = rs.getString("min(pressure)");
    min_pressure.setText(add1);
    String add2 = rs.getString("max(pressure)");
    max_pressure.setText(add2);
    String add3 = rs.getString("avg(pressure)");
    avg_pressure.setText(add3);
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }

This results in the exception:

java. sql. SQLException: Before Start Of ResultSet

like image 682
Raihan Uddin Avatar asked Oct 25 '25 03:10

Raihan Uddin


1 Answers

The ResultSet javadoc says (in part) initially the cursor is positioned before the first row. Advance the resultset cursor to the first row by calling next() like

rs = pst.executeQuery();
rs.next(); // <-- or if (rs.next()) {
String add1 = rs.getString("min(pressure)");
like image 125
Elliott Frisch Avatar answered Oct 26 '25 23:10

Elliott Frisch