userdao.java
来自「struts做得固定资产管理系统 带毕业论文完整版」· Java 代码 · 共 159 行
JAVA
159 行
/*
* @(#)UserDAO.java Aug 8, 2007
* Copyright 2007 ThinkJ organization, Inc. All rights reserved
*/
package com.qrsx.appcam.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.qrsx.appcam.model.User;
public class UserDAO extends BaseDAO {
/**
* 创建用户
*
* @param user
* @throws SQLException
*/
public void create(User user) throws SQLException {
String sql = "Insert Into User(logonName, password,employeeId ) Values(?, ?, ?)";
// 设置参数值
ps = conn.prepareStatement(sql);
ps.setString(1, user.getLogonName());
ps.setString(2, user.getPassword());
ps.setInt(3, user.getEmployeeId());
// 执行
ps.executeUpdate();
}
/**
* 更新用户信息
*
* @param user
* @throws SQLException
*/
public void update(User user) throws SQLException {
String sql = "Update User set logonName=?, password=?,employeeId=? where id=? ";
// 设置参数值
ps = conn.prepareStatement(sql);
ps.setString(1, user.getLogonName());
ps.setString(2, user.getPassword());
ps.setInt(3, user.getEmployeeId());
ps.setInt(4, user.getId());
// 执行
ps.executeUpdate();
}
/**
* 删除用户
*
* @param UserId
*
* @return 删除的用户数目
*/
public int delete(Integer userId) throws SQLException {
String sql = "Delete from User where id=? ";
// 设置参数值
ps = conn.prepareStatement(sql);
ps.setInt(1, userId);
// 执行
int total = ps.executeUpdate();
return total;
}
/**
* 根据主键检索用户
*
* @param UserId
*
* @return User
*/
public User findById(Integer userId) throws SQLException {
String sql = "Select * from User where id=? ";
// 设置参数值并执行查询
ps = conn.prepareStatement(sql);
ps.setInt(1, userId);
ResultSet rs = ps.executeQuery();
EmployeeDAO dao = new EmployeeDAO();
User user = null;
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setLogonName(rs.getString("logonName"));
user.setPassword(rs.getString("password"));
user.setEmployeeId(rs.getInt("employeeId"));
user.setEmployee(dao.findById(rs.getInt("employeeId")));
}
return user;
}
/**
* 用户登陆
*
* @param logonName
* @param password
* @return User
* @throws SQLException
*/
public User findForLogon(String logonName, String password)
throws SQLException {
String sql = "select * from user where logonName=? and password=?";
// 设置参数值并执行查询
ps = conn.prepareStatement(sql);
ps.setString(1, logonName);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
EmployeeDAO dao = new EmployeeDAO();
User user = null;
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setLogonName(rs.getString("logonName"));
user.setPassword(rs.getString("password"));
user.setEmployeeId(rs.getInt("employeeId"));
user.setEmployee(dao.findById(rs.getInt("employeeId")));
}
return user;
}
/**
* 检索所有用户
*
* @return List<User>
* @throws SQLException
*/
public List<User> findAll() throws SQLException {
String sql = "Select * from User ";
// 执行查询
ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
// 将查询结果组装至User实体对象
User user = null;
List<User> list = new ArrayList<User>();
EmployeeDAO dao = new EmployeeDAO();
while (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setLogonName(rs.getString("logonName"));
user.setPassword(rs.getString("password"));
user.setEmployeeId(rs.getInt("employeeId"));
user.setEmployee(dao.findById(rs.getInt("employeeId")));
list.add(user);
}
return list;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?