private boolean checkUser(String username, String password) { // TODO: 这里需要编写查询数据库的代码,判断用户是否存在 return false; }补全代码数据库名为research
时间: 2024-02-27 16:55:48 浏览: 26
好的,以下是代码内容:
```
private boolean checkUser(String username, String password) {
// 连接数据库
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
boolean result = false;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/research?useSSL=false";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
// 查询用户名和密码是否匹配
String sql = "SELECT * FROM user WHERE username=? AND password=?";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery();
if (rs.next()) {
result = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
```
需要注意的是,这里使用的是 MySQL 数据库,并且用户名和密码保存在名为 `user` 的表中。你需要根据你的具体情况修改数据库连接信息和 SQL 语句。