【问题标题】:Java resultset.next() not returning resultsJava resultset.next() 不返回结果
【发布时间】:2014-05-06 00:57:33
【问题描述】:

有一个连接到 apache derby 数据库的 java 类。突然停止工作,我不知道为什么。

基本上是一个客户端连接到服务器,服务器调用“联系人”类,然后联系人类查询数据库并返回结果。

这是来自 Contact 类的代码:

import java.sql.*;

//Contact Class. Used to get information from the contact database.
//Takes the database connection and the student ID as arguments
public class Contact {
//Define the original variables
String student = null;
Connection db = null;
PreparedStatement selectStatement = null;
ResultSet resultSet = null;
StringBuilder result = null;

//Constructor method. Used to set passed arguments to class variables.
public Contact(Connection conn, String studentNumber)
{
    this.student = studentNumber;
    this.db = conn;
}

//getResult method used to query the database and return result.
public String getResult()
{
    //Wrap it all in a try loop to catch sql errors.
    try {
        //Set up the statement, prepare the statement and set the variable.
        String selectSQL = "SELECT * FROM Contact WHERE STUID = ?";
        selectStatement = db.prepareStatement(selectSQL);                   
        selectStatement.setString(1, this.student);
        //Execute the query
        resultSet = selectStatement.executeQuery();
        //If there is no results, set up a string to return letting the user know.
        this.result = new StringBuilder();
        if(!resultSet.next())
        {
            result.append("No record for ");
            result.append(this.student);
            result.append(".");
        }
        else
        {
            //If there are results, loop through the result and build a string
            //To be able to return to the user with the correct details.
            System.out.println("FOUND A RESULT");
            while(resultSet.next())
            {
                System.out.println("TEST");
                System.out.println(resultSet.getString("STUID"));
                result.append(resultSet.getString("STUID"));
                result.append(" ");
                result.append(resultSet.getString("STUNAME"));
                result.append(" ");
                result.append(resultSet.getString("ADDRESS"));
                result.append(" ");
                result.append(resultSet.getString("POSTCODE"));
                result.append(" ");
                result.append(resultSet.getString("EMAIL"));
            }
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //Return the built string with correct information.
    return this.result.toString();
}
}

如果我输入了一个不在数据库中的 STUID,它会通过返回“No Record For ID”成功让我知道。但是,如果我输入数据库中的一个,它会打印“FOUND A RESULT”行(只是一个测试行),但实际上永远不会到达“TEST”输出 - 因此永远不会用结果构建任何字符串。

我知道这不是数据库,因为我在我的线程类中测试了一些代码(在调用此联系人类之前),并且查询相同的数据库有效:

    Statement s = null;
    ResultSet rs = null;
    try{
        s = dbConnection.createStatement();
        rs = s.executeQuery("SELECT * FROM Contact");
        while(rs.next())
        {
            System.out.println(rs.getString("STUID"));
        }
    }catch(SQLException e)
    {
        e.printStackTrace();
    }

该测试代码有效。

所以我真的很困惑为什么它可以成功查询数据库,并确定没有结果(通过使用 if(!resultSet.next() ),但如果它确实有一个结果它无法通过循环向我提供详细信息。任何帮助将不胜感激!

【问题讨论】:

    标签: java sql derby


    【解决方案1】:

    发生这种情况是因为您超过了结果集中的第一个结果:

    if(!resultSet.next())
    {
        result.append("No record for ");
        result.append(this.student);
        result.append(".");
    }
    else // <-- failurehere
    {
    

    你的 else 隐式调用 resultSet.next() ,这将超过第一个元素。如果您查询包含两个元素的内容,则只会返回第二个元素。

    【讨论】:

    • 这适用于一个结果 - 将其交换为 if(resultSet.next())。如果我返回了多个结果,我该怎么办?
    • 每次调用 ResultSet.Next() 时,都会移动到结果集中的下一行。
    猜你喜欢
    • 2016-02-22
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多