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

📄 connectionpool.java

📁 本程序使用struts+hibernate实现的oa办公系统
💻 JAVA
字号:
package com.db;

/**
 * ConnectionPool 用于连接缓冲池, 数据库连接 创建日期:(2000-9-21 13:10:46)
 * 
 * @author:
 */
import java.sql.*;
import java.util.*;
//import com.content.model.*;

//import com.ibm.ejs.dbm.jdbcext.*;
import javax.naming.*;

import javax.sql.*;

//import com.borland.javax.sql.*;

public class ConnectionPool {
	// ****************************************************************
	// * source 缓冲池名称
	// ****************************************************************
	String dbSource;

	// ****************************************************************
	// * dbLoginID 登录数据库的loginID
	// ****************************************************************
	String dbLoginID;

	// ****************************************************************
	// * dbPasswd 登录数据库的password
	// ****************************************************************
	String dbPasswd;

	// ****************************************************************
	// * ds 数据源
	// ****************************************************************
	DataSource ds;

	// ****************************************************************
	// * conn 数据库的连接
	// ****************************************************************
	//Connection conn;

	// ****************************************************************
	// * flag 数据库的连接标志
	// * = 0 时表示连接成功
	// * = -1 时表示绑定文件错误
	// * = -2 时表示缓冲池名称错误
	// * = -3 时表示数据库loginId or passwd错误
	// ****************************************************************
	int flag;

	/**
	 * ConnectionPool 构造子注解。
	 */
	public ConnectionPool(String fileName) {
		super();
		flag = 0;
		try {
			// Get information at runtime (from an external property file
			// identified by CONFIG_BUNDLE_NAME) about the user, password,
			// and owner information. This information could be provided
			// in other ways - perhaps in a somewhat more secure compiled
			// resource bundle, or hardcoded within this application.
			PropertyResourceBundle configBundle = (PropertyResourceBundle) ResourceBundle
					.getBundle(fileName);
			dbLoginID = configBundle.getString("controlDB.user");
			dbPasswd = configBundle.getString("controlDB.password");

			//If user and password are empty, set to null so that
			//the implicit database login will be used
			if (dbLoginID != null
					&& (dbLoginID.equals("null") || dbLoginID.equals("")))
				dbLoginID = null;
			if (dbPasswd != null
					&& (dbPasswd.equals("null") || dbPasswd.equals("")))
				dbPasswd = null;

			// Get context and logical name information to use below in a
			// naming service lookup to get a DataSource object.
			dbSource = configBundle.getString("controlDB.source");

		} catch (Exception e) {
			flag = -1;
		}

		setDataSource(dbSource);
	}

	/**
	 * 
	 * ConnectionPool 构造子注解。
	 * 
	 * 创建日期:(2000-9-21 16:40:30)
	 */
	public ConnectionPool(String source, String schema, String passwd) {
		super();

		dbSource = source;
		dbLoginID = schema;
		dbPasswd = passwd;

		setDataSource(dbSource);

	}

	/**
	 * 
	 * ConnectionPool 构造子注解。
	 * 
	 * 创建日期:(2000-9-21 16:40:30)
	 */
	/*
	 * public Connection getConnection() { Connection conn = null; try { conn =
	 * ds.getConnection(dbLoginID, dbPasswd); return conn; } catch(Exception e) {
	 * return conn; } }
	 */

	public static Connection getConnection() {
		Connection conn = null;
		javax.sql.DataSource ds = null;
		try {
			javax.naming.InitialContext ic = new javax.naming.InitialContext();
			ds = (javax.sql.DataSource) ic.lookup("java:comp/env/jdbc/OA");
		} catch (javax.naming.NamingException ne) {
			ne.printStackTrace();
		}
		try {
			conn = ds.getConnection();
			conn.setAutoCommit(false);

		} catch (java.sql.SQLException sqle) {
			//throw new Exception("SQLException while getting Connection " +
			// sqle.getMessage());
		}
		return conn;
	}

	/**
	 * getConnection(String fileName) 通过配置文件得到数据库连接 创建日期:(2000-9-21 13:29:20)
	 */
	public Connection getConnection(String fileName) {
		Connection conn = null;
		try {
			// Get information at runtime (from an external property file
			// identified by CONFIG_BUNDLE_NAME) about the user, password,
			// and owner information. This information could be provided
			// in other ways - perhaps in a somewhat more secure compiled
			// resource bundle, or hardcoded within this application.
			PropertyResourceBundle configBundle = (PropertyResourceBundle) ResourceBundle
					.getBundle(fileName);
			dbLoginID = configBundle.getString("controlDB.user");
			dbPasswd = configBundle.getString("controlDB.password");

			//If user and password are empty, set to null so that
			//the implicit database login will be used
			if (dbLoginID != null
					&& (dbLoginID.equals("null") || dbLoginID.equals("")))
				dbLoginID = null;
			if (dbPasswd != null
					&& (dbPasswd.equals("null") || dbPasswd.equals("")))
				dbPasswd = null;

			// Get context and logical name information to use below in a
			// naming service lookup to get a DataSource object.
			dbSource = configBundle.getString("controlDB.source");

		} catch (Exception e) {
			flag = -1;
		}

		setDataSource(dbSource);
		try {
			conn = ds.getConnection(dbLoginID, dbPasswd);
			return conn;
		} catch (Exception e) {
			flag = 3;
			return conn;
		}

	}

	/**
	 * getConnection(String source, String schema, String dbPasswd)
	 * 通过缓冲池名称,数据库schema,password得到数据库连接 创建日期:(2000-9-21 13:29:30)
	 */
	public Connection getConnection(String source, String schema,
			String dbPasswd) {
		Connection conn = null;
		setDataSource(source);
		try {
			conn = ds.getConnection(schema, dbPasswd);
			return conn;
		} catch (Exception e) {
			flag = 3;
			return conn;
		}

	}

	/**
	 * 在此处插入方法说明。 创建日期:(2000-9-21 18:34:38)
	 * 
	 * @return com.ibm.db2.jdbc.app.stdext.javax.sql.DataSource
	 */
	public DataSource getDataSource() {
		return ds;
	}

	/**
	 * getFlag() 返回错误标志 创建日期:(2000-9-21 18:23:53)
	 */
	public int getFlag() {
		return flag;
	}

	/**
	 * 在此处插入方法说明。 创建日期:(2000-9-21 18:28:05)
	 */
	public void setDataSource(String value) {
		try {
			// Create the initial naming context.
			Hashtable parms = new Hashtable();
			parms.put(Context.INITIAL_CONTEXT_FACTORY,
					"com.ibm.ejs.ns.jndi.CNInitialContextFactory");
			Context ctx = new InitialContext(parms);

			ds = (DataSource) ctx.lookup(value);
		} catch (Exception e) {
			flag = -2;
		}
	}
}

⌨️ 快捷键说明

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