![](/img/trans.png)
[英]Java (JDBC) : Alternatives for iteration of query results [ while(resultSet.next()) ]
[英]Java resultset.next() not returning results
得到了一個連接到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”成功地通知我。 但是,如果我輸入的是數據庫中的一個,它將打印“找到結果”行(只是測試行),但實際上並沒有到達“ 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()),但是如果它可以得出實際結果,則可以無法設法給我提供詳細信息。任何幫助將不勝感激!
發生這種情況是因為您要跳過結果集中的第一個結果:
if(!resultSet.next())
{
result.append("No record for ");
result.append(this.student);
result.append(".");
}
else // <-- failurehere
{
您的另一個隱式調用resultSet.next()
,它將移到第一個元素之后。 如果您對包含兩個元素的內容進行查詢,則只會返回第二個元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.