ResultSet exception - before start of result set

后端 未结 6 769
独厮守ぢ
独厮守ぢ 2020-11-21 23:30

I\'m having trouble getting data from a ResultSet object. Here is my code:

    String sql = \"SELECT type FROM node WHERE nid = ?\";
    PreparedStatement pr         


        
相关标签:
6条回答
  • 2020-11-22 00:10

    You have to call next() before you can start reading values from the first row. beforeFirst puts the cursor before the first row, so there's no data to read.

    0 讨论(0)
  • 2020-11-22 00:11

    It's better if you create a class that has all the query methods, inclusively, in a different package, so instead of typing all the process in every class, you just call the method from that class.

    0 讨论(0)
  • 2020-11-22 00:21

    Basically you are positioning the cursor before the first row and then requesting data. You need to move the cursor to the first row.

     result.next();
     String foundType = result.getString(1);
    

    It is common to do this in an if statement or loop.

    if(result.next()){
       foundType = result.getString(1);
    }
    
    0 讨论(0)
  • 2020-11-22 00:21

    You need to move the pointer to the first row, before asking for data:

    result.beforeFirst();
    result.next();
    String foundType = result.getString(1);
    
    0 讨论(0)
  • 2020-11-22 00:31

    You have to do a result.next() before you can access the result. It's a very common idiom to do

    ResultSet rs = stmt.executeQuery();
    while (rs.next())
    {
       int foo = rs.getInt(1);
       ...
    }
    
    0 讨论(0)
  • 2020-11-22 00:34

    Every answer uses .next() or uses .beforeFirst() and then .next(). But why not this:

    result.first();
    

    So You just set the pointer to the first record and go from there. It's available since java 1.2 and I just wanted to mention this for anyone whose ResultSet exists of one specific record.

    0 讨论(0)
提交回复
热议问题