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

📄 dbfactory.java

📁 java数据库操作工具类
💻 JAVA
字号:
package com.baosight.util;

import org.apache.commons.dbcp.BasicDataSource;
import java.lang.reflect.*;
import java.util.*;
import java.sql.*;

/**
 * <p>
 * Title: Baosight Appraise System
 * </p>
 * <p>
 * Description: Web-based Appraise System
 * </p>
 * <p>
 * Copyright: Copyright (c) Baosight 2005
 * </p>
 * <p>
 * Company: Baosight
 * </p>
 * 
 * @author WangDingYi
 * @version 1.0
 */

public class DBFactory {

	// 文件声明
	private static final Properties p = new Properties();

	// 声明数据库连接池

	private static final BasicDataSource DBS = new BasicDataSource();

	// 声明配置文件变量变量
	private static String DB_URL = null;

	private static String DB_USER = null;

	private static String DB_PASS = null;

	private static String DB_DRIVER = null;

	private static String JNDI_DS = null;

	private static String USE_DS = null;

	private static String DB_MaxActive = null;

	private static String DB_MaxIdle = null;

	private static String DB_MaxWait = null;

	static {
		try {

			// 获得系统配置
			Logger.log("Loading [config.properties] ...");
			p.load(Thread.currentThread().getContextClassLoader().
					getResourceAsStream("config.properties"));
			Enumeration enu = p.keys();
			Field f[] = DBFactory.class.getDeclaredFields();
			String field_name = null;

			// 使用循环取出配置文件的内容
			while (enu.hasMoreElements()) {
				field_name = (String) enu.nextElement();
				for (int i = 0; i < f.length; i++) {
					if (f[i].getName().toLowerCase().indexOf("class") == -1
							&& f[i].getName().equalsIgnoreCase(field_name)) {
						f[i].set(null, p.getProperty(field_name));
					}
				}
			}

			Logger.log("DB_URL = " + DB_URL);
			Logger.log("DB_USER = " + DB_USER);
			Logger.log("DB_PASS = " + DB_PASS);
			Logger.log("DB_DRIVER = " + DB_DRIVER);
			Logger.log("JNDI_DS = " + JNDI_DS);
			Logger.log("USE_DS = " + USE_DS);
			Logger.log("DB_MaxActive = " + DB_MaxActive);
			Logger.log("DB_MaxIdle = " + DB_MaxIdle);
			Logger.log("DB_MaxWait = " + DB_MaxWait);
			Logger.log("DB_USER = " + DB_USER);

			// 输出信息
			System.out.println("Configuration data successfully loaded!");

			// 进行数据库链接池配置
			DBS.setDriverClassName(DB_DRIVER);
			DBS.setUrl(DB_URL);
			DBS.setPassword(DB_PASS);
			DBS.setUsername(DB_USER);
			DBS.setMaxActive(Integer.parseInt(DB_MaxActive));
			DBS.setMaxIdle(Integer.parseInt(DB_MaxIdle));
			DBS.setMaxWait(Long.parseLong(DB_MaxWait));

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
	}

	/**
	 * 获得数据连接
	 * 
	 * @throws Exception
	 * @return Connection
	 */
	public static Connection borrowConnection() throws Exception {
		try {

			Connection conn = null;

			if (USE_DS.equals("false")) {
				conn = DBS.getConnection();
				System.out.println("当前的忙碌链接个数:" + DBS.getNumActive());
				System.out.println("当前的空闲链接个数:" + DBS.getMinIdle());
			} else {

			}

			return conn;

		} catch (SQLException e) {
			e.printStackTrace();
			throw new Exception(e.toString());
		}
	}

	/**
	 * 返回数据库连接
	 * 
	 * @parameter conn Connection
	 * @throws Exception
	 */
	public static void returnConnection(java.sql.Connection conn)
			throws Exception {
		try {
			if( conn != null ) conn.close();
		} catch (SQLException e) {
			throw new Exception(e.toString());
		}
	}

	/**
	 * 运行sql,支持事务
	 * 
	 * @param sql
	 *            String
	 * @param conn
	 *            Connection
	 * @return int
	 * @throws Exception
	 */
	public static int doUpdate(String sql)
			throws Exception {
		java.sql.Statement stmt = null;
		Connection conn=null;
		try {
			conn = borrowConnection();
			stmt = conn.createStatement();
			return stmt.executeUpdate(sql);
		} catch (Exception e) {
			throw new Exception(e);
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
				if( conn != null){
					conn.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

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