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

📄 docman.java

📁 本例利用java和jsp实现电子政务系统中涉及到的档案管理系统。涉及到javabean和jsp的使用技巧。
💻 JAVA
字号:
package com.csbook.documentsystem;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;

public class DocMan{
    Context ctx=null;
    DataSource ds=null;
    SysLog log=null;
    ChangeEncoding ce=null;
//构造函数
    public DocMan()
    {
      //从连接池中获取数据库连接
      try{
      ctx=new InitialContext();
      ds=(DataSource)ctx.lookup("documents");
     }
     catch(NamingException e){
     e.printStackTrace();
    }
    log=new SysLog();
    ce=new ChangeEncoding();
    }

    //获得制定编号的档案的信息
    //id为档案的编号
    public Hashtable getDocInfo(int id)
        {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs=null;
        Hashtable ht=new Hashtable();
        try{
          String strQuery = "select * from documents where id=?";
          con=ds.getConnection();
          ps = con.prepareStatement(strQuery);
          ps.setInt(1, id);
          rs = ps.executeQuery(strQuery);
          String temp;
          if(rs.next()){
          ht.put("name",ce.changeCharset(rs.getString("name")));
          temp=rs.getString("dept");
          if(temp!=null)      ht.put("dept",ce.changeCharset(temp));
            else ht.put("dept"," ");
          ht.put("docyear",rs.getString("docyear"));
          Integer timeli=new Integer(rs.getInt("timelimit"));
          ht.put("timelimit",timeli.toString());
          temp=rs.getString("description");
          if(temp!=null)
          ht.put("desc",ce.changeCharset(temp));
          else ht.put("desc"," ");
          temp=rs.getString("docBase");
          if(temp!=null)
          ht.put("docBase",ce.changeCharset(rs.getString("docBase")));
          else
            ht.put("docBase"," ");
          }
        }
        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 ht;
       }

    //从档案编号获得档案的名称
    //id为档案编号
     public String getDocNameById(int id)
        {
          Connection con = null;
          PreparedStatement ps = null;
          ResultSet rs=null;
          String doc="";
          try {
            String strQuery = "select name from documents where id=?";
            con=ds.getConnection();
            ps = con.prepareStatement(strQuery);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            if(rs.next())
              doc=ce.changeCharset(rs.getString("name"));
          }
        catch(SQLException e){
          e.printStackTrace();
         }
         finally{
              if (ps != null)  try {ps.close();}
                 catch (SQLException ignore) {}
              if (con != null)   try {con.close();}
                 catch (SQLException ignore) {}
        }
        return doc;
       }

      //根据输入信息检索档案信息
      public ArrayList getDocList(int attr,String name,String strDept,int intDocyear,String strBuilder,String docBase)
      {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        ArrayList docList=new ArrayList();
        try {
          int i = 1;
          String strQuery="select * from documents where attr=?";
          if (!strDept.equals(""))
            strQuery+=" and dept=?";
          if (intDocyear != 0)
            strQuery+=" and docyear=?";
          if (!strBuilder.equals(""))
            strQuery+=" and builder=?";
          if (!docBase.equals(""))
            strQuery+=" and docBase=?";
          if (!name.equals(""))
              strQuery+=" and name like ?";
          con=ds.getConnection();
          ps = con.prepareStatement(strQuery);
          ps.setInt(i++, attr);
          if (!strDept.equals(""))
            ps.setString(i++, strDept);
          if (intDocyear !=0)
            ps.setInt(i++, intDocyear);
          if (!strBuilder.equals(""))
            ps.setString(i++, strBuilder);
          if (!docBase.equals(""))
            ps.setString(i++,ce.changeCharset(docBase));
          if(!name.equals(""))
            ps.setString(i++,'%'+ce.changeCharset(name)+'%');
          rs = ps.executeQuery();
          String temp;
          while(rs.next()){
             temp=rs.getInt("id")+" "+ce.changeCharset(rs.getString("name"))+" "+ce.changeCharset(rs.getString("docBase"))+" "+ce.changeCharset(rs.getString("dept"))+" "+rs.getInt("docyear");
             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;
      }

       //添加档案
      public void addDoc(String name,String dept,String docyear,int timelimit,String docBase,String description,String builder)
        {
         Connection con = null;
         PreparedStatement ps = null;
         try{
           String strInsert = "insert into documents(name,dept,docyear,timelimit,attr,docBase,description,builder,buildtime) values(?,?,?,?,?,?,?,?,?)";
           con=ds.getConnection();
           ps = con.prepareStatement(strInsert);
           ps.setString(1,ce.changeCharset(name));
           ps.setString(2,ce.changeCharset(dept));
           ps.setString(3,ce.changeCharset(docyear));
           ps.setInt(4, timelimit);
           ps.setInt(5,0);
           ps.setString(6,ce.changeCharset(docBase));
           if(description==null||description.equals(""))
             ps.setString(7,description);
          else
             ps.setString(7,ce.changeCharset(description));
           ps.setString(8,builder);
           java.sql.Date currentTime=new java.sql.Date(System.currentTimeMillis());
           ps.setDate(9,currentTime);
           ps.executeUpdate();
           log.addLog(builder,"add document:"+name,"document");
          }
          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 updateDoc(String operator,int intId,String dept,String docyear,int timelimit,String desc)
        {
        Connection con = null;
        PreparedStatement ps = null;
        try{
          String strUpdate="update documents set dept=?,docyear=?, timelimit=?,description=? where id=?";
          con=ds.getConnection();
          ps = con.prepareStatement(strUpdate);
          ps.setString(1, dept);
          ps.setString(2, docyear);
          ps.setInt(3, timelimit);
          if(desc==null||desc.equals(""))
               ps.setString(4, desc);
          else
               ps.setString(4,ce.changeCharset(desc));
          ps.setInt(5,intId);
          ps.executeUpdate();
          log.addLog(operator,"update document:" +intId,"documents");
        }
        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 removeDoc(String operator,int id)
       {
        Connection con = null;
        PreparedStatement ps = null;
        try{
          String strDelete = "delete from documents where id=?";
          con=ds.getConnection();
          ps = con.prepareStatement(strDelete);
          ps.setInt(1, id);
          ps.executeUpdate();
          log.addLog(operator,"remove document:"+id,"documents");
        }
        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 setDocBase(String operator,int id,String docBase)
        {
        Connection con = null;
        PreparedStatement ps = null;
        try{
          String strUpdate = "update documents set docBase=? where id=?";
          con=ds.getConnection();
          ps = con.prepareStatement(strUpdate);
          ps.setString(1,ce.changeCharset(docBase));
          ps.setInt(2,id);
          ps.executeUpdate();
          log.addLog(operator,"update document:" +id+" set docBase="+ce.changeCharset(docBase),"documents");
        }
        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 setDocAttr(String operator,int id,int intAttr)
       {
        Connection con = null;
        PreparedStatement ps = null;
        try{
          String strUpdate = "update documents set attr=? where id=?";
          con=ds.getConnection();
          ps = con.prepareStatement(strUpdate);
          ps.setInt(1, intAttr);
          ps.setInt(2, id);
          ps.executeUpdate();
          log.addLog(operator,"update document:" +id+" set attr="+intAttr,"documents");
        }
        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 int getAttr(int id)
     {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs=null;
        int attr=0;
        try{
          String strUpdate = "select attr from documents where id=?";
          con=ds.getConnection();
          ps = con.prepareStatement(strUpdate);
          ps.setInt(1,id);
          rs=ps.executeQuery();
          if(rs.next())
             attr=rs.getInt("attr");
        }
        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 attr;
    }

    //从档案编号获得档案存储的档案库
   public String getDocBaseById(int id)
    {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs=null;
        String docBase="";
            try{
          String strQuery = "select docBase from documents where id=?";
          con=ds.getConnection();
          ps = con.prepareStatement(strQuery);
          ps.setInt(1,id);
          rs=ps.executeQuery();
          if(rs.next())
             docBase=rs.getString("docBase");
       }
       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 docBase;
      }

   //在删除机构前,把该机构的所有档案转到其他机构
    public void changeDocDept(String oldDept,String newDept)
    {
      Connection con = null;
      PreparedStatement ps = null;
      try{
        String strUpdate = "update documents set dept=? where dept=?";
        con=ds.getConnection();
        ps = con.prepareStatement(strUpdate);
        ps.setString(1,ce.changeCharset(newDept));
        ps.setString(2,ce.changeCharset(oldDept));
        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 void setDeptDocOut(String dept)
    {
      Connection con = null;
      PreparedStatement ps = null;
      try{
       String strUpdate = "update documents set attr=0,dept='deleted' where dept=?";
       con=ds.getConnection();
       ps = con.prepareStatement(strUpdate);
       ps.setString(1,ce.changeCharset(dept));
       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) {}
       }
    }
}

⌨️ 快捷键说明

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