Update AdminDaoImpl.java

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

Loading…
Cancel
Save