【Java开源代码栏目提醒】:网学会员--在 Java开源代码编辑为广大网友搜集整理了:BorrowMan.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 BorrowMan{
Context ctx = null;
DataSource ds = null;
SysLog log = null;
ChangeEncoding ce=null;
//构造函数
public BorrowMan() {
//从连接池中获取数据库连接
try {
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("documents");
}
catch (NamingException e) {
e.printStackTrace();
}
log = new SysLog();
ce=new ChangeEncoding();
}
//借阅档案
public void borrowDocument(int doc, String borrower,
String begintime, String endtime, String reason) {
Connection con = null;
PreparedStatement ps = null;
DocMan dMan=new DocMan();
String docName=dMan.getDocNameById(doc);
if (this.getBorrowStateById(doc) == 1){
log.addMsg("system", borrower, "您所申请借阅的" + docName + "已被别人借阅,请稍候时日", 1);
}
else {
try {
String strInsert = "insert into borrowRecord(document,borrower,begintime,endtime,reason,state) values(?,?,?,?,?,?)";
con = ds.getConnection();
ps = con.prepareStatement(strInsert);
ps.setInt(1, doc);
ps.setString(2, borrower);
ps.setString(3, begintime);
ps.setString(4, endtime);
ps.setString(5,ce.changeCharset(reason));
ps.setInt(6, 0);
ps.executeUpdate();
log.addMsg("system", borrower, "您借阅" + docName + "的申请已经提交,等待批准", 1);
}
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 Hashtable getBorrowInfo(int borrowId) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Hashtable borrows = new Hashtable();
try {
String strQuery = "select * from borrowRecord where id=?";
con = ds.getConnection();
ps = con.prepareStatement(strQuery);
ps.setInt(1, borrowId);
rs = ps.executeQuery();
String temp;
Integer temp1;
if (rs.next()) {
borrows.put("document",(new Integer(rs.getInt("document")).toString()));
borrows.put("borrower", rs.getString("borrower"));
borrows.put("begintime", rs.getString("begintime"));
borrows.put("endtime", rs.getString("endtime"));
temp1=new Integer(rs.getInt("state"));
borrows.put("state", temp1.toString());
temp=rs.getString("lender");
if(temp==null||temp.equals(""))
borrows.put("lender"," ");
else
borrows.put("lender",temp);
borrows.put("reason",ce.changeCharset(rs.getString("reason")));
}
}
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 borrows;
}
//根据借阅编号获得档案编号
public int getDocById(int id) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
int doc = 0;
try {
String strQuery = "select document from borrowRecord where id=?";
con = ds.getConnection();
ps = con.prepareStatement(strQuery);
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next())
doc = rs.getInt("document");
}
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 doc;
}
//根据档案编号获得该档案的借阅状态
public int getBorrowStateById(int id) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
int state = 2;
try {
String strQuery = "select state from borrowRecord where document=?";
con = ds.getConnection();
ps = con.prepareStatement(strQuery);
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next())
state = rs.getInt("state");
}
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 state;
}
//根据借阅编号获得档案的借阅者
public String getBorrowerById(int borrowId) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String borrower = "";
try {
String strQuery = "select borrower from borrowRecord where id=?";
con = ds.getConnection();
ps = con.prepareStatement(strQuery);
ps.setInt(1, borrowId);
rs = ps.executeQuery();
if (rs.next())
borrower = rs.getString("borrower");
}
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 borrower;
}
//拒绝借阅档案的请求
public void rejectApplication(String operator,int borrowId)
{
Connection con = null;
PreparedStatement ps = null;
int docId = this.getDocById(borrowId);
DocMan dMan = new DocMan();
String doc = dMan.getDocNameById(docId);
String borrower = this.getBorrowerById(borrowId);
try {
String strDelete = "delete from borrowRecord where id=?";
con = ds.getConnection();
ps = con.prepareStatement(strDelete);
ps.setInt(1, borrowId);
ps.executeUpdate();
log.addMsg(operator, borrower, "您借阅的" + doc + "申请已被拒绝", 1);
}
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 approveBorrow(String operator,int borrowId)
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs=null;
int docId=this.getDocById(borrowId);
DocMan dMan=new DocMan();
String doc=dMan.getDocNameById(docId);
String borrower=this.getBorrowerById(borrowId);
try{
//批准该申请
String strUpdate = "update borrowRecord set state=1,lender=? where id=?";
con = ds.getConnection();
ps = con.prepareStatement(strUpdate);
ps.setString(1,operator);
ps.setInt(2, borrowId);
ps.executeUpdate();
log.addMsg(operator,borrower,"您申请借阅的"+doc+"已被批准",1);
//拒绝其它申请该档案的申请
String strQuery="select id from borrowRecord where document=? and state=0 and id!=?";
ps=con.prepareStatement(strQuery);
ps.setInt(1,docId);
ps.setInt(2,borrowId);
rs=ps.executeQuery();
while(rs.next())
this.rejectApplication(operator,rs.getInt("id"));
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (rs != n
上一篇:
BorrowInfo.java
下一篇:
ASP小区停车管理系统(Access)(含录像)