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

📄 personfilebean.java

📁 “JSP数据库项目案例导航”一书从第一章到第十一章各章实例的源程序文件以及数据库文件。 注意: 1. 本书中的案例提供的数据库环境不同
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

package oa.bean;

import java.text.*;
import java.util.*;
import java.io.*;
import java.sql.*;
import oa.main.*;
import javax.swing.tree.*;

public class PersonFileBean extends ParentBean {

	//错误代码定义
	final public static int err_DIRNotFound = -1;
	final public static int err_FileNotFound = -2;
	final public static int err_SuchNameHasBeenUsed = -3;
	final public static int err_delTreeError = -4;
	//错误代码定义完

	//上传文件下载时的cache路径(只能为Tomcat root下的一个路径)
	final public static String strDownCachePath = oa.main.ParentBean.FilesPath+"cabinet\\downcache\\";
	//"c:/Tomcat 4.1/webapps/ROOT/oa/person/upload/downcache/";
	//上传文件保存的路径(可以为服务器的任意路径)
	final public static String strFileStorePath = oa.main.ParentBean.FilesPath+"cabinet\\personfiles\\";
	//"c:/Tomcat 4.1/webapps/ROOT/oa/person/upload/";

	public static void main(String args[])  {
		PersonFileBean myBean = new PersonFileBean();
		System.out.println("\n");
		System.out.println(myBean.getFileIMG("doc"));

	}

	private DefaultMutableTreeNode currentNode; //定义为当前的节点,界面显示的是这一个节点的所有子节点
	private int nFileID = -1; //文件唯一编号
	private int nParentID = -1; //父文件编号
	private String strPersonID = ""; //职工唯一编号
	int nNum;						
	String strNum;
	
	//当DownCache中的文件大于50的时候,清空DownCache
	public int ClearDownCache(){
		String strDownCacheFileName=strDownCachePath.substring(0,strDownCachePath.length());
		File f = new File(strDownCacheFileName);
		File[] fs = f.listFiles();
		int nSum=0;
		if(fs.length<50) return -1;
		for (int i = 0; i < fs.length; i++) {
			//System.out.println(fs[i].getName());
			if (fs[i].exists()) {
			  try{
				if (fs[i].delete()) nSum++;
			  }catch (Exception e){}
			}
		}
		return nSum;
	}

	public PersonFileBean()  {
		super();
		currentNode = null;
		strPersonID = "";
		nFileID = -1;
		// TODO Auto-generated constructor stub
	}

	//增加目录
	public int addDir(String strNewDirName, String strParentID) {
		int intMax = db.makeID("ZZ_GRWJB", "ZGBH", "WJBH", strPersonID, false);

		Vector vect = new Vector();
		vect.add("ZZ_GRWJB");
		vect.add(addVector("ZGBH", strPersonID, "CHAR"));
		vect.add(addVector("WJBH", String.valueOf(intMax), "NUM"));
		vect.add(addVector("WJMC", strNewDirName, "CHAR"));
		vect.add(addVector("WJDX", "0", "NUM"));
		vect.add(addVector("WJLB", "D", "CHAR"));
		vect.add(addVector("FMLBH", strParentID, "NUM"));

		return insertRecord(vect);

	}

	//增加文件
	public int addFile(
		String strFileName,
		String strFileExt,
		String strFileSize,
		String strFileStorePath) {
		int intMax = db.makeID("ZZ_GRWJB", "ZGBH", "WJBH", strPersonID, false);
		strFileName = ds.toString(strFileName);
		strFileExt = ds.toString(strFileExt);
		strFileSize = ds.toString(strFileSize);
		strFileStorePath = ds.toString(strFileStorePath);

		Vector vect = new Vector();
		vect.add("ZZ_GRWJB");
		vect.add(addVector("ZGBH", strPersonID, "CHAR"));
		vect.add(addVector("WJBH", Integer.toString(intMax), "NUM"));
		vect.add(addVector("WJMC", strFileName, "CHAR"));
		vect.add(addVector("WJDX", strFileSize, "NUM"));
		vect.add(addVector("WJLJ", strFileStorePath, "CHAR"));
		vect.add(addVector("WJLB", "F", "CHAR"));
		vect.add(addVector("FMLBH", Integer.toString(nParentID), "NUM"));
		if (!strFileExt.equals("")) {
			vect.add(addVector("WJKZM", strFileExt, "CHAR"));
		}
		return insertRecord(vect);
	}

	/**
	 * 
	 */
	//生成根节点
	public int addRoot()  {
		int intMax = 0;

		Vector vect = new Vector();
		vect.add("ZZ_GRWJB");
		vect.add(addVector("ZGBH", strPersonID, "CHAR"));
		vect.add(addVector("WJBH", "0", "NUM"));
		vect.add(addVector("WJMC", "根目录", "CHAR"));
		vect.add(addVector("WJDX", "0", "NUM"));
		vect.add(addVector("WJLB", "D", "CHAR"));
		vect.add(addVector("FMLBH", "-1", "NUM"));

		return insertRecord(vect);
	}

	//生成一层的树  
	public DefaultMutableTreeNode build1LevTree(){

		String sql =
			"select ZGBH,WJBH,WJMC,WJDX,WJLJ,WJLB,FMLBH,WJKZM from ZZ_GRWJB "
				+ " where zgbh='"
				+ strPersonID
				+ "'"
				+ " and WJBH="
				+ nParentID
				+ " order by WJLB,WJMC";
		ResultSet rs = db.QuerySQL(sql);
		Hashtable hash = new Hashtable();
		Statement stmt = null;
		ResultSetMetaData rsmd = null;
		int cols = 0;
		try{
		rsmd = rs.getMetaData();
		//结果集为空时返回
		if (!(rs.next())) {
			return null;
		}
		cols = rsmd.getColumnCount();
		hash.clear();
		for (int i = 1; i <= cols; i++) {
			String field = ds.toString(rsmd.getColumnName(i));
			String value = ds.toString(rs.getString(i));
			hash.put(field, value);
		}
		}catch(Exception e){System.out.println("build1LevTree()1运行时出错:"+e);}
		finally{
			if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("关闭记录集rs时出错"+e);}
			if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("关闭声明时statement出错"+e);}		
		}
		DefaultMutableTreeNode root = new DefaultMutableTreeNode(hash);

		//选出root的子节点
		String strParentNo =
			(String) ((Hashtable) root.getUserObject()).get("WJBH");

		sql =
			" Select ZGBH,WJBH,WJMC,WJDX,WJLJ,WJLB,FMLBH,WJKZM from ZZ_GRWJB "
				+ " Where FMLBH="
				+ strParentNo
				+ "  and ZGBH='"
				+ strPersonID
				+ "'"
				+ "   order by WJLB,WJMC ";
System.out.println(sql);
		rs = db.QuerySQL(sql);
		try{
		rsmd = rs.getMetaData();
		cols = rsmd.getColumnCount();
		while (rs.next()) {
			hash = new Hashtable();
			hash.clear();
			for (int i = 1; i <= cols; i++) {
				String field = ds.toString(rsmd.getColumnName(i));
				String value = ds.toString(rs.getString(i));
				hash.put(field, value);
			}
			DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(hash);
			root.add(treeNode);
		}
		}catch(Exception e){System.out.println("build1LevTree()2运行时出错:"+e);}
		finally{
			if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("关闭记录集rs时出错"+e);}
			if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("关闭声明时statement出错"+e);}		
		}
		return root;
	}

	//迭代生成树
	private void buildSubTree(DefaultMutableTreeNode rootNode)
	{
		String sql = "";
		String strParentNo = "";
		DefaultMutableTreeNode root = rootNode;
		DefaultMutableTreeNode treeNode;

		//选出rootNode的子节点
		strParentNo =
			(String) ((Hashtable) rootNode.getUserObject()).get("WJBH");
		sql =
			" Select ZGBH,WJBH,WJMC,WJDX,WJLJ,WJLB,FMLBH,WJKZM from ZZ_GRWJB "
				+ " Where FMLBH="
				+ strParentNo
				+ "  and ZGBH='"
				+ strPersonID
				+ "'"
				+ "   order by WJLB,WJMC ";
		ResultSet rs = selectRecord(sql); ///db.QuerySQL
		Statement stmt = null;
		try{
		ResultSetMetaData rsmd = rs.getMetaData();

		int cols = rsmd.getColumnCount();
		while (rs.next()) {
			Hashtable hash = new Hashtable();
			for (int i = 1; i <= cols; i++) {
				String field = ds.toString(rsmd.getColumnName(i));
				String value = ds.toString(rs.getString(i));
				hash.put(field, value);
			}
			treeNode = new DefaultMutableTreeNode(hash);

			root.add(treeNode);
			buildSubTree(treeNode);
		}
		}catch(Exception e){System.out.println("buildSubTree()运行时出错:"+e);}
		finally{
			if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("关闭记录集rs时出错"+e);}
			if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("关闭声明时statement出错"+e);}		
		}
	}

	//生成树并返回根节点
	public DefaultMutableTreeNode buildTree(){
		String sql =
			" Select ZGBH,WJBH,WJMC,WJDX,WJLJ,WJLB,FMLBH,WJKZM from ZZ_GRWJB "
				+ " Where FMLBH = -1"
				+ "   and ZGBH='"
				+ strPersonID
				+ "'"
				+ "   order by WJBH ";
		ResultSet rs = db.QuerySQL(sql);
		Statement stmt = null;
		DefaultMutableTreeNode root = null;
		try{
			ResultSetMetaData rsmd = rs.getMetaData();
			//		结果集为空时返回
			if (!(rs.next())) {
				//System.err.println("数据表错误:根为空。");
				return null;
			}
			int cols = rsmd.getColumnCount();
			Hashtable hash = new Hashtable();
			hash.clear();
			for (int i = 1; i <= cols; i++) {
				String field = ds.toString(rsmd.getColumnName(i));
				String value = ds.toString(rs.getString(i));
				hash.put(field, value);
			}
			root = new DefaultMutableTreeNode(hash);
			buildSubTree(root);
		}catch(Exception e){System.out.println("buildTree()运行时出错:"+e);}
		finally{
			if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("关闭记录集rs时出错"+e);}
			if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("关闭声明时statement出错"+e);}		
		}
		return root;
	}

	//从某一节点做根生成树并返回根节点
	public DefaultMutableTreeNode buildTree(String strFileID)
	{
		String sql =
			" Select ZGBH,WJBH,WJMC,WJDX,WJLJ,WJLB,FMLBH,WJKZM from ZZ_GRWJB "
				+ " Where WJBH = "
				+ strFileID
				+ "   and ZGBH='"
				+ strPersonID
				+ "'"
				+ "   order by WJBH ";
		ResultSet rs = db.QuerySQL(sql);
		Statement stmt = null;
		DefaultMutableTreeNode root = null;
		try{
		ResultSetMetaData rsmd = rs.getMetaData();
		//		结果集为空时返回
		if (!(rs.next())) {
			//System.err.println("数据表错误:根为空。");
			return null;
		}
		int cols = rsmd.getColumnCount();
		Hashtable hash = new Hashtable();
		hash.clear();
		for (int i = 1; i <= cols; i++) {
			String field = ds.toString(rsmd.getColumnName(i));
			String value = ds.toString(rs.getString(i));
			hash.put(field, value);
		}
		root = new DefaultMutableTreeNode(hash);
		buildSubTree(root);
		}catch(Exception e){System.out.println("buildTree()运行时出错:"+e);}
		finally{
			if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("关闭记录集rs时出错"+e);}
			if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("关闭声明时statement出错"+e);}		
		}
		return root;
	}

	//	删除个人文件表数据记录
	public int delFile(){
		String strRootPath = strFileStorePath;
		if (strRootPath.equals(""))
			return -1;
		String sql = "";
		sql =
			"select WJLJ from zz_grwjb where zgbh= '"
				+ strPersonID
				+ "' and wjbh="
				+ nFileID;
		ResultSet rs = db.QuerySQL(sql);
		Statement stmt = null;
		try{
			rs.next();
			String strPath = ds.toString(rs.getString("WJLJ"));
			File f = new File(strRootPath + strPath);
			//System.out.println(strRootPath + strPath);
			if (f.exists()) {
				f.delete();
			}
		}catch(Exception e){System.out.println("delFile()运行时出错:"+e);}
		finally{
			if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("关闭记录集rs时出错"+e);}
			if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("关闭声明时statement出错"+e);}		
		}
		sql =
			"delete from zz_grwjb where zgbh= '"
				+ strPersonID
				+ "' and wjbh="
				+ nFileID;
		return deleteRecord(sql);
	}

	//取得文件名称
	public String getFileName(String zgbh,String fileid)
	{
		String filename = "";
		String sql = " select WJMC from zz_grwjb where zgbh= '"+ zgbh+ "' and wjbh="+ fileid; 
		Statement stmt = null;
		ResultSet rs = selectRecord(sql);
		try{
			if(rs.next())
			{
				filename = ds.toString(rs.getString(1));
			}
		}catch(Exception e){System.out.println("getFileName()运行时出错:"+e);}
		finally{
			if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("关闭记录集rs时出错"+e);}
			if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("关闭声明时statement出错"+e);}		
		}
		return filename;
	}

	private int delSubFile(String strFileID)
	{
		String strRootPath = strFileStorePath;
		String sql = "";
		sql =
			"select WJLJ from zz_grwjb where zgbh= '"
				+ strPersonID
				+ "' and wjbh="
				+ strFileID;
		ResultSet rs = db.QuerySQL(sql);
		Statement stmt = null;
		try{
		rs.next();
		String strPath = ds.toString(rs.getString("WJLJ"));
		File f = new File(strRootPath + strPath);
		//System.out.println(strRootPath + strPath);
		if (f.exists()) {
			f.delete();
		}
		}catch(Exception e){System.out.println("delSubFile()运行时出错:"+e);}
		finally{
			if(rs!=null)try{ stmt = rs.getStatement(); rs.close();}catch(Exception e){System.out.println("关闭记录集rs时出错"+e);}
			if(stmt!=null) try{stmt.close();}catch(Exception e){System.out.println("关闭声明时statement出错"+e);}		
		}

⌨️ 快捷键说明

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