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

📄 database.java

📁 这是一个用jsp+Oracle开发的联系人客户关系管理系统!
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/**********************************************************
Copyright (C),2003-10-12, Beijing  USTB.
All rights reserved.
Filename: Datebase.java
Author:    
Version 1.0
Date:2003-10-12
Description:此文件为数据库的基本操作处理类
Other:
Variable List:
1.Connection conn = null;//数据库连接对象

Function List:
//基本操作
1.public DataBase()//构造函数
2.public void getConnPool()//取得连接池
3.public void createConn(String url,String usr,String pwd)//生成Oracle SQLServer等的连接
4.public void createConn(String url)//生成Access连接
5.public void releaseConn()//释放数据库连接
6.public ResultSet QuerySQL(String sql)//查询记录
7.public int ExecuteSQL(String sql)//执行增删改的语句

//辅助使用函数
8.public int makeID(String table,String field1,String field2,String value1,boolean type1)//产生唯一编号,type1为false时表示条件字段为字符串类型
  public int makeID(String table,String field1,String field2,String field3,String value1,String value2,boolean type1,boolean type2)
  public int makeID_Add1(String table,String field1,String field2,String value1,boolean type1)//产生唯一编号,type1为false时表示条件字段为字符串类型
  public int makeID_Add1(String table,String field1,String field2,String field3,String value1,String value2,boolean type1,boolean type2)
9.public int toID(String table,String field1,String field2,String value1)//将名称转换为编号

10.public String toName(String table,String field1,String field2,String value)//将编号转换为名称
11.public Vector getOnePage(String sql,int page,int records)//分页时取得一页的数据量
12.public int setSort(String table,String field1,String field2,String wherestr,String orderstr,boolean b)//为某一个字段进行重新排序

//查询与更新LOB类型
13.public String QueryCLOB(String table,String wherestr,String clobfield)//查询CLOB类型值
14.public int UpdateCLOB(String table,String wherestr,String blobfield,String blobvalue)//修改CLOB类型值
15.public String QueryBLOB(String table,String wherestr,String blobfield)//查询BLOB类型值
16.public int UpdateBLOB(String table,String wherestr,String blobfield,String blobvalue)//修改BLOB类型值
17.public String QueryBLOB_JNDI(String table,String wherestr,String blobfield)//查询BLOB类型值
178.public int UpdateBLOB_JNDI(String table,String wherestr,String blobfield,String blobvalue)//修改BLOB类型值


//LOB类型基本操作(到文件)
19.public int clobInsert(String sql,String table,String wherestr,String clobfield,String infile)//往数据库中插入一个新的CLOB对象
20.public int clobModify(String table,String wherestr,String clobfield,String infile)//修改CLOB对象(是在原CLOB对象基础上进行覆盖式的修改)
21.public int clobReplace(String table,String wherestr,String clobfield,String infile)//替换CLOB对象(将原CLOB对象清除,换成一个全新的CLOB对象)
22.public int blobInsert(String sql,String table,String wherestr,String blobfield,String infile)//往数据库中插入一个新的BLOB对象
23.public int blobModify(String table,String wherestr,String blobfield,String infile)//修改BLOB对象(是在原BLOB对象基础上进行覆盖式的修改)
24.public int blobReplace(String table,String wherestr,String blobfield,String infile)//替换BLOB对象(将原BLOB对象清除,换成一个全新的BLOB对象)

//数据库结构信息
25.public Hashtable getDataBaseInfo()//数据库信息
26;public Vector getTableList()//数据表列表
27.public Vector getTableStruct(String table)//数据表的结构
28.public Vector getResultSetData(ResultSet rs)//取得数据集内容

//对sql语句的分布处理
29.public void prepareStatement(String sql)//创建申明对象
30.public void executeQuery()//执行查询
31.public boolean next()//转向下一条
32.public String getObject(String field,String sqlType)//取得数据并根据数据类型转化为字符串
33.public void setObject(int index,String value,String sqlType)//根据数据类型保存到数据库
34.public void executeUpdate()//执行更新
35.public void closePstm()//关闭申明对象
36.public void closeRs()//关闭游标

37.public Vector getData(String sql);

History:
date:2003-11-21
name:liumei
action:修改getOnePage(String sql,int page,int records)//分页时取得一页的数据量
	   每页的记录显示条数从字典中取,即去掉records的作用
***********************************************************/
package com.db;

import java.util.*;
import java.text.*;
import java.sql.*;
import java.io.*;
import java.lang.*;
import javax.naming.*;
import oracle.jdbc.driver.OracleResultSet;
import oracle.sql.*;
import com.parser.Configuration;
import com.util.*;

/**
 * 此文件为数据库的基本操作处理类
 * @author 
 * @version 1.0-hg
*/
public class DataBase extends Object
{
	Connection conn = null;
	PreparedStatement pstm = null;
	ResultSet rs = null;

	boolean connected = false;

	/**构造函数*/
	public DataBase()
	{
	}

	/**取得连接池*/
	public void getConnPool()
	{
		try
		{
			if(connected){
				throw new SQLException("数据库已连接,无须重连!");
			}
			Context initCtx = new InitialContext(); 
			Context ctx = (Context) initCtx.lookup("java:comp/env"); 

			/**获取连接池对象*/
			Object obj = (Object) ctx.lookup("jdbc/OracleDB"); 

			/**类型转换*/
			javax.sql.DataSource ds = (javax.sql.DataSource)obj; 
			conn = ds.getConnection();

			/**测试连接计数*/
			connected = true;
			ParentBean.showConnNUM(true);
		}
		catch(NamingException e){System.out.println("从数据池取得数据库连接时出错;\r\n错误为:"+e);}
		catch(SQLException e){System.out.println("从数据池取得数据库连接时出错;\r\n错误为:"+e);}
	}

	/**创建我的连接池*/
	public boolean getMyConnPool()
	{
		conn = Configuration.connMgr.getConnection(Configuration.ConnectionPoolName);
		ParentBean.showConnNUM(true);
		if(conn == null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	/**释放我的连接池*/
	public boolean releaseMyConnPool()
	{
		Configuration.connMgr.freeConnection(Configuration.ConnectionPoolName, conn);
		ParentBean.showConnNUM(false);
		return true;
	}

/*
	public void getConnDataSourse()
	{
		try
		{

			OracleDataSource ods = new OracleDataSource();
			ods.setDriverType("thin");
			ods.setServerName("10.0.0.94");
			ods.setNetworkProtocol("tcp");
			ods.setDatabaseName("hg");
			ods.setPortNumber(1521);
			ods.setUser("oaadminuser");
			ods.setPassword("lancom4454");

			// 从数据源中获取数据库连接
			conn = ods.getConnection();

			/**测试连接计数
			ParentBean.curnum++;
			ParentBean.sumnum++;
		}
		catch(SQLException e){System.out.println("从自身建立数据库连接时出错;\r\n错误为:"+e);}
	}

	public void getConnJNDI()
	{
		Context ctx = null;
		try
		{
			//建立上下文环境
			Hashtable env = new Hashtable (5);
			env.put (Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.fscontext.RefFSContextFactory");
			env.put (Context.PROVIDER_URL, "file:JNDI");
			ctx = new InitialContext(env);

			//从JNDI取得连接
			OracleDataSource ods1 = (OracleDataSource) ctx.lookup ("jdbc/chidb");

			if(ods1==null)
			{
				

				//建立数据源绑定
				OracleDataSource ods = new OracleDataSource();
				ods.setDriverType("thin");
				ods.setServerName("10.0.0.94");
				ods.setNetworkProtocol("tcp");
				ods.setDatabaseName("hg");
				ods.setPortNumber(1521);
				ods.setUser("oaadminuser");
				ods.setPassword("lancom4454");
				ctx.bind ("jdbc/chidb",ods);
			}

			ods1 = (OracleDataSource) ctx.lookup ("jdbc/chidb");
			conn = ods1.getConnection();

		}
		catch(SQLException e){System.out.println("从自身建立数据库连接时出错;\r\n错误为:"+e);}
		catch (NamingException ne)
		{
			ne.printStackTrace();
		}
	}
*/

	/**生成Oracle SQLServer等的连接*/
	public void createConn(String drv,String url,String usr,String pwd)
	{
		try
		{
			if(connected){
				throw new SQLException("数据库已连接,无须重连!");
			}
			Class.forName(drv).newInstance();
			conn = DriverManager.getConnection(url,usr,pwd);

			connected = true;

			/**测试连接计数*/
			ParentBean.showConnNUM(true);
		}
		catch(ClassNotFoundException ec){System.out.println("从自身建立数据库连接时出错;\r\n错误为:"+ec);}
		catch(SQLException e){System.out.println("从自身建立数据库连接时出错;\r\n错误为:"+e);}
		catch(Exception et){System.out.println("从自身建立数据库连接时出错;\r\n错误为:"+et);}
	}

	/**生成Access连接*/
	public void createConn(String drv,String url)
	{
		try
		{
			if(connected){
				throw new Exception("数据库已连接,无须重连!");
			}
			Class.forName(drv).newInstance();
			conn = DriverManager.getConnection(url);
			connected = true;

			/**测试连接计数*/
			ParentBean.showConnNUM(true);
		}
		catch(ClassNotFoundException ec){System.out.println("从自身建立数据库连接时出错;\r\n错误为:"+ec);}
		catch(SQLException e){System.out.println("从自身建立数据库连接时出错;\r\n错误为:"+e);}
		catch(Exception et){System.out.println("从自身建立数据库连接时出错;\r\n错误为:"+et);}
	}

	/**释放数据库连接*/
	public void releaseConn()
	{
		try
		{
			if(!connected){
				throw new SQLException("数据库未连接!");
			}
			if(conn!=null)
			{
				conn.close();
				connected = false;

				/**测试连接计数*/
				ParentBean.showConnNUM(false);
			}
		}
		catch(SQLException e){System.out.println("关闭数据库连接时出错;\r\n错误为:"+e);}
	}	

	/**查询记录*/
	public ResultSet QuerySQL(String sql)
	{
		ResultSet rs = null;
		try
		{
			pstm = conn.prepareStatement(sql);
			rs = pstm.executeQuery();
		}catch(SQLException sqle){
			System.out.println("执行DataBase::QuerySQL(String)调用SQL语句 "+sql+" 时出错;\r\n错误为:"+sqle);
			if(pstm!=null)
			try{pstm.close();}catch(Exception e){System.out.println("执行DataBase::QuerySQL(String)试图关闭错误的声明时出错;\r\n错误为:"+e);}
		}
		return rs;
	}

	/**执行增删改的语句*/
	public int ExecuteSQL(String sql)
	{
		try
		{
			pstm = conn.prepareStatement(sql);
			pstm.executeUpdate();
			//conn.commit();
		}
		catch(SQLException sqle)
		{
			System.out.println("执行DataBase::ExecuteSQL(String)调用SQL语句 "+sql+" 时出错;\r\n错误为:"+sqle);
			return sqle.getErrorCode();
		}
		finally{
			try{
			pstm.close();
			}catch(SQLException sqle){System.out.println("执行DataBase::ExecuteSQL(String)调用SQL语句 "+sql+" 时出错;\r\n错误为:"+sqle);}
		}
		return 0;
	}

	/**产生唯一编号*/
 	public int makeID(String table,String field1,String field2,String value1,boolean type1)
	{
		int out = -1;
		String sql = "";
		try
		{
					//只有唯一主键field1
			sql = "select "+field1+" as ID from "+table+" order by "+field1;
					//有两个主键field1、field2
			if(!value1.equals(""))//当第一个字段不空时,作为条件查询第二个字段
			{
				sql = "select "+field2+" as ID from "+table+" where "+field1+"="+value1+" order by "+field2;
				if(!type1)       //是字符串时 将type1设为false
					sql = "select "+field2+" as ID from "+table+" where "+field1+"='"+value1+"' order by "+field2;
			}
			pstm = conn.prepareStatement(sql);
			rs = pstm.executeQuery();
			int t1 = 1;
			int t2 = 2;
			if(rs.next())  //有记录
			{
				t1 = rs.getInt("ID");
				out = t1;
				boolean bool = false;
				while(rs.next())   //不止一条纪录
				{
					bool = true;
					t2 = rs.getInt("ID");
					if((t2-t1)>1)break;  //如果t2与t1相差大于1,则跳出去,新编号为t1++(见后面**)
					t1 = t2;			//否则将t2赋给t1
				}
				if(!bool)  //如果只有一条纪录
				{
					if(t1>1)t1 = 1;  //如果已有纪录的ID号大于1,则新编号设为1
					else t1++;
				}
				else t1++;		//**
			}
			if(out>1)out = 1;
			else out = t1;

		}catch(SQLException sqle){System.out.println("执行DataBase::makeID(String table,String field1,String field2,String value1,boolean type1)调用SQL语句 "+sql+" 时出错;\r\n错误为:"+sqle);}
		finally{
			if(rs!=null){
				try{
				rs.close();
				}catch(SQLException e){System.out.println("执行DataBase::makeID(String table,String field1,String field2,String value1,boolean type1)调用SQL语句 "+sql+" 时出错;\r\n错误为:"+e);}
			}
			if(pstm!=null){
				try{
				pstm.close();
				}catch(SQLException e){System.out.println("执行DataBase::makeID(String table,String field1,String field2,String value1,boolean type1)调用SQL语句 "+sql+" 时出错;\r\n错误为:"+e);}
			}
		}
		return out;
	}

	/**产生唯一编号*/
 	public int makeID_Add1(String table,String field1,String field2,String value1,boolean type1)
	{
		int out = -1;
		String sql = "";
		try
		{
					//只有唯一主键field1
			sql = "select max("+field1+")+1 as ID from "+table+" order by "+field1;
					//有两个主键field1、field2
			if(!value1.equals(""))//当第一个字段不空时,作为条件查询第二个字段
			{
				sql = "select ("+field2+")+1 as ID from "+table+" where "+field1+"="+value1+" order by "+field2;
				if(!type1)       //是字符串时 将type1设为false
					sql = "select ("+field2+")+1 as ID from "+table+" where "+field1+"='"+value1+"' order by "+field2;
			}
			pstm = conn.prepareStatement(sql);
			rs = pstm.executeQuery();
			if(rs.next())  //有记录
			{
				out = rs.getInt(1);
			}
		}catch(SQLException sqle){System.out.println("执行DataBase::makeID_Add1(String table,String field1,String field2,String value1,boolean type1)调用SQL语句 "+sql+" 时出错;\r\n错误为:"+sqle);}
		finally{
			if(rs!=null){
				try{
				rs.close();
				}catch(SQLException e){System.out.println("执行DataBase::makeID_Add1(String table,String field1,String field2,String value1,boolean type1)调用SQL语句 "+sql+" 时出错;\r\n错误为:"+e);}
			}
			if(pstm!=null){
				try{
				pstm.close();
				}catch(SQLException e){System.out.println("执行DataBase::makeID_Add1(String table,String field1,String field2,String value1,boolean type1)调用SQL语句 "+sql+" 时出错;\r\n错误为:"+e);}
			}
		}
		return out;
	}

 	public int makeID(String table,String field1,String field2,String field3,String value1,String value2,boolean type1,boolean type2)
	{
		int out = -1;
		String sql = "";
		try
		{
					//只有唯一主键field1
			sql = "select "+field1+" as ID from "+table+" order by "+field1;
					//有两个主键field1、field2
			if(!value1.equals(""))//当第一个字段不空时,作为条件查询第二个字段
			{
				sql = "select "+field2+" as ID from "+table+" where "+field1+"="+value1+" order by "+field2;
				if(!type1)       //是字符串时 将type1设为false

⌨️ 快捷键说明

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