Update AdminDaoImpl.java

pull/1/head
prwfxgajt 8 months ago
parent 2772b289cf
commit 88614c6871

@ -1,162 +1,141 @@
package com.cn.dao.impl; package com.cn.dao.impl; // 数据访问对象实现包
import java.sql.Connection; import java.sql.*; // 导入SQL相关类
import java.sql.PreparedStatement; import java.util.*; // 导入集合相关类
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import com.cn.dao.AdminDao; import com.cn.dao.AdminDao; // 导入AdminDao接口
import com.cn.domain.Admin; import com.cn.domain.Admin; // 导入Admin实体类
import com.cn.util.JDBCUtil; import com.cn.util.JDBCUtil; // 导入JDBC工具类
/** /**
* @className AdminDaoImpl * AdminDao访
* @description
* @author lxs
* @date 201991
*/ */
public class AdminDaoImpl implements AdminDao { public class AdminDaoImpl implements AdminDao {
private Connection conn = null; private Connection conn = null; // 数据库连接
private PreparedStatement pstmt = null; private PreparedStatement pstmt = null; // 预编译语句
private ResultSet rs = null; private ResultSet rs = null; // 结果集
@Override @Override
public int add(Admin admin) throws SQLException { public int add(Admin admin) throws SQLException { // 添加管理员
String sql = "insert into admins (username,password,creattime,flag,isuse,logintime) VALUES (?,?,?,?,?,?)"; String sql = "insert into admins (username,password,creattime,flag,isuse,logintime) VALUES (?,?,?,?,?,?)";
conn = JDBCUtil.getConnection(); // 获取连接
conn = JDBCUtil.getConnection(); pstmt = conn.prepareStatement(sql); // 准备SQL语句
pstmt = conn.prepareStatement(sql); pstmt.setObject(1, admin.getUserName()); // 设置参数
pstmt.setObject(1, admin.getUserName()); pstmt.setObject(2, admin.getPassword());
pstmt.setObject(2, admin.getPassword()); pstmt.setObject(3, admin.getCreatTime());
pstmt.setObject(3, admin.getCreatTime()); pstmt.setObject(4, admin.getFlag());
pstmt.setObject(4, admin.getFlag()); pstmt.setObject(5, admin.getIsUse());
pstmt.setObject(5, admin.getIsUse()); pstmt.setObject(6, admin.getLoginTime());
pstmt.setObject(6, admin.getLoginTime()); int status = pstmt.executeUpdate(); // 执行插入
// 执行添加语句 closeResources(); // 关闭资源
int status = pstmt.executeUpdate(); return status;
// 关流 }
pstmt.close();
conn.close(); @Override
public int delete(int adminId) throws SQLException { // 删除管理员
return status; String sql = "delete from admins where adminid=?";
} conn = JDBCUtil.getConnection();
pstmt = conn.prepareStatement(sql);
@Override pstmt.setObject(1, adminId);
public int delete(int adminId) throws SQLException { int status = pstmt.executeUpdate();
String sql = "delete from admins where adminid=?"; closeResources();
return status;
conn = JDBCUtil.getConnection(); }
pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, adminId); @Override
// 执行删除语句 public int update(Admin admin) throws SQLException { // 更新管理员信息
int status = pstmt.executeUpdate(); String sql = "update admins set username=?,password=?,flag=?,isuse=?,logintime=? where adminid=?";
conn = JDBCUtil.getConnection();
pstmt.close(); pstmt = conn.prepareStatement(sql);
conn.close(); pstmt.setObject(1, admin.getUserName());
pstmt.setObject(2, admin.getPassword());
return status; pstmt.setObject(3, admin.getFlag());
} pstmt.setObject(4, admin.getIsUse());
pstmt.setObject(5, admin.getLoginTime());
pstmt.setObject(6, admin.getAdminId());
@Override int status = pstmt.executeUpdate();
public int update(Admin admin) throws SQLException { closeResources();
String sql = "update admins set username=?,password=?,flag=?,isuse=?,logintime=? where adminid=?"; return status;
}
conn = JDBCUtil.getConnection();
pstmt = conn.prepareStatement(sql); @Override
pstmt.setObject(1, admin.getUserName()); public Admin getById(int adminId) throws SQLException { // 根据ID获取管理员
pstmt.setObject(2, admin.getPassword()); String sql = "select * from admins where adminid=?";
pstmt.setObject(3, admin.getFlag()); conn = JDBCUtil.getConnection();
pstmt.setObject(4, admin.getIsUse()); pstmt = conn.prepareStatement(sql);
pstmt.setObject(5, admin.getLoginTime()); pstmt.setObject(1, adminId);
pstmt.setObject(6, admin.getAdminId()); rs = pstmt.executeQuery();
Admin admin = null;
int status = pstmt.executeUpdate(); if (rs.next()) {
admin = resultSetToAdmin(rs); // 将结果集转换为Admin对象
pstmt.close(); }
conn.close(); closeResources();
return admin;
return status; }
}
@Override
@Override public List<Admin> getAll() throws SQLException { // 获取所有管理员
public Admin getById(int adminId) throws SQLException { String sql = "select * from admins";
String sql = "select * from admins where adminid=?"; conn = JDBCUtil.getConnection();
conn = JDBCUtil.getConnection(); pstmt = conn.prepareStatement(sql);
pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery();
pstmt.setObject(1, adminId); List<Admin> admins = new ArrayList<>();
rs = pstmt.executeQuery(); while (rs.next()) {
Admin admin = null; admins.add(resultSetToAdmin(rs)); // 将结果集转换为Admin对象并添加到列表
while (rs.next()) { }
String userName = rs.getString("userName"); closeResources();
String password = rs.getString("password"); return admins;
Timestamp creatTime = rs.getTimestamp("creatTime"); }
int flag = rs.getInt("flag");
int isUse = rs.getInt("isUse"); @Override
Timestamp loginTime = rs.getTimestamp("loginTime"); public Admin getByName(String userName) throws SQLException { // 根据用户名获取管理员
admin = new Admin(adminId, userName, password, creatTime, flag, isUse, loginTime); String sql = "select * from admins where username=?";
} conn = JDBCUtil.getConnection();
pstmt = conn.prepareStatement(sql);
rs.close(); pstmt.setObject(1, userName);
pstmt.close(); rs = pstmt.executeQuery();
conn.close(); Admin admin = null;
if (rs.next()) {
return admin; admin = resultSetToAdmin(rs); // 将结果集转换为Admin对象
} }
closeResources();
@Override return admin;
public List<Admin> getAll() throws SQLException{ }
String sql = "select * from admins";
// 将结果集转换为Admin对象的方法
conn = JDBCUtil.getConnection(); private Admin resultSetToAdmin(ResultSet rs) throws SQLException {
pstmt = conn.prepareStatement(sql); int adminId = rs.getInt("adminId");
rs = pstmt.executeQuery(); String userName = rs.getString("userName");
String password = rs.getString("password");
List<Admin> admins = new ArrayList<Admin>(); Timestamp creatTime = rs.getTimestamp("creatTime");
while (rs.next()) { int flag = rs.getInt("flag");
int adminId = rs.getInt("adminId"); int isUse = rs.getInt("isUse");
String userName = rs.getString("userName"); Timestamp loginTime = rs.getTimestamp("loginTime");
String password = rs.getString("password"); return new Admin(adminId, userName, password, creatTime, flag, isUse, loginTime);
Timestamp creatTime = rs.getTimestamp("creatTime"); }
int flag = rs.getInt("flag");
int isUse = rs.getInt("isUse"); // 关闭数据库资源的方法
Timestamp loginTime = rs.getTimestamp("loginTime"); private void closeResources() {
Admin admin = new Admin(adminId, userName, password, creatTime, flag, isUse, loginTime); if (rs != null) {
admins.add(admin); try {
} rs.close();
} catch (SQLException e) {
rs.close(); e.printStackTrace();
pstmt.close(); }
conn.close(); }
if (pstmt != null) {
return admins; try {
} pstmt.close();
} catch (SQLException e) {
@Override e.printStackTrace();
public Admin getByName(String userName) throws SQLException { }
String sql = "select * from admins where username=?"; }
conn = JDBCUtil.getConnection(); if (conn != null) {
pstmt = conn.prepareStatement(sql); try {
pstmt.setObject(1, userName); conn.close();
rs = pstmt.executeQuery(); } catch (SQLException e) {
Admin admin = null; e.printStackTrace();
while (rs.next()) { }
int adminId = rs.getInt("adminId"); }
String password = rs.getString("password"); }
Timestamp creatTime = rs.getTimestamp("creatTime");
int flag = rs.getInt("flag");
int isUse = rs.getInt("isUse");
Timestamp loginTime = rs.getTimestamp("loginTime");
admin = new Admin(adminId, userName, password, creatTime, flag, isUse, loginTime);
}
rs.close();
pstmt.close();
conn.close();
return admin;
}
} }

Loading…
Cancel
Save