?? jdbcutil.java
字號:
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
import java.io.*;
public class JdbcUtil {
private static Properties info=new Properties();
static{
try {
InputStream is=JdbcUtil.class
.getResourceAsStream("/config/config.properties");
info.load(is);
is.close();
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
private static final ThreadLocal<Connection> tl=new ThreadLocal<Connection>();
public static Connection getConnection() throws Exception{
Connection conn=tl.get();
if(conn==null){
Class.forName(info.getProperty("driver"));
conn=DriverManager.getConnection(info.getProperty("url"),
info.getProperty("username"),info.getProperty("password"));
tl.set(conn);
}
return conn;
}
public static void release(ResultSet rs,Statement stm,Connection conn){
if(rs!=null) try{ rs.close();} catch(Exception ee){}
if(stm!=null) try{ stm.close();} catch(Exception ee){}
if(conn!=null) try{ conn.close();} catch(Exception ee){}
}
/*public static void main(String[] args) throws Exception{
System.out.println(getConnection());
}*/
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -