2014-05-06 161 views
0

有一个连接到apache derby数据库的java类。突然停止工作,我不知道为什么。Java resultset.next()不返回结果

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

下面是从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,是不是在数据库中,它成功地让我知道通过返回“没有记录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()),但如果它实际上存在,实际上有因此,它不能管理遍历给我详细的任何帮助将不胜感激

回答

1

这是因为你正在过去在结果的第一个结果:!

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

你是别的隐含地调用resultSet.next(),它将移过第一个元素,如果你对两个元素进行查询,你只会得到第二个元素一个返回。

+0

这适用于一个结果 - 将其交换为if(resultSet.next())。 如果我返回了多个结果,我该怎么办? – CynePhoba12

+1

每次调用ResultSet.Next()时,都会移至结果集中的下一行。 –