简体   繁体   English

Java SQL:结果集之前的错误

[英]Java SQL: Error before result set

I'm having a problem with a query in my java application. 我的Java应用程序中的查询有问题。 It's a simple query to get a users score based on their name, however, I am constantly getting an error and can't spot where the problem is coming from. 这是一个简单的查询,可以根据用户的姓名获得用户评分,但是,我不断收到错误消息,无法找出问题的根源。

            String driver = "com.mysql.jdbc.Driver";    
        Class.forName(driver).newInstance();

        con = DriverManager.getConnection(url, USERNAME, PASSWORD);   
        st  = con.createStatement();


        PreparedStatement preStatement = con.prepareStatement("select score from playerscore where name=?");
        preStatement.setString(1, "tomcat");

        ResultSet resultSet = preStatement.executeQuery();
        score               = resultSet.getInt(1);

Could someone cast an eye and point me in the right direction? 有人可以注视我并指向正确的方向吗?

EDIT: Image of stack trace 编辑:堆栈跟踪的图像

http://imageshack.us/photo/my-images/854/stackr.jpg/ http://imageshack.us/photo/my-images/854/stackr.jpg/

Initially, the ResultSet is positioned before the first row. 最初, ResultSet位于第一行之前 Therefore, you must first call resultSet.next() to move to the first row, before you can retrieve any of its values. 因此,必须先调用resultSet.next()移至第一行,然后才能检索其任何值。

You can verify if the first row is indeed present by checking the returned boolean of resultSet.next() . 您可以通过检查返回的resultSet.next() boolean来验证第一行是否确实存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM