deptmentdao.java
来自「struts做得固定资产管理系统 带毕业论文完整版」· Java 代码 · 共 151 行
JAVA
151 行
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.Deptment;
public class DeptmentDAO extends BaseDAO {
/**
* 创建部门
*
* @param AssetType
*/
public void create(Deptment deptment) throws SQLException {
String sql = "Insert Into deptment(name, description) Values(?, ?)";
// 设置参数值
ps = conn.prepareStatement(sql);
ps.setString(1, deptment.getName());
ps.setString(2, deptment.getDescription());
// 执行
ps.executeUpdate();
}
/**
* 更新部门信息
*
* @param deptment
*/
public void update(Deptment deptment) throws SQLException {
String sql = "Update deptment set name=?, description=? where id=? ";
// 设置参数值
ps = conn.prepareStatement(sql);
ps.setString(1, deptment.getName());
ps.setString(2, deptment.getDescription());
ps.setInt(3, deptment.getId());
// 执行
ps.executeUpdate();
}
/**
* 删除部门
*
* @param deptmentId
* 要删除的部门的主键
* @return 删除的用户数目
*/
public int delete(Integer deptmentId) throws SQLException {
String sql = "Delete from deptment where id=? ";
// 设置参数值
ps = conn.prepareStatement(sql);
ps.setInt(1, deptmentId);
// 执行
int total = ps.executeUpdate();
return total;
}
/**
* 根据主键检索部门
*
* @param deptmentId
* 要检索的部门主键
* @return 检索到的部门
*/
public Deptment findById(Integer deptmentId) throws SQLException {
String sql = "Select * from deptment where id=? ";
// 设置参数值并执行查询
ps = conn.prepareStatement(sql);
ps.setInt(1, deptmentId);
ResultSet rs = ps.executeQuery();
Deptment deptment = null;
if (rs.next()) {
deptment = new Deptment();
deptment.setId(rs.getInt("id"));
deptment.setName(rs.getString("name"));
deptment.setDescription(rs.getString("description"));
}
return deptment;
}
/**
* 检索所有部门
*
* @param deptment
* @return
*/
public List<Deptment> findAll() throws SQLException {
String sql = "Select * from deptment ";
// 执行查询
ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
// 将查询结果组装至deptment实体对象
Deptment deptment = null;
List<Deptment> list = new ArrayList<Deptment>();
while (rs.next()) {
deptment = new Deptment();
deptment.setId(rs.getInt("id"));
deptment.setName(rs.getString("name"));
deptment.setDescription(rs.getString("description"));
list.add(deptment);
}
return list;
}
/**
* 根据条件检索部门
*
* @param deptment
* @return
*/
public List<Deptment> list(Deptment deptment) throws SQLException {
String sql = "Select * from deptment where 1=1 ";
// 根据资产类型名称检索
if (deptment.getName() != null && !deptment.getName().equals("")) {
sql = sql + " and name like '%" + deptment.getName() + "%'";
}
// 执行查询
ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
// 将查询结果组装至deptment实体对象
Deptment deptment1 = null;
List<Deptment> list = new ArrayList<Deptment>();
while (rs.next()) {
deptment1 = new Deptment();
deptment1.setId(rs.getInt("id"));
deptment1.setName(rs.getString("name"));
deptment1.setDescription(rs.getString("description"));
list.add(deptment1);
}
return list;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?