关闭

JDBC链接数据库JdbcConnecton.java

标签: javaconnection
454人阅读 评论(0) 收藏 举报
分类:
package ht.chart.fusioncharts.dao;

import ht.chart.fusioncharts.ChartProperties;
import ht.chart.utils.HTLogger;
import ht.chart.utils.PropertiesUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import org.apache.commons.logging.LogFactory;

/**
 * 数据连接
 * @author tuwei
 */
public class JdbcConnecton {

	private static HTLogger logger = HTLogger.getLogger(JdbcConnecton.class);
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//String sql = "select * from COMMONDATAATTR a join COMMONDATAVALUE b on A.COMMONDATAATTRID = B.COMMONDATAATTRID";
		try {
			getConnection();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		logger.debug("test");
	}

	public static Connection getConnection() throws ClassNotFoundException, SQLException  {
		ResourceBundle rb = PropertiesUtils.getPropertiesBundle(ChartProperties.DATA_SOURCE);
		
		String driver = rb.getString("database.driverClassName");
		String url = rb.getString("database.url");
		String username = rb.getString("database.username");
		String password = rb.getString("database.password");
		
		Class.forName(driver);
		
		Connection conn = DriverManager.getConnection(url, username, password);
		
		logger.info("database connect successfully!");
		
		return conn;

	}

	public static void close(Connection con, Statement st) {

		close(con);

		close(st);
	}

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

		close(rs);
	}

	public static void close(Connection con) {
		try {
			con.close();
			logger.info("connection close successfully!");
		} catch (Exception e) {
		}
	}

	public static void close(Statement st) {
		try {
			st.close();
			logger.info("st close successfully!");
		} catch (Exception e) {
		}
	}

	public static void close(ResultSet rs) {
		try {
			rs.close();
			logger.info("rs close successfully!");
		} catch (Exception e) {
		}
	}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:35110次
    • 积分:837
    • 等级:
    • 排名:千里之外
    • 原创:47篇
    • 转载:7篇
    • 译文:0篇
    • 评论:2条
    最新评论