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

📄 database.java

📁 JSP+Mysql+Tomcat的新闻发部系统,最好下一个最新MYSQL驱动
💻 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 (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()//关闭游标
 History:
 date:2003-11-21
 name:liumei
 action:修改getOnePage(String sql,int page,int records)//分页时取得一页的数据量
 每页的记录显示条数从字典中取,即去掉records的作用
 ***********************************************************/
package com.ideas.bean;

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.ideas.util.*;

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

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

	/**取得连接池*/
	public void getConnPool()
	{
		try
		{
			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();

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

	/**创建我的连接池*/
	public boolean getMyConnPool()
	{
		//Configuration.DB_URL = "jdbc:mysql://192.168.0.1:3306/yj";
		//Configuration.DB_JDBCDRIVER = "org.gjt.mm.mysql.Driver";
		connMgr = DBConnectionManager.getInstance();
		conn = connMgr.getConnection(Configuration.ConnectionPoolName);
		if(conn == null)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

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



	 /**生成Oracle SQLServer等的连接*/
	 public void createConn(String drv, String url, String usr, String pwd)
	 {
		 try
		 {
			 Class.forName(drv).newInstance();
			 conn = DriverManager.getConnection(url, usr, pwd);

			 /**测试连接计数*/
			 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
		{
			Class.forName(drv).newInstance();
			conn = DriverManager.getConnection(url);

			/**测试连接计数*/
			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(conn != null)
			{
				conn.close();

				/**测试连接计数*/
				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("调用SQL语句 " + sql + " 时出错;\r\n错误为:" + sqle);
		}
		return rs;
	}

	/**执行增删改的语句*/
	public int ExecuteSQL(String sql)
	{
		try
		{
            boolean defaultCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);
			pstm = conn.prepareStatement(sql);
			pstm.executeUpdate();
			conn.commit();
            conn.setAutoCommit(defaultCommit);
			pstm.close();
		}
		catch(SQLException sqle)
		{
			System.out.println("调用SQL语句 " + sql + " 时出错;\r\n错误为:" + sqle);
			return sqle.getErrorCode();
		}
		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;

			}
			rs.close();
			pstm.close();
		}
		catch(SQLException sqle)
		{
			System.out.println("调用SQL语句 " + sql + " 时出错;\r\n错误为:" + sqle);
		}
		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);
			}

			rs.close();
			pstm.close();
		}
		catch(SQLException sqle)
		{
			System.out.println("调用SQL语句 " + sql + " 时出错;\r\n错误为:" + sqle);
		}
		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;
			}

⌨️ 快捷键说明

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