我正在用 Java 编写一个简单的程序,用于连接到 MySQL。我尝试编写一个简单的查询来仅检查用户名和密码(我在运行程序时输入的)是否在我的数据库中。
由于我对 JDBC 没有太多经验,所以我想知道 Java 是否有像 PHP 的 mysql_num_rows
这样的方法,检查特定信息是否在我的数据库中。
最佳答案
使用简单的 SELECT 语句:
String username = "..."; //the username, it could be a method parameter
String password = "..."; //the password, it could be a method parameter
Connection con = .... //retrieve the connection the way you're doing it now
//replace ... for the data you want/need from user
String sql = "SELECT ... FROM user WHERE name = ? and password = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
//read the data from ResultSet
}
rs.close();
pstmt.close();
con.close();
关于java - Java中有类似mysql_num_rows的东西吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27489523/