Update AdminDaoImpl.java

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

@ -1,70 +1,50 @@
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(); // 执行插入
int status = pstmt.executeUpdate(); closeResources(); // 关闭资源
// 关流
pstmt.close();
conn.close();
return status; return status;
} }
@Override @Override
public int delete(int adminId) throws SQLException { public int delete(int adminId) throws SQLException { // 删除管理员
String sql = "delete from admins where adminid=?"; String sql = "delete from admins where adminid=?";
conn = JDBCUtil.getConnection(); conn = JDBCUtil.getConnection();
pstmt = conn.prepareStatement(sql); pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, adminId); pstmt.setObject(1, adminId);
// 执行删除语句
int status = pstmt.executeUpdate(); int status = pstmt.executeUpdate();
closeResources();
pstmt.close();
conn.close();
return status; return status;
} }
@Override @Override
public int update(Admin admin) throws SQLException { public int update(Admin admin) throws SQLException { // 更新管理员信息
String sql = "update admins set username=?,password=?,flag=?,isuse=?,logintime=? where adminid=?"; String sql = "update admins set username=?,password=?,flag=?,isuse=?,logintime=? where adminid=?";
conn = JDBCUtil.getConnection(); conn = JDBCUtil.getConnection();
pstmt = conn.prepareStatement(sql); pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, admin.getUserName()); pstmt.setObject(1, admin.getUserName());
@ -73,90 +53,89 @@ public class AdminDaoImpl implements AdminDao {
pstmt.setObject(4, admin.getIsUse()); pstmt.setObject(4, admin.getIsUse());
pstmt.setObject(5, admin.getLoginTime()); pstmt.setObject(5, admin.getLoginTime());
pstmt.setObject(6, admin.getAdminId()); pstmt.setObject(6, admin.getAdminId());
int status = pstmt.executeUpdate(); int status = pstmt.executeUpdate();
closeResources();
pstmt.close();
conn.close();
return status; return status;
} }
@Override @Override
public Admin getById(int adminId) throws SQLException { public Admin getById(int adminId) throws SQLException { // 根据ID获取管理员
String sql = "select * from admins where adminid=?"; String sql = "select * from admins where adminid=?";
conn = JDBCUtil.getConnection(); conn = JDBCUtil.getConnection();
pstmt = conn.prepareStatement(sql); pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, adminId); pstmt.setObject(1, adminId);
rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
Admin admin = null; Admin admin = null;
while (rs.next()) { if (rs.next()) {
String userName = rs.getString("userName"); admin = resultSetToAdmin(rs); // 将结果集转换为Admin对象
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);
} }
closeResources();
rs.close();
pstmt.close();
conn.close();
return admin; return admin;
} }
@Override @Override
public List<Admin> getAll() throws SQLException{ public List<Admin> getAll() throws SQLException { // 获取所有管理员
String sql = "select * from admins"; String sql = "select * from admins";
conn = JDBCUtil.getConnection(); conn = JDBCUtil.getConnection();
pstmt = conn.prepareStatement(sql); pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
List<Admin> admins = new ArrayList<>();
List<Admin> admins = new ArrayList<Admin>();
while (rs.next()) { while (rs.next()) {
int adminId = rs.getInt("adminId"); admins.add(resultSetToAdmin(rs)); // 将结果集转换为Admin对象并添加到列表
String userName = rs.getString("userName");
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 admin = new Admin(adminId, userName, password, creatTime, flag, isUse, loginTime);
admins.add(admin);
} }
closeResources();
rs.close();
pstmt.close();
conn.close();
return admins; return admins;
} }
@Override @Override
public Admin getByName(String userName) throws SQLException { public Admin getByName(String userName) throws SQLException { // 根据用户名获取管理员
String sql = "select * from admins where username=?"; String sql = "select * from admins where username=?";
conn = JDBCUtil.getConnection(); conn = JDBCUtil.getConnection();
pstmt = conn.prepareStatement(sql); pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, userName); pstmt.setObject(1, userName);
rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
Admin admin = null; Admin admin = null;
while (rs.next()) { if (rs.next()) {
admin = resultSetToAdmin(rs); // 将结果集转换为Admin对象
}
closeResources();
return admin;
}
// 将结果集转换为Admin对象的方法
private Admin resultSetToAdmin(ResultSet rs) throws SQLException {
int adminId = rs.getInt("adminId"); int adminId = rs.getInt("adminId");
String userName = rs.getString("userName");
String password = rs.getString("password"); String password = rs.getString("password");
Timestamp creatTime = rs.getTimestamp("creatTime"); Timestamp creatTime = rs.getTimestamp("creatTime");
int flag = rs.getInt("flag"); int flag = rs.getInt("flag");
int isUse = rs.getInt("isUse"); int isUse = rs.getInt("isUse");
Timestamp loginTime = rs.getTimestamp("loginTime"); Timestamp loginTime = rs.getTimestamp("loginTime");
admin = new Admin(adminId, userName, password, creatTime, flag, isUse, loginTime); return new Admin(adminId, userName, password, creatTime, flag, isUse, loginTime);
} }
// 关闭数据库资源的方法
private void closeResources() {
if (rs != null) {
try {
rs.close(); rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close(); pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close(); conn.close();
} catch (SQLException e) {
return admin; e.printStackTrace();
}
}
} }
} }

Loading…
Cancel
Save