⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 departmentdao.java

📁 07年做得人力资源管理系统
💻 JAVA
字号:
 package com.buat.hr.department;

import java.util.ArrayList;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.SQLException;

import com.buat.hr.share.DBConnection;


public class DepartmentDao implements IDepartmentDao {
	
	DBConnection db=new DBConnection();

	public boolean addDepartment(Department department) {//添加记录
		
		Connection con=db.getConnection();
		
		StringBuffer sql=new StringBuffer();
		sql.append(" insert into Department(name,leader,brief)  ");
		sql.append(" values(?,?,?) ");
		
		PreparedStatement pstmt=null;
		try {
			pstmt = con.prepareStatement(sql.toString(),ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

			pstmt.setString(1, department.getName());
			pstmt.setString(2, department.getLeader());
			pstmt.setString(3, department.getBrief());
		    
			pstmt.executeUpdate();
		} catch (SQLException e) {
		} finally {
			try {
				if (con != null) 
					con.close();
			} catch (SQLException e) {
			}
		}
		return true;
	}

	public boolean deleteDepartment(int departmentId) {//删除记录
		Connection con = db.getConnection();
		try 
		{
			PreparedStatement pstmt = con.prepareStatement("delete from Department where departmentId = ? ",
																		ResultSet.TYPE_SCROLL_SENSITIVE,
																				ResultSet.CONCUR_UPDATABLE);
			pstmt.setInt(1, departmentId);
			pstmt.executeUpdate();
		} catch (SQLException e) 
		{
			e.printStackTrace();
		} finally {
			try 
			{
				if (con != null)
					con.close();
			} catch (SQLException e) {
			}
		}
		return true;
	}

	public ArrayList queryDepartment() {//查询记录
		
		Connection con = db.getConnection();
		
		ArrayList list=new ArrayList();
		
		try {
			PreparedStatement pre = con.prepareStatement("select * from Department ",
										ResultSet.TYPE_SCROLL_SENSITIVE,
											ResultSet.CONCUR_UPDATABLE);
			
			ResultSet rs = pre.executeQuery();

			while (rs.next()) 
			{			
				Department department = new Department();
				
				department.setDepartmentId(rs.getInt("departmentId"));
				department.setName(rs.getString("name"));
				department.setLeader(rs.getString("leader"));
				department.setBrief(rs.getString("brief"));

				list.add(department);
			}
			return list;
		} catch (SQLException e) {
			return null;
		} 
		finally 
		{
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
			}
		}
	}
		
	public int queryDepartmentLast() {//查询最后一条记录
		
		Connection con = db.getConnection();
		String url="select * from Department";
		int number=0;
		try {
			PreparedStatement pre = con.prepareStatement(url);
			ResultSet rs = pre.executeQuery();
			rs.last();
			number=rs.getInt("departmentId");
		} catch (SQLException e) {
		} 
		finally 
		{
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
			}
		}
		return number;
	}

	public boolean updateDepartment(Department department) {//更新记录
		Connection con = db.getConnection();
		PreparedStatement pstmt = null;
		try 
		{
			pstmt = con.prepareStatement("update Department set name=?,leader=?,brief=? where departmentId=?",
							ResultSet.TYPE_SCROLL_SENSITIVE,
									ResultSet.CONCUR_UPDATABLE);
			pstmt.setString(1, department.getName());
			pstmt.setString(2, department.getLeader());
			pstmt.setString(3, department.getBrief());
			pstmt.setInt(4, department.getDepartmentId());
	
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally 
		{
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
			}
		}
		return true;
	}

	public Department queryDepartmentByDepartmentId(int departmentId) {//通过id查询部门
		Department department = new Department();                      //返回一个department对象
		Connection con = db.getConnection();
		try {
			PreparedStatement ps = con.prepareStatement("select * from Department where departmentId = ?");
		    ps.setInt(1,departmentId);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				department.setDepartmentId(rs.getInt("departmentId"));
				department.setName(rs.getString("name"));
				department.setLeader(rs.getString("leader"));
				department.setBrief(rs.getString("brief"));
			}
		} catch (Exception e) {
		} finally{
				try {
					if(	con!= null)
						con.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
		return department;
	}

	public ArrayList queryDepartment(int beginIndex, int countPerPage) //每页的记录个数
	{
		Connection con = db.getConnection();
		
		StringBuffer sql=new StringBuffer("select * from Department limit ?,?");
		ArrayList list=new ArrayList();
		
		try 
		{
			PreparedStatement pstt=con.prepareStatement(sql.toString(),ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
			pstt.setInt(1, beginIndex);
			pstt.setInt(2, countPerPage);
			ResultSet rst=pstt.executeQuery();
			
			while(rst.next()){
				Department department=new Department();
				
				int departmentId=rst.getInt(1);
				String name=rst.getString(2);
				String leader=rst.getString(3);
				String brief=rst.getString(4);
		  				
				department.setDepartmentId(departmentId);
				department.setName(name);
				department.setLeader(leader);
				department.setBrief(brief);

				list.add(department);
			}				 
		} catch (SQLException e) {			
		}finally{
			try 
			{
				if(	con!= null)	
					con.close();
			} catch (Exception e) {
			}
		}			
		return list;	
	}

	public int queryDepartmentCount() //表中记录的总数
	{
		int total=0;
		Connection con=db.getConnection();
		String sql="select count(*) from Department";
		try {
			PreparedStatement pstt=con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE);
			ResultSet rst=pstt.executeQuery();
			if(rst.next()){
				total=rst.getInt(1);
				con.close();
			}
		} catch (SQLException e) {
		}
		return total;
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -