日期:2014-05-16  浏览次数:20354 次

数据库连接工具类
package tarena.util;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBUtil {
	private static final String URL;
	private static final String DRIVER;
	private static final String USERNAME;
	private static final String PASSWORD;

	static {
		try {
			Properties prop = new Properties();
			/**
			 * db.properties文件保存于项目src目录下
			 */
			InputStream in = DBUtil.class.getResourceAsStream("/db.properties");
			prop.load(in);
			in.close();

			URL = prop.getProperty("url");
			DRIVER = prop.getProperty("driver");
			USERNAME = prop.getProperty("username");
			PASSWORD = prop.getProperty("password");

			Class.forName(DRIVER);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("database connection config error!!!", e);
		}
	}

	public static Connection open() throws SQLException {
		return DriverManager.getConnection(URL, USERNAME, PASSWORD);
	}

	public static void close(ResultSet rs) {
		if (rs == null)
			return;
		try {
			rs.close();
		} catch (Exception e) {
		}
	}

	public static void close(Statement stmt) {
		if (stmt == null)
			return;
		try {
			stmt.close();
		} catch (Exception e) {
		}
	}

	public static void close(Connection con) {
		if (con == null)
			return;
		try {
			con.close();
		} catch (Exception e) {
		}
	}

	public static void close(ResultSet rs, Statement stmt) {
		close(rs, stmt, null);
	}

	public static void close(Statement stmt, Connection con) {
		close(null, stmt, con);
	}

	public static void close(ResultSet rs, Statement stmt, Connection con) {
		close(rs);
		close(stmt);
		close(con);
	}

}