package com.bjsxt.drp.business.usermgr.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import com.bjsxt.drp.business.usermgr.model.User;
import com.bjsxt.drp.business.util.DB;
/**
* 用户增删改查Dao层MySql实现
*
*/
public class UserDao4MySqlImpl implements UserDao {
/**
* 增加用户
* @param conn
* @param user user对象
*/
public void addUser(Connection conn, User user) {
String sql = "insert into t_user(user_id, user_name, password, contact_tel, email, create_date) "
+ "values(?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUserId());
pstmt.setString(2, user.getUserName());
pstmt.setString(3, user.getPassword());
pstmt.setString(4, user.getContactTel());
pstmt.setString(5, user.getEmail());
pstmt.setTimestamp(6, new Timestamp(user.getCreateDate().getTime()));
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.closeStmt(pstmt);
}
}
/**
* 根据userId的集合删除用户
* @param conn
* @param userIdList userId的集合
*/
public void deleteUsers(Connection conn, String[] userIdList) {
StringBuffer sbfSql = new StringBuffer();
for (int i = 0; i < userIdList.length; i++) {
sbfSql.append("'")
.append(userIdList[i])
.append("'")
.append(",");
}
String sql = "delete from t_user where user_id in (" + sbfSql.substring(0, sbfSql.length()-1) + ")";
Statement stmt = null;
try {
stmt = conn.createStatement();
stmt.executeUpdate(sql);
}catch(SQLException e) {
e.printStackTrace();
}finally {
DB.closeStmt(stmt);
DB.closeConn(conn);
}
}
/**
* 查询所有用户
* @return user对象列表
*/
public List findAllUserList() {
String sql ="select * from t_user where user_id <> 'root' order by user_id ";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List userList = new ArrayList();
try {
conn = DB.getConn();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
User user = new User();
user.setUserId(rs.getString("user_id"));
user.setUserName(rs.getString("user_name"));
user.setPassword(rs.getString("password"));
user.setContactTel(rs.getString("contact_tel"));
user.setEmail(rs.getString("email"));
user.setCreateDate(rs.getTimestamp("create_date"));
userList.add(user);
}
}catch(SQLException e) {
e.printStackTrace();
}finally {
DB.closeRs(rs);
DB.closeStmt(stmt);
DB.closeConn(conn);
}
return userList;
}
/**
* 根据用户id查询用户
* @param userId 用户id
* @return user对象
*/
public User findUserById(String userId) {
String sql = "select * from t_user where user_id=?";
User user = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DB.getConn();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userId);
rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setUserId(rs.getString("user_id"));
user.setUserName(rs.getString("user_name"));
user.setPassword(rs.getString("password"));
user.setContactTel(rs.getString("contact_tel"));
user.setEmail(rs.getString("email"));
user.setCreateDate(rs.getTimestamp("create_date"));
}
}catch(SQLException e) {
e.printStackTrace();
}finally {
DB.closeRs(rs);
DB.closeStmt(pstmt);
DB.closeConn(conn);
}
return user;
}
/**
* 修改用户
* @param conn
* @param user user对象
*/
public void modifyUser(Connection conn, User user) {
String sql = "update t_user set user_name=?, password=?, contact_tel=?, email=? where user_id=?";
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getContactTel());
pstmt.setString(4, user.getEmail());
pstmt.setString(5, user.getUserId());
pstmt.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}finally {
DB.closeStmt(pstmt);
}
}
}
yxkfw
- 粉丝: 82
- 资源: 2万+
最新资源
- Adams多体动力学仿真 Cruise 动力性,经济性仿真 动力系统参数匹配 纯电动汽车,混合动力汽车,双电机汽车 simulink控制策略开发,制动能量回收,转矩分配,驻车机构动力学仿真分析 联合仿
- 机械设计滚切机sw19可编辑非常好的设计图纸100%好用.zip
- python-29.日志分析-我直接化身调度员.py
- 机械设计焊接机械手与变位机调试模拟sw18非常好的设计图纸100%好用.zip
- python-30.兔八哥与猎人-千里之外锁你兔头.py
- 批量重命名软件win系统环境下可用
- python-31.路径计数2-千百度.py
- Python基于BERT和朴素贝叶斯算法的新闻文本分类源码+数据集+实验报告,基于NaiveBayes的新闻情感分类模型
- 利用ADS设计射频功率放大亲
- 计算机考研408真题,个人学习整理,仅供参考
- 计算机考研408统考复习试卷真题合集
- 害虫识别+python+神经网络【数据集+代码+操作手册】
- YOLOv8深度学习对象检测完整流程指南:从环境搭建到模型部署
- jeecgBoot-master.zip,JeecgBoot的Vue3版本的前后端源码
- 微信点餐系统微信小程序+JAVA毕业设计 源码+数据库+论文+配套教程.zip
- 微信小程序源码-随堂测微信小程序-服务端-毕业设计源码-期末大作业.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈