时间:2021-07-01 10:21:17 帮助过:4人阅读
package com.xx.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
public class JdbcUtilk {
private static Properties map = new Properties();
static{
InputStream is = null;
try {
// 读取配置文件,获取信息
//is = new FileInputStream("d:/workspace/Jdbc/src/conf/conn.properties");
is = JdbcUtilk.class.getResourceAsStream("/conf/conn.properties");
map.load(is);
} catch (Exception e) {
throw new RuntimeException("配置文件读取出现错误");
} finally{
if(is!=null) try{ is.close(); }catch(Exception e){}
}
}
private static final ThreadLocal<Connection> tol = new ThreadLocal<Connection>();
public static Connection getConnection(){
try {
Connection conn = tol.get();
if(conn == null){
Class.forName( map.getProperty("driver") );
conn = DriverManager.getConnection( map.getProperty("url"),map.getProperty("username"),map.getProperty("password"));
tol.set(conn);
}
return conn;
} catch (Exception e) {
throw new RuntimeException("出现问题,连接无法获取");
}
}
public static void close(ResultSet rs,Statement stm,Connection conn){
if(rs!=null) try{ rs.close(); }catch(Exception e){}
if(stm!=null) try{ stm.close(); }catch(Exception e){}
if(conn!=null) try{ conn.close(); tol.remove(); }catch(Exception e){}
}
}本文出自 “永恒之光” 博客,请务必保留此出处http://zhuws.blog.51cto.com/11134439/1969560
jdbc
标签:jdbc