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

📄 dbutil.java

📁 数据库类
💻 JAVA
字号:
package com.frame.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.transaction.UserTransaction;

import com.frame.exception.NoConnectionException;
import com.frame.log.LogHelper;

/**
 * <p>Title: DBUtil</p>
 * <p>Description: This class <code>DBUtil</code> is an utility class used bu another class to get
 * <code>java.sql.Connection</code>, <code>java.sql.DataSource</code> and <code>javax.transaction.
 * UserTransaction</code></p>
 * <p>Copyright: Copyright (c) 2002 by LBS Co.,Ltd</p>
 * <p>Company: LBS</p>
 * @author LBS Archiect Team
 * @version 0.1
 */

public class DBUtil {

	private static DataSource ds;
	private static Context ic;
	private static String dataSourceName;
	private static DataSource specifiedDS;
	private static DataSource logServiceDS;
	private static LogHelper log;

	public void init() {
		try {
			log = new LogHelper("DBUtil.class");
			ic = getInitialContext();
			Connection conn1 = null;
			Connection conn2 = null;
			try {
				conn1 = getConnection();
				conn2 = getConnection(GlobalNames.LOG_DATASOURCE_NAME);
			} catch (NoConnectionException ex) {
			}
			if (conn1 != null)
				conn1.close();
			if (conn2 != null)
				conn2.close();
		} catch (NamingException nam) {
			log.log(null, null, GlobalNames.DEBUG_LOG, nam.toString());
		} catch (SQLException sql) {
			log.log(null, null, GlobalNames.DEBUG_LOG, sql.toString());
		}

	}

	public static Connection getLogConnection() throws NoConnectionException {
		Connection conn = null;
		try {
			//if(ic == null)
			ic = getInitialContext();
			if (logServiceDS == null) {
				logServiceDS = (DataSource) ic.lookup(GlobalNames.LOG_DATASOURCE_NAME);
			}
			conn = logServiceDS.getConnection();
		} catch (NamingException ex) {
			System.out.println("catch NamingException when trying to lookup DataSource name\n" + GlobalNames.LOG_DATASOURCE_NAME);
			throw new NoConnectionException();
		} catch (SQLException sql) {
			System.out.println("catch SQLException when trying to get connection from DataSource\n"	+ sql.toString());
			throw new NoConnectionException();
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new NoConnectionException();
		} finally {
			if (GlobalNames.DEBUG_OUTPUT_FLAG) {
				System.out.println("Return connection = " + conn);
			}
			return conn;
		}
	}
	/**
	 * This method is used to get a <code>java.sql.connection</code> from connection pool
	 * @return java.sql.Connection
	 */
	public static Connection getConnection() throws NoConnectionException {
		Connection conn = null;
		try {
			if (ic == null){
				ic = getInitialContext();
			}
			if (ds == null){
				ds = (DataSource) ic.lookup(GlobalNames.DATA_SOURCE);
			}
			conn = ds.getConnection();
			if (conn == null){
				throw new NoConnectionException();
			}
		} catch (NamingException ex) {
			System.out.println("catch NamingException when trying to lookup DataSource name\n"	+ ex.toString());
			throw new NoConnectionException();
		} catch (SQLException sql) {
			System.out.println("catch SQLException when trying to get connection from DataSource\n"	+ sql.toString());
			throw new NoConnectionException();
		} finally {
			if (GlobalNames.DEBUG_OUTPUT_FLAG) {
				System.out.println("Return connection = " + conn);
			}
			return conn;
		}
	}

	public static Connection getConnection(String dsName) throws NoConnectionException {
		Connection conn = null;
		if (dsName == null) {
			System.out.println("unable to look a null datasource!!!");
			return null;
		}
		try {
			if (ic == null){
				ic = getInitialContext();
			}
			if (dataSourceName == null) {
				dataSourceName = dsName;
			}
			if (!dsName.equals(dataSourceName)) {
				dataSourceName = dsName;
				specifiedDS = (DataSource) ic.lookup(dataSourceName);
			} else if (specifiedDS == null) {
				specifiedDS = (DataSource) ic.lookup(dataSourceName);
			}
			conn = specifiedDS.getConnection();
		} catch (NamingException naming) {
			System.out.println("Catching naming exception when try to lookup "	+ "datasource["	+ dsName + "] "	+ naming.toString());
			throw new NoConnectionException();
		} catch (SQLException sqle) {
			System.out.println("Catching SQLException when trying to getConnection"	+ "from datasource[" + dsName + "] " + sqle.toString());
			throw new NoConnectionException();
		} finally {
			return conn;
		}
	}
	/**
	 * This method is used to get a Sequence  from
	 * Database.
	 * @return java.lang.String
	 */
	public static String getSequence(Connection con, String sequenceName) throws SQLException {
		String sequenceID = null;
		StringBuffer buffer = new StringBuffer();
		buffer.append("Select " + sequenceName + ".nextval from dual");
		PreparedStatement ps = con.prepareStatement(buffer.toString());
		ResultSet rs = ps.executeQuery();
		if (rs.next()){
			sequenceID = rs.getString(1);
		}
		if (rs != null){
			rs.close();
		}
		if (ps != null){
			ps.close();
		}
		//log.log(null, GlobalNames.DEBUG_LOG, " ===== generate a sequenceID :" + sequenceID + " =====");
		return sequenceID;
	}
	/**
	 * This method is used to get a Sequence  from
	 * Database.
	 * @return java.lang.String
	 */
	public static Integer getSequence(String sequenceName) throws SQLException {
		Integer sequenceID = null;
		StringBuffer buffer = new StringBuffer();
		buffer.append("Select " + sequenceName + ".nextval from dual");
		Connection con = null;
		try {
			con = getConnection();
		} catch (NoConnectionException e) {
			e.printStackTrace();
		}
		PreparedStatement ps = con.prepareStatement(buffer.toString());
		ResultSet rs = ps.executeQuery();
		if (rs.next()){
			sequenceID = new Integer(rs.getInt(1));
		}
		if (rs != null){
			rs.close();
		}
		if (ps != null){
			ps.close();
		}
		if (con != null) {
			con.close();
		}
		//log.log(null, GlobalNames.DEBUG_LOG, " ===== generate a sequenceID :" + sequenceID + " =====");
		return sequenceID;
	}
	/**
	 * This method is used to get a <code>javax.transaction.UserTransaction</code> from
	 * JNDI tree.
	 * @return javax.transaction.UserTransaction
	 */
	public static UserTransaction getUserTransaction() throws NamingException, Exception {
		UserTransaction tran = null;
		try {
			Context ic = getInitialContext();
			if (ic != null) {
				tran = (UserTransaction) ic.lookup(GlobalNames.USER_TRANSACTION);
			}
			if (tran == null) {
				throw new Exception("UserTransaction is null!");
			}
		} catch (NamingException ex) {
			System.out.println("catch NamingException when trying to lookup UserTransaction name\n"	+ ex.toString());
			throw ex;
		} finally {
			return tran;
		}
	}

	public static String dumpBlob(Blob blob) throws SQLException, IOException {
		String returnStr = null;
		long length = 0;
		InputStream instream = blob.getBinaryStream();
		byte[] buffer = new byte[(int) blob.length()];
		instream.read(buffer);
		instream.close();
		returnStr = new String(buffer);                              
		return returnStr;
	}

	/**
	 * This method is used to get an <code>javax.naming.InitialContext</code> object from
	 * JDNI tree.
	 * @return javax.transaction.InitialContext
	 * @throws NamingException
	 */
	private static Context getInitialContext() throws NamingException {
		Properties properties = null;
		try {
			properties = new Properties();
			properties.put(Context.INITIAL_CONTEXT_FACTORY,	GlobalNames.INITIAL_FACTORY);
			properties.put(Context.PROVIDER_URL, GlobalNames.URL_PROVIDER);
			return new InitialContext(properties);
		} catch (Exception e) {
			System.out.println("Unable to connect to WebLogic server at " + GlobalNames.URL_PROVIDER);
			System.out.println("Please make sure that the server is running.");
			return null;
		}
	}
	private static Context getInitialContext(String url) throws NamingException {
		Properties properties = null;
		try {
			properties = new Properties();
			properties.put(Context.INITIAL_CONTEXT_FACTORY,GlobalNames.INITIAL_FACTORY);
			properties.put(Context.PROVIDER_URL, url);
			return new InitialContext(properties);
		} catch (Exception e) {
			System.out.println("Unable to connect to WebLogic server at " + GlobalNames.URL_PROVIDER);
			System.out.println("Please make sure that the server is running.");
			return null;
		}
	}

	/**
	 * 释放PreparedStatement,以供子类调用
	 * @param statement
	 * @throws SQLException
	 */
	public static void closeStatement(PreparedStatement statement) throws SQLException {
		if (statement != null) {
			statement.close();
			statement = null;
		}
	}
	/**
	 * 释放PreparedStatement,以供子类调用
	 * @param statement
	 * @throws SQLException
	 */
	public static void closeConnection(Connection con) throws SQLException {
		if (con != null) {
			con.close();
			con = null;
		}
	}
	/**
	 * 释放ResultSet,以供子类调用
	 * @param rs
	 * @throws SQLException
	 */
	public static void closeResultSet(ResultSet rs) throws SQLException {
		if (rs != null) {
			rs.close();
			rs = null;
		}
	}

	/**
	 * 释放ResultSet和
	 * @param rs
	 * @param statement
	 * @throws SQLException
	 */
	public static void closeResStat(ResultSet rs, PreparedStatement statement)
		throws SQLException {
		closeResultSet(rs);
		closeStatement(statement);
	}
	public static void closeRes(Connection con,ResultSet rs,PreparedStatement statement)throws SQLException {
		closeResultSet(rs);
		closeStatement(statement);
		closeConnection(con);
	}

}

⌨️ 快捷键说明

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