You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
1.3 KiB
32 lines
1.3 KiB
package javabean;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
|
|
public class Manager {
|
|
@SuppressWarnings("null") // 忽略空值警告
|
|
public String login(String user, String psw) throws ClassNotFoundException, SQLException {
|
|
// 检查用户名是否为空或仅包含空格
|
|
if (user == null || user.trim().equals("")) {
|
|
return "账号不能为空"; // 返回错误信息
|
|
} else if (psw == null || psw.trim().equals("")) {
|
|
return "密码不能为空"; // 返回错误信息
|
|
}
|
|
Connection connection = null; // 数据库连接对象
|
|
PreparedStatement pstmt = null; // 预编译的SQL语句对象
|
|
ResultSet resultSet = null; // 结果集对象
|
|
String sql = "select * from manager where ACCOUNT=? and PASSWORD=?"; // SQL查询语句
|
|
connection = Base.getConnection(); // 获取数据库连接
|
|
pstmt = (PreparedStatement) connection.prepareStatement(sql); // 创建预编译的SQL语句对象
|
|
pstmt.setString(1, user); // 设置第一个参数(用户名)
|
|
pstmt.setString(2, psw); // 设置第二个参数(密码)
|
|
resultSet = pstmt.executeQuery(); // 执行查询并获取结果集
|
|
if (resultSet.next()) { // 如果结果集中有数据,表示登录成功
|
|
return "1"; // 返回成功标志
|
|
}
|
|
return "账号或密码错误"; // 返回错误信息
|
|
}
|
|
}
|