1 回答

TA貢獻1890條經驗 獲得超9個贊
您不能用于ResultSet.equals
此目的,因為這不是Object.equals
合同的用途。它用于檢查一個對象是否等于相同(或至少兼容)類型的另一個對象。因此, AResultSet
永遠不會等于對象值數組。
看起來您想從表中選擇與emp
您的搜索值匹配的一行,在這種情況下,正確的解決方案是僅向數據庫詢問該行。選擇所有行然后在 Java 應用程序中進行過濾效率非常低,因為數據庫必須將所有行發送到您的應用程序,而查找數據正是數據庫所擅長的。
相反,您應該使用帶有準備好的語句的 where 子句:
try (Connection connection = DriverManager.getConnection(url, userid, pwd);
? ? ?PreparedStatement pstmt = connection.prepareStatement(
? ? ? ? ?"select * from emp where Name = ? and Designation = ? and Pay = ? and City = ?")) {
? ?pstmt.setString(1, search);
? ?pstmt.setString(2, search1);
? ?pstmt.setDouble(3, search2);
? ?pstmt.setString(4, search3);
? ?try (ResultSet rs = pstmt.executeQuery()) {
? ? ? ?if (rs.next() {
? ? ? ? ? ?String nm = rs.getString("Name");
? ? ? ? ? ?String desg = rs.getString("Designation");
? ? ? ? ? ?double pay = rs.getDouble("Pay");
? ? ? ? ? ?String city = rs.getString("City");
? ? ? ? ? ?jTextField1.setText(nm);
? ? ? ? ? ?jTextField2.setText(desg);
? ? ? ? ? ?jTextField3.setText(String.valueOf(pay));
? ? ? ? ? ?jTextField4.setText(city);
? ? ? ?} else {
? ? ? ? ? ?// handle not found case
? ? ? ?}
? ?}?
}
添加回答
舉報