📄 operatordao.java
字号:
package com.cdaccp.dao;
import com.util.DBAccess;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.cdaccp.entity.Operator;
import java.sql.*;
public class OperatorDAO {
public OperatorDAO() {
}
/**
* 根据操作员的编号和密码验证该操作员是否有效
* @param id int 操作员编号
* @param password String 操作员密码
* @return boolean 有效则返回真,否则返回假
*/
public static boolean verifyUser(int id, String password) {
boolean b = false;
Connection con = DBAccess.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
String sql = "select * from operator where id = ? and pwd = ?";
try {
pst = con.prepareStatement(sql);
pst.setInt(1, id);
pst.setString(2, password);
rs = pst.executeQuery();
if (rs.next()) {
b = true;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try {
rs.close();
}
catch (SQLException ex1) {
ex1.printStackTrace();
}
DBAccess.closeStatement(pst);
DBAccess.closeConnection(con);
}
return b;
}
/**
* 根据操作员的编号或的该操作员的其他信息
* @param id int 参数为操作员的Id号
* @return Operator 返回一个Operator(操作员)对象
*/
public static Operator loadById(int id) {
Operator op = null;
Connection conn = DBAccess.getConnection();
PreparedStatement pstmt = null;
String sql = "select name,pwd from operator where id=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
op = new Operator();
op.setName(rs.getString("name"));
op.setPassword(rs.getString("pwd"));
op.setId(id);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
DBAccess.closeConnection(conn);
}
return op;
}
/**
* 添加操作员
* @param operator Operator 参数为一个Operator(操作员)对象
* @return boolean 成功返回真,否则返回假
*/
public static boolean addOperator(Operator operator) {
Connection con = DBAccess.getConnection();
PreparedStatement pst = null;
String sql = "insert into Operator (name,pwd) values(?,?)";
try {
pst = con.prepareStatement(sql);
pst.setString(1, operator.getName());
pst.setString(2, operator.getPassword());
int i = pst.executeUpdate();
if (i > 0) {
return true;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
DBAccess.closeStatement(pst);
DBAccess.closeConnection(con);
}
return false;
}
/**
* 更改操作员密码
* @param id int 操作员ID
* @param pwd String 要更改的密码
* @return boolean 成功返回true,矢败返回false
*/
public static boolean ChangePassword(int id, String pwd) {
Connection con = DBAccess.getConnection();
PreparedStatement pst = null;
String sql = "update operator set pwd = ? where id = ?";
try {
pst = con.prepareStatement(sql);
pst.setString(1, pwd);
pst.setInt(2, id);
int i = pst.executeUpdate();
if (i > 0) {
return true;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
DBAccess.closeConnection(con);
}
return false;
}
/**
* 查找操作员
*/
public static Operator loadByName(String name, String pwd) {
Operator op = null;
Connection conn = DBAccess.getConnection();
PreparedStatement pstmt = null;
String sql = "select * from operator where name=? and pwd =?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, pwd);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
op = new Operator();
op.setId(rs.getInt("id"));
op.setName(rs.getString("name"));
op.setPassword(rs.getString("pwd"));
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
DBAccess.closeConnection(conn);
}
return op;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -