81,092
社区成员
发帖
与我相关
我的任务
分享
public final class Server {
private ConnectionFactory factory = null;
private static Server svr = new Server();
private Server() {
}
public static Server getInstance() {
return svr;
}
public synchronized ConnectionFactory getConnFactory() {
if (factory == null) {
try {
MiniProperties ppt = new MiniProperties();
ppt.load(Server.class.getResourceAsStream("/jdbc.properties"));
factory = new PoolConnectionFactory("test", ppt);
} catch (Exception e) {
throw new RuntimeException("jdbc配置文件读取失败!");
}
}
return factory;
}
public static void closeResource(Connection conn, Statement stmt,
ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
}
}
}
}
Context ct = new InitialContext() ;
DataSource ds = (DataSource) ct.lookup("jdbc/myOracle") ; //这里写你配置的jdbc/oracle名字
Connection conn = ds.getConnection() ;
if(conn!=null){
...
}