📄 departmentdao.java
字号:
package edu.yinhe.mis.model;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import edu.yinhe.mis.dto.DepartmentDTO;
import edu.yinhe.mis.vo.DepartmentVO;
import edu.yinhe.system.model.BaseDAO;
/**
* @author 赵训福
*/
public class DepartmentDAO extends BaseDAO {
/**
* 根据id,删除department表中的信息
* @throws SQLException
* @param obj id号
* @return flag 删除成功返回true,反之为false。
*/
public Object delete(Object obj) throws SQLException{
boolean flag =false;
PreparedStatement pstat = null;
String sql = null;
String did=(String) obj;
String []str=did.split(" ");
String id;
try {
sql = "DELETE FROM department WHERE ID=?";
pstat = conn.prepareStatement(sql);
if(str !=null){
for(int i=0; i<str.length; i++){
id=str[i];
if(id!=null && !"".equals(id)){
pstat.setInt(1, Integer.parseInt(id));
pstat.executeUpdate();
}
}
}
flag = true;
conn.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(pstat != null) pstat.close();
sql = null;
}
return flag;
}
@Override
public Object find() throws SQLException {
// TODO Auto-generated method stub
return super.find();
}
@Override
public Object find(Object obj) throws SQLException {
// TODO Auto-generated method stub
return super.find(obj);
}
@Override
public Object findAll() throws SQLException {
// TODO Auto-generated method stub
return super.findAll();
}
/**
* 查看department表中所有记录
* @throws SQLException
* @param obj DepartmentDTO
* @return objs 返回Object[] 对象数组
*/
public Object findAll(Object obj) throws SQLException {
PreparedStatement pstat = null;
String sql = null;
ResultSet rs = null;
DepartmentDTO deptdto = (DepartmentDTO) obj;
List list = new ArrayList();
DepartmentVO dvo = null;
Integer integer=new Integer(0);
Object[] objs = new Object[2];
StringBuffer sb = new StringBuffer("SELECT ID,DEPARTMENT_NO,SUPERDEPARTMENT_NAME,DEPARTMENT_NAME,PRINCIPAL,TEL,ADDRESS FROM department WHERE 1=1 ");
try {
integer = getCount(deptdto);
if(deptdto.getId()!=0 && !"".equals(deptdto.getId())){
sb.append(" AND ID='"+deptdto.getId()+"'");
}
if(deptdto.getDepartmentNo()!=null && !"".equals(deptdto.getDepartmentNo())){
sb.append(" AND DEPARTMENT_NO='"+deptdto.getDepartmentNo()+"'");
}
if(deptdto.getSuperdepartmentName()!=null && !"".equals(deptdto.getSuperdepartmentName())){
sb.append(" AND SUPERDEPARTMENT_NAME='"+deptdto.getSuperdepartmentName()+"'");
}
if(deptdto.getDepartmentName()!=null && !"".equals(deptdto.getDepartmentName())){
sb.append(" AND DEPARTMENT_NAME LIKE '%"+deptdto.getDepartmentName()+"%'");
}
if(deptdto.getPrincipal()!=null && !"".equals(deptdto.getPrincipal())){
sb.append(" AND PRINCIPAL LIKE '%"+deptdto.getPrincipal()+"%'");
}
if(deptdto.getTel()!=null && !"".equals(deptdto.getTel())){
sb.append(" AND TEL='"+deptdto.getTel()+"'");
}
if(deptdto.getAddress()!=null && !"".equals(deptdto.getAddress())){
sb.append(" AND ADDRESS='"+deptdto.getAddress()+"'");
}
int i = (Integer.parseInt(deptdto.getCurrentPage())-1)*Integer.parseInt(deptdto.getRowPerPage());
sb.append(" ORDER BY ID DESC LIMIT "+i+","+deptdto.getRowPerPage());
sql = sb.toString();
pstat = conn.prepareStatement(sql);
rs = pstat.executeQuery();
while(rs.next()){
dvo = new DepartmentVO();
dvo.setId(rs.getInt("id"));
dvo.setDepartmentNo(rs.getString("department_no"));
dvo.setSuperdepartmentName(rs.getString("superdepartment_name"));
dvo.setDepartmentName(rs.getString("department_name"));
dvo.setPrincipal(rs.getString("principal"));
dvo.setTel(rs.getString("tel"));
dvo.setAddress(rs.getString("address"));
list.add(dvo);
}
objs[0]=list;
objs[1]=integer;
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(pstat != null) pstat.close();
if(rs != null) rs.close();
sql = null;
sb = null;
deptdto = null;
}
return objs;
}
/**
* 根据id,查询department表中对应一条记录的详细信息
* @throws SQLException
* @param obj id号
* @return dvo 返回department表中记录的对象
*/
public Object findById(Object obj) throws SQLException {
PreparedStatement pstat = null;
String sql = null;
Integer id =(Integer) obj;
ResultSet rs = null;
DepartmentVO dvo = new DepartmentVO();
try {
sql = "SELECT ID,DEPARTMENT_NO,SUPERDEPARTMENT_NAME,DEPARTMENT_NAME,PRINCIPAL,TEL,ADDRESS FROM department WHERE ID="+id;
pstat = conn.prepareStatement(sql);
rs = pstat.executeQuery();
if(rs.next()){
dvo.setId(rs.getInt("id"));
dvo.setDepartmentNo(rs.getString("department_no"));
dvo.setSuperdepartmentName(rs.getString("superdepartment_name"));
dvo.setDepartmentName(rs.getString("department_name"));
dvo.setPrincipal(rs.getString("principal"));
dvo.setTel(rs.getString("tel"));
dvo.setAddress(rs.getString("address"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(pstat != null) pstat.close();
if(rs != null) rs.close();
sql = null;
}
return dvo;
}
@Override
public Object findByObject(Object obj) throws SQLException {
// TODO Auto-generated method stub
return super.findByObject(obj);
}
/**
* 查看department表中记录数
* @throws SQLException
* @param obj DepartmentDTO对象
* @return integer department表中记录数
*/
public int getCount(Object obj) throws SQLException {
PreparedStatement pstat = null;
String sql = null;
Integer integer=new Integer(0);
ResultSet rs = null;
DepartmentDTO deptdto = (DepartmentDTO) obj;
StringBuffer sb = new StringBuffer("SELECT COUNT(*)result FROM department WHERE 1=1");
try {
if(deptdto.getId()!=0 && !"".equals(deptdto.getId())){
sb.append(" AND ID='"+deptdto.getId()+"'");
}
if(deptdto.getDepartmentNo()!=null && !"".equals(deptdto.getDepartmentNo())){
sb.append(" AND DEPARTMENT_NO='"+deptdto.getDepartmentNo()+"'");
}
if(deptdto.getSuperdepartmentName()!=null && !"".equals(deptdto.getSuperdepartmentName())){
sb.append(" AND SUPERDEPARTMENT_NAME='"+deptdto.getSuperdepartmentName()+"'");
}
if(deptdto.getDepartmentName()!=null && !"".equals(deptdto.getDepartmentName())){
sb.append(" AND DEPARTMENT_NAME LIKE '%"+deptdto.getDepartmentName()+"%'");
}
if(deptdto.getPrincipal()!=null && !"".equals(deptdto.getPrincipal())){
sb.append(" AND PRINCIPAL LIKE '%"+deptdto.getPrincipal()+"%'");
}
if(deptdto.getTel()!=null && !"".equals(deptdto.getTel())){
sb.append(" AND TEL='"+deptdto.getTel()+"'");
}
if(deptdto.getAddress()!=null && !"".equals(deptdto.getAddress())){
sb.append(" AND ADDRESS='"+deptdto.getAddress()+"'");
}
sql = sb.toString();
pstat = conn.prepareStatement(sql);
rs = pstat.executeQuery();
if(rs.next()){
integer = new Integer(rs.getInt("result"));
}
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(pstat != null) pstat.close();
if(rs != null) rs.close();
sql = null;
sb = null;
deptdto = null;
}
return integer;
}
@Override
public Object getMaxId(Object tableName) throws SQLException {
// TODO Auto-generated method stub
return super.getMaxId(tableName);
}
@Override
public Object getMaxId(String tableName) throws SQLException {
// TODO Auto-generated method stub
return super.getMaxId(tableName);
}
/**
* 增加department表中一条记录
* @throws SQLException
* @param obj DepartmentDTO对象
* @return flag 成功返回true
*/
public Object insert(Object obj) throws SQLException {
PreparedStatement pstat = null;
boolean flag = false;
DepartmentDTO deptdto = null;
String sql = null;
try {
deptdto=(DepartmentDTO) obj;
sql = "INSERT INTO department(DEPARTMENT_NO,SUPERDEPARTMENT_NAME,DEPARTMENT_NAME,PRINCIPAL,TEL,ADDRESS) VALUES(?,?,?,?,?,?)";
pstat = conn.prepareStatement(sql);
pstat.setString(1, deptdto.getDepartmentNo());
pstat.setString(2, deptdto.getSuperdepartmentName());
pstat.setString(3, deptdto.getDepartmentName());
pstat.setString(4, deptdto.getPrincipal());
pstat.setString(5, deptdto.getTel());
pstat.setString(6, deptdto.getAddress());
pstat.executeUpdate();
conn.commit();
flag = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(pstat != null) pstat.close();
sql = null;
deptdto = null;
}
return flag;
}
/**
* 修改department表中一条记录
* @throws SQLException
* @param obj DepartmentDTO对象
* @return flag 修改成功返回true
*/
public Object update(Object obj) throws SQLException{
boolean flag =false;
PreparedStatement pstat = null;
int index =1;
DepartmentDTO deptdto = (DepartmentDTO) obj;
String sql = null;
try {
sql = "UPDATE department SET DEPARTMENT_NO=?,SUPERDEPARTMENT_NAME=?,DEPARTMENT_NAME=?,PRINCIPAL=?,TEL=?,ADDRESS=? WHERE ID=?";
pstat = conn.prepareStatement(sql);
pstat.setString(index++, deptdto.getDepartmentNo());
pstat.setString(index++, deptdto.getSuperdepartmentName());
pstat.setString(index++, deptdto.getDepartmentName());
pstat.setString(index++, deptdto.getPrincipal());
pstat.setString(index++, deptdto.getTel());
pstat.setString(index++, deptdto.getAddress());
pstat.setInt(index++, deptdto.getId());
pstat.executeUpdate();
flag = true;
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(pstat != null) pstat.close();
sql = null;
deptdto = null;
}
return flag;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -