首页>代码>java开发“马拉松名人堂”管理系统>/stan2nats-master/src/com/hongbo5/top/dao/UserDao.java
package com.hongbo5.top.dao;
import com.hongbo5.top.model.User;
import com.hongbo5.top.model.PageBean;
import com.hongbo5.top.util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//管理员账号/密码Dao
public class UserDao {
	/**
	 * 管理员登录验证
	 * @param con
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public User login(Connection con, User user) throws Exception{
		User resultUser=null;
		String sql="select * from t_user where userName=? and password=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, user.getUserName());
		pstmt.setString(2, user.getPassword());
		ResultSet rs=pstmt.executeQuery();
		if(rs.next()){
			resultUser=new User();
			resultUser.setUserName(rs.getString("userName"));
			resultUser.setPassword(rs.getString("password"));
		}
		return resultUser;
	}
	public ResultSet userList(Connection con, PageBean pageBean, User user)throws Exception{
		StringBuffer sb=new StringBuffer("select * from t_user");
		//user!=null 查询功能块需要
		if (user!=null && StringUtil.isNotEmpty(user.getUserName())) { sb.append(" and userName like '%" + user.getUserName() + "%'");
		}
		if(pageBean!=null){
			sb.append(" limit "+pageBean.getStart()+","+pageBean.getRows());//分页功能
		}
		PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and","where"));
		return pstmt.executeQuery();
	}
	public int userCount(Connection con,User user)throws Exception{
		StringBuffer sb=new StringBuffer("select count(*) as total from t_user");
		if (StringUtil.isNotEmpty(user.getUserName())) {
			//and 该条件不一定执行
			sb.append(" and userName like '%" + user.getUserName() + "%'");
		}
		PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and","where"));
		ResultSet rs=pstmt.executeQuery();
		if(rs.next()){
			return rs.getInt("total");
		}else{
			return 0;
		}
	}
	public int userDelete(Connection con, String delIds)throws Exception {
		String sql = "delete from t_user where id in(" + delIds + ")";
		PreparedStatement psmt = con.prepareStatement(sql);
		return psmt.executeUpdate();
	}
	public int userAdd(Connection con, User user)throws Exception {
		String sql = "insert into t_user values(null,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1,user.getUserName());
		pstmt.setString(2,user.getPassword());
		return pstmt.executeUpdate();
	}
	public int userModify(Connection con,User user)throws Exception {
		String sql = "update t_user set userName=?,password=? where id=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1,user.getUserName());
		pstmt.setString(2,user.getPassword());
		pstmt.setInt(3,user.getId());
		return pstmt.executeUpdate();
	}
}
最近下载更多
poipoiiop  LV8 2023年1月3日
多吃多占  LV12 2022年11月30日
wkw1376744752  LV1 2022年10月26日
zhouzhouzhouyy  LV2 2022年10月25日
uni-code_0123  LV1 2022年10月24日
最近浏览更多
gann2015  LV25 4月8日
LLLlllAAAaaa  LV1 4月7日
陈小灏  LV18 1月4日
1239wky  LV1 2024年6月17日
bw200488  LV6 2024年6月6日
asddwh  LV13 2023年12月26日
WBelong  LV8 2023年12月26日
fff2003  LV9 2023年12月19日
kisooo 2023年11月8日
暂无贡献等级
xp95323  LV15 2023年10月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友