我靠學校的一些Java,出於某種原因下面的代碼不返回任何結果?這段代碼有問題嗎?有在MySQL數據庫中的12爲什麼此查詢不返回任何結果?
public static ResultSet GetByID(int studentID) {
// This method loads the mysql driver and establishes the database connection
Connect();
ResultSet results = null;
try {
String query = "SELECT * FROM student where studentID = ?";
PreparedStatement statement = Connection.prepareStatement(query);
statement.setInt(1, studentID);
results = statement.executeQuery();
} catch (SQLException ex) {
LogException(ex);
} catch (Exception ex) {
System.out.println(ex);
}
// This method terminates the mysql connection.
Disconnect();
return results;
}
調用代碼的單個記錄,具有studentID
(這是int(10)
)是:
@Override
public ResultSet query() {
return DB.GetByID(getStudentID()); // this is 12
}
這不返回null,而只是一個空結果集。
假設你確定studentID == 10,並且沒有拋出異常嗎? – cwallenpoole
您是否收到任何錯誤/異常?如果不是,那麼您使用的學生證是否存在於數據庫E中? – Waqas
我已檢查並正常返回,但是當我讀取從此返回的ResultSet時,沒有記錄。它不屬於任何一個catch塊。 – Eli