package com.book.dao; import com.book.model.User; import com.book.util.StringUtil; import com.mysql.jdbc.Connection; import com.mysql.jdbc.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class UserDao { public User login(Connection con,User user)throws Exception { User resultUser = null; String sql = "select * from user where username=? and password=? and role = ?"; PreparedStatement pstmt = (PreparedStatement) con.prepareStatement(sql); pstmt.setString(1,user.getUserName()); pstmt.setString(2,user.getPassword()); pstmt.setInt(3,user.getRole()); ResultSet rs = pstmt.executeQuery(); if(rs.next()){ resultUser = new User(); resultUser.setUserId(rs.getInt("id")); resultUser.setUserName(rs.getString("username")); } return resultUser; } public int addUser(Connection con,User user) throws Exception{ //查询username是否存在 String sql = "select * from user where userName=? "; PreparedStatement pstmt = (PreparedStatement) con.prepareStatement(sql); pstmt.setString(1,user.getUserName()); ResultSet rs = pstmt.executeQuery(); if(rs.next()){ return 2; } sql="insert into user (username,password,role) values (?,?,?)"; PreparedStatement pstmt2=(PreparedStatement) con.prepareStatement(sql); pstmt.setString(1, user.getUserName()); pstmt.setString(2, user.getPassword()); pstmt.setInt(3, user.getRole()); return pstmt.executeUpdate(); } public ResultSet list(Connection con,User user)throws Exception{ StringBuffer sb=new StringBuffer("select * from user where role = 1"); if(!StringUtil.isEmpty(user.getUserName())){ sb.append(" and username like '%"+user.getUserName()+"%'"); } PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sb.toString()); return pstmt.executeQuery(); } public int update(Connection con,User user)throws Exception{ String sql="update user set username=?,password=? where id=?"; PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sql); pstmt.setString(1, user.getUserName()); pstmt.setString(2, user.getPassword()); pstmt.setInt(3, user.getUserId()); return pstmt.executeUpdate(); } }
最近下载更多
lsglsg9 LV22
2023年12月20日
sunshine9920 LV12
2023年10月23日
hongdongdong LV12
2023年8月9日
12345681 LV2
2023年6月10日
gxluchuan LV1
2023年4月7日
lolokei LV1
2022年10月10日
喃喵xxxx LV6
2022年9月6日
zxcvbnm1580 LV1
2022年6月15日
testuser1234567 LV24
2022年5月31日
sks666 LV5
2022年5月25日
最近浏览更多
小小白银110 LV1
1月2日
偷喝汽水
2023年12月26日
暂无贡献等级
lshlsh
2023年12月25日
暂无贡献等级
huangzy LV12
2023年12月21日
夜猫子111 LV2
2023年11月6日
卢本伟不开挂 LV4
2023年10月22日
sunshine9920 LV12
2023年10月22日
ezra77934 LV2
2023年9月16日
hongdongdong LV12
2023年8月9日
yuexiaolong LV1
2023年7月4日