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

📄 borrowman.java

📁 本例利用java和jsp实现电子政务系统中涉及到的档案管理系统。涉及到javabean和jsp的使用技巧。
💻 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 != 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) {}
          }
        }

 //归还档案
  public void returnDoc(String operator, int id) {
    Connection con = null;
    PreparedStatement ps = null;
    try {
      String borrower = getBorrowerById(id);
      DocMan dMan=new DocMan();
      String doc = dMan.getDocNameById(id);
      log.addMsg(operator, borrower, "您所申请借阅的" + doc + "已归还", 1);
      String strUpdate = "delete from borrowRecord where id=?";
      con=ds.getConnection();
      ps = con.prepareStatement(strUpdate);
      ps.setInt(1, id);
      ps.executeUpdate();
    }
    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 ArrayList getBorrowInfo(String user, int state) {
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    ArrayList documents=new ArrayList();
    try {
      String strQuery ="select document from borrowRecord where state=? and borrower=?";
      con = ds.getConnection();
      ps = con.prepareStatement(strQuery);
      ps.setInt(1, state);
      ps.setString(2, user);
      rs = ps.executeQuery();
      while(rs.next())
        documents.add(rs.getString("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 documents;
  }

 //获取指定借阅状态的借阅信息
  public ArrayList getDocList(int state) {
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    ArrayList docList = new ArrayList();
    try {
      int i = 1;
      String strQuery = "select * from borrowRecord where state=?";
      con=ds.getConnection();
      ps = con.prepareStatement(strQuery);
      ps.setInt(1, state);
      rs = ps.executeQuery();
      String temp;
      while (rs.next()) {
        temp =rs.getInt("id")+" "+rs.getInt("document")+" "+rs.getString("borrower");
        docList.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 docList;
  }
}


⌨️ 快捷键说明

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