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.
34 lines
1.3 KiB
34 lines
1.3 KiB
package javabean;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
|
|
public class Reader {
|
|
@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 borrow_card where ID=? and PASSWORD=?"; // SQL查询语句
|
|
|
|
connection = Base.getConnection(); // 获取数据库连接
|
|
pstmt = (PreparedStatement) connection.prepareStatement(sql); // 创建PreparedStatement对象
|
|
pstmt.setString(1, user); // 设置第一个参数(用户名)
|
|
pstmt.setString(2, psw); // 设置第二个参数(密码)
|
|
resultSet = pstmt.executeQuery(); // 执行查询
|
|
|
|
if (resultSet.next()) { // 如果查询有结果,说明账号密码正确
|
|
return "1";
|
|
}
|
|
return "账号或密码错误"; // 如果没有查询到结果,返回错误信息
|
|
}
|
|
}
|