📄 departmentman.java
字号:
package com.csbook.documentsystem;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class DepartmentMan{
Context ctx=null;
DataSource ds=null;
SysLog log=null;
ChangeEncoding ce=null;
//构造函数
public DepartmentMan()
{
//从连接池中获取数据库连接
try{
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("documents");
}
catch(NamingException e){
e.printStackTrace();
}
log = new SysLog();
ce=new ChangeEncoding();
}
//删除部门信息
public void removeDept(String operator,String deptname)
{
Connection con = null;
PreparedStatement ps = null;
try{
String strDelete = "delete from department where name=?";
con = ds.getConnection();
ps = con.prepareStatement(strDelete);
ps.setString(1,ce.changeCharset(deptname));
ps.executeUpdate();
log.addLog(operator,"removed dept "+deptname, "department");
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
}
//检查是否已存在指定名称的部门
public boolean isDeptNameConflict(String strName)
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs=null;
boolean conflict=true;
try{
String strQuery = "select * from department where name=?";
con = ds.getConnection();
ps = con.prepareStatement(strQuery);
ps.setString(1,ce.changeCharset(strName));
rs=ps.executeQuery();
if(!rs.next())
conflict=false;
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (rs != null) try {rs.close();}
catch (SQLException ignore) {}
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
return conflict;
}
//添加部门
public void addDept(String operator,String deptname)
{
Connection con = null;
PreparedStatement ps = null;
try{
String strInsert = "insert into department(name) values(?)";
con = ds.getConnection();
ps = con.prepareStatement(strInsert);
ps.setString(1,ce.changeCharset(deptname));
ps.executeUpdate();
log.addLog(operator,"add department "+ce.changeCharset(deptname),"department");
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
}
//设置部门描述信息
public void setDeptDesc(String operator,String deptname,String deptdesc)
{
Connection con = null;
PreparedStatement ps = null;
try{
String strUpdate = "update department set description=? where name=?";
con = ds.getConnection();
ps = con.prepareStatement(strUpdate);
ps.setString(1,ce.changeCharset(deptdesc));
ps.setString(2,ce.changeCharset(deptname));
ps.executeUpdate();
log.addLog(operator,"set department description of "+ce.changeCharset(deptname)+":"+ce.changeCharset(deptdesc),"department");
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
}
//获取部门信息列表,供部门管理页面(deptMan.jsp)使用
public ArrayList getDeptInfo()
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs=null;
ArrayList dept=new ArrayList();
try{
String strQuery = "select * from department";
con = ds.getConnection();
ps = con.prepareStatement(strQuery);
rs=ps.executeQuery();
String temp;
while(rs.next()){
String des=rs.getString("description");
if(des!=null)
temp=ce.changeCharset(rs.getString("name"))+" "+ce.changeCharset(des);
else
temp=ce.changeCharset(rs.getString("name"))+" empty";
dept.add(temp);
}
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (rs != null) try {rs.close();}
catch (SQLException ignore) {}
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
return dept;
}
//获取部门名称列表
public ArrayList getDeptList()
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs=null;
ArrayList dept=new ArrayList();
try{
String strQuery = "select name from department";
con = ds.getConnection();
ps = con.prepareStatement(strQuery);
rs=ps.executeQuery();
String temp;
while(rs.next()){
temp=rs.getString("name");
dept.add(ce.changeCharset(temp));
}
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (rs != null) try {rs.close();}
catch (SQLException ignore) {}
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
return dept;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -