我是新來的mysql和m試圖從eclipse中的mysql表中選擇N行。現在,我想從數據庫中選擇N行相同的值。我使用下面的代碼從mysql數據庫的N行中選擇相同的值
User user= null;
ArrayList<User> searchedUsers = new ArrayList<User>();
PreparedStatement stmt = null;
ResultSet rs = null;
try {
String authenticationSql;
authenticationSql = "SELECT * FROM users WHERE " + searchIn + " = ?";
log.info(authenticationSql);
stmt = (PreparedStatement) dbConn.prepareStatement(authenticationSql);
stmt.setString(1, searchFor);
rs = stmt.executeQuery();
while (rs.next()) {
user = new User(rs.getString("username"),
rs.getInt("user_type"), OnlineStatus.ONLINE);
searchedUsers.add(user);
}
rs.close();
stmt.close();
} catch (SQLException ex) {
log.error("SQLException: " + ex.getMessage());
log.error("SQLState: " + ex.getSQLState());
log.error("VendorError: " + ex.getErrorCode());
}
問題是這樣的代碼只返回我搜索的第一個值,而不是值的其餘部分是從數據庫中選擇。可以請一些人指出我在這裏做錯了什麼。任何幫助將不勝感激。謝謝。
是一些錯誤..它是用java – Hadi
什麼是完整的SQL語句之前執行?否則,'searchFor'的價值是什麼。 – hafichuk
完整的語句就像這樣.. 「SELECT *,COUNT(*)FROM users WHERE username =?」 searchFor是一個具有所需用戶名的字符串 – Hadi