3

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

a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
Raihan Uddin
  • 66
  • 2
  • 8
  • 1
    You should also use the [try-with-resources statement](http://stackoverflow.com/questions/8066501/how-should-i-use-try-with-resources-with-jdbc) for managing JDBC resources efficiently and properly. – Mick Mnemonic Mar 27 '16 at 18:30
  • 1
    you might want to go through the [JDBC tutorial](http://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html) – a_horse_with_no_name Mar 27 '16 at 18:39

2 Answers2

3

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)");
Elliott Frisch
  • 191,680
  • 20
  • 149
  • 239
0
rs = pst.executeQuery();
if (rs.next()) {
    String add1 = rs.getStr
}
Floern
  • 32,709
  • 24
  • 103
  • 117