首页>代码>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);
	}

}
最近下载更多
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日
y1214435276  LV9 2023年3月22日
漫步的海星  LV4 2023年3月14日
微信网友_6081645661278208  LV2 2023年2月28日
121575982  LV8 2023年2月5日
最近浏览更多
wuwutu  LV7 4月16日
linfon  LV2 1月25日
取个啥名字好呢  LV6 1月25日
codeshare  LV3 1月3日
LTPR66  LV1 2023年11月28日
xp95323  LV14 2023年11月27日
fesfefe  LV13 2023年11月25日
1542457015  LV3 2023年11月15日
awen287  LV6 2023年11月7日
zcfhh1  LV2 2023年10月3日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友