Java 结果集异常 - 在结果集开始之前

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2120255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:45:04  来源:igfitidea点击:

ResultSet exception - before start of result set

javajdbc

提问by Nick Heiner

I'm having trouble getting data from a ResultSetobject. Here is my code:

我在从ResultSet对象获取数据时遇到问题。这是我的代码:

    String sql = "SELECT type FROM node WHERE nid = ?";
    PreparedStatement prep = conn.prepareStatement(sql);
    int meetNID = Integer.parseInt(node.get(BoutField.field_meet_nid));
    prep.setInt(1, meetNID);

    ResultSet result = prep.executeQuery();
    result.beforeFirst();
    String foundType = result.getString(1);

    if (! foundType.equals("meet")) {
        throw new IllegalArgumentException(String.format("Node %d must be of type 'meet', but was %s", meetNID, foundType));
    }

The error trace:

错误跟踪:

Exception in thread "main" java.sql.SQLException: Before start of result set
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1072)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:986)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:981)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
    at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5656)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5576)
    at nth.cumf3.nodeImport.Validator.validate(Validator.java:43)
    at nth.cumf3.nodeImport.Main.main(Main.java:38)

What am I doing wrong here?

我在这里做错了什么?

采纳答案by Vincent Ramdhanie

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 语句或循环中执行此操作是很常见的。

if(result.next()){
   foundType = result.getString(1);
}

回答by Paul Tomblin

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

您必须先执行 result.next() 才能访问结果。这是一个很常见的习惯用法

ResultSet rs = stmt.executeQuery();
while (rs.next())
{
   int foo = rs.getInt(1);
   ...
}

回答by Dan

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

您必须先调用,next()然后才能开始从第一行读取值。beforeFirst将光标放在第一行之前,因此没有要读取的数据。

回答by ryanprayogo

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

在请求数据之前,您需要将指针移动到第一行:

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

回答by Pedro

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.

最好创建一个包含所有查询方法的类,包括在不同的包中,这样就不必在每个类中键入所有过程,而只需从该类中调用该方法。

回答by MmynameStackflow

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

每个答案都使用.next()或使用.beforeFirst()然后.next()。但为什么不是这个:

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 ResultSetexists of one specific record.

所以你只需将指针设置为第一条记录并从那里开始。它从 java 1.2 开始可用,我只是想为ResultSet存在特定记录的任何人提及这一点。