ahxcjxzxh 发表于 2017-2-3 07:50:57

tomcat websphere 从数据源中获取链接

public boolean getConnection() {

/**
* 连接池直接配置tomcat
*/
try {
Context initCtx = new InitialContext();
if (initCtx == null)
throw new Exception("没有匹配的上下文环境");

// tomcat 的连接方式。
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/trms");
// websphere 的连接方式。
// System.out.println("数据库链接前");
//DataSource ds = (DataSource) initCtx.lookup("jdbc/trms");
//         System.out.println("数据库链接成功");
if (ds == null)
throw new Exception("没有匹配的数据库");
conn = ds.getConnection();
conn.setAutoCommit(true);
// conn=DriverManager.getConnection("proxool.connpool");
return true;
} catch (Exception e) {
// 系统异常记录日志
error = "《==tool/com.cncsi.dm.db.conn/dbConnect//数据库公共类处理异常==》";
UserException.error(error);
UserException.error(e.toString());
e.printStackTrace();
return false;
}
}
页: [1]
查看完整版本: tomcat websphere 从数据源中获取链接