2014-05-06 158 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()時,都會移至結果集中的下一行。 –