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

📄 database.java

📁 Jsp作的新闻信息发布系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

package oa.main;

import java.util.*;
import java.text.*;
import java.sql.*;
import java.io.*;
import java.lang.*;
import javax.naming.*;


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

	boolean connected = false;

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

	public void showConnNUM()
	{
		//连接池中使用
		DealString ds = new DealString();
		int curnum = Configuration.connMgr.getCurConns(Configuration.ConnectionPoolName);
		int sumnum = Configuration.connMgr.getSumConns(Configuration.ConnectionPoolName);
		int maxnum = Configuration.connMgr.getMaxConns(Configuration.ConnectionPoolName);
		int isNullPool = Configuration.connMgr.getNullPool(Configuration.ConnectionPoolName);
		int isOracleErr = Configuration.connMgr.getOracleErr(Configuration.ConnectionPoolName);
		int jvmcount = ParentBean.count;
		float jvm1 = (float)Runtime.getRuntime().totalMemory()/1024/1024;
		float jvm2 = (float)Runtime.getRuntime().maxMemory()/1024/1024;
		System.out.print("\r" + ds.getDateTime ( ).substring ( 11 , 19 ) + "  连接累计> "+ds.toLengthStrRight(sumnum+"",10)+"当前连接> "+ds.toLengthStrRight(curnum+"",10)+"池中连接> "+ds.toLengthStrRight(maxnum+"",10)+"池空未连接> " +ds.toLengthStrRight(isNullPool+"",10)+"DB未响应> " +ds.toLengthStrRight(isOracleErr+"",10)+"JVM垃圾回收> "+jvmcount+"  ");
	}

	/**取得连接池*/
	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;
			showConnNUM();
		}
		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);
		showConnNUM();
		if(conn == null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

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

/*
	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;

			/**测试连接计数*/
			showConnNUM();
		}
		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;

			/**测试连接计数*/
			showConnNUM();
		}
		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;

				/**测试连接计数*/
				showConnNUM();
			}
		}
		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
					sql = "select "+field2+" as ID from "+table+" where "+field1+"='"+value1+"' order by "+field2;
			}
			if(!value2.equals(""))//当第二个字段不空时,作为条件查询第三个字段
			{
				sql = "select "+field3+" as ID from "+table+" where "+field1+"="+value1+" and "+field2+"="+value2+" order by "+field3;
				if(!type2)       //是字符串时 将type1设为false
					sql = "select "+field3+" as ID from "+table+" where "+field1+"='"+value1+"' and "+field2+"='"+value2+"' order by "+field3;
			}
			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 field3,String value1,String value2,boolean type1,boolean type2)调用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 field3,String value1,String value2,boolean type1,boolean type2)调用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 field3,String value1,String value2,boolean type1,boolean type2)调用SQL语句 "+sql+" 时出错;\r\n错误为:"+e);}
			}
		}
		return out;
	}

	public int makeID_Add1(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 max("+field1+") as ID from "+table+" order by "+field1;
					//有两个主键field1、field2
			if(!value1.equals(""))//当第一个字段不空时,作为条件查询第二个字段
			{
				sql = "select max("+field2+") as ID from "+table+" where "+field1+"="+value1+" order by "+field2;
				if(!type1)       //是字符串时 将type1设为false
					sql = "select max("+field2+") as ID from "+table+" where "+field1+"='"+value1+"' order by "+field2;
			}
			if(!value2.equals(""))//当第二个字段不空时,作为条件查询第三个字段
			{
				sql = "select max("+field3+") as ID from "+table+" where "+field1+"="+value1+" and "+field2+"="+value2+" order by "+field3;
				if(!type2)       //是字符串时 将type1设为false
					sql = "select max("+field3+") as ID from "+table+" where "+field1+"='"+value1+"' and "+field2+"='"+value2+"' order by "+field3;
			}
			pstm = conn.prepareStatement(sql);
			rs = pstm.executeQuery();
			if(rs.next())  //有记录
			{
				out = rs.getInt("ID");
			}
		}catch(SQLException sqle){System.out.println("执行DataBase::makeID_Add1(String table,String field1,String field2,String field3,String value1,String value2,boolean type1,boolean type2)调用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 field3,String value1,String value2,boolean type1,boolean type2)调用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 field3,String value1,String value2,boolean type1,boolean type2)调用SQL语句 "+sql+" 时出错;\r\n错误为:"+e);}

⌨️ 快捷键说明

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