首页>代码>java swing开发进销存管理系统>/SalesManagerment/src/org/rancode/framework/util/JdbcUtil.java
package org.rancode.framework.util;

/**
*
* 说明:JDBC工具类 用于数据库连接以及数据库资源释放
* 
* @author LS
* 
*/

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

import org.apache.log4j.Logger;

public class JdbcUtil {

	private static Logger logger = Logger.getLogger(JdbcUtil.class);
	private static JdbcUtil jdbcUtil;
	private static String url = null;
	private static String username = null;
	private static String password = null;
	private static String driver = null;
	private static Properties props = new Properties();

	static {

		try {
//			// 读取数据库配置文件
			InputStream in  = JdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
			props.load(in);	
		} catch (IOException e) {
			logger.error("加载jdbc.properties配置文件异常", e);
		}

		url = (props.getProperty("jdbc.url"));
		username = (props.getProperty("jdbc.username"));
		password = (props.getProperty("jdbc.password"));
		driver = (props.getProperty("jdbc.driver"));

		// 加载数据库驱动
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			logger.error("加载数据库驱动异常", e);
		}

	}

	// 单例模式
	public JdbcUtil getJdbcUtil() {

		if (jdbcUtil == null) {
			synchronized (JdbcUtil.class) {
				if (jdbcUtil == null) {
					jdbcUtil = new JdbcUtil();
				}
			}
		}
		return jdbcUtil;
	}

	/**
	 * 创建一个数据库连接
	 * 
	 * @return 一个数据库连接
	 * 
	 */
	public Connection getConnection() {
		Connection conn = null;
		// 创建数据库连接
		try {
			conn = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			logger.error("创建数据库连接发生异常", e);
		}
		return conn;
	}

	/**
	 * 释放数据库资源
	 */
	public void release(Object o) {
		if (o == null) {
			return;
		}
		if (o instanceof ResultSet) {
			try {
				((ResultSet) o).close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else if (o instanceof PreparedStatement) {
			try {
				((PreparedStatement) o).close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else if (o instanceof Connection) {
			Connection c = (Connection) o;
			try {
				if (!c.isClosed()) {
					c.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	// 释放数据库资源方法重载
	public void release(ResultSet rs, PreparedStatement pst, Connection conn) {
		release(rs);
		release(pst);
		release(conn);
	}

}
最近下载更多
DoustrongWU  LV5 2月12日
alive13gyp  LV2 1月22日
Zyy19820821  LV2 2024年9月24日
ma406805131  LV19 2024年6月25日
1542457015  LV3 2023年11月15日
zcfhh1  LV2 2023年10月3日
微信网友_6608533750566912  LV2 2023年8月16日
Assoc洛晓  LV2 2023年7月24日
2385649653  LV7 2023年6月17日
buhuia  LV4 2023年6月9日
最近浏览更多
519506215  LV1 4月16日
wwb521  LV7 4月10日
DoustrongWU  LV5 2月12日
alive13gyp  LV2 1月22日
lz88888  LV12 2024年10月17日
Zyy19820821  LV2 2024年9月16日
luoyanglin  LV2 2024年9月14日
微信网友_7030143019094016  LV1 2024年9月11日
liulian666  LV2 2024年7月19日
大明代码时代  LV7 2024年7月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友