/**
* jndi 获取连接
*
* @param jndiName
* @return
*/
public static Connection getconn(String jndiName) {
Connection conn = null;
try {
// weblogic 环境下使用
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
ht.put(Context.SECURITY_PRINCIPAL, "weblogic");//用户名
ht.put(Context.SECURITY_CREDENTIALS, "weblogic1");//密码
Context ctx;
ctx = new InitialContext(ht);
DataSource source = (DataSource) ctx.lookup(jndiName);
Connection connection = source.getConnection();
if (connection != null) {
conn = connection;
}
} catch (NamingException e) {
log.error(e);
} catch (SQLException e) {
// logger.error(e);
log.error(e);
}
return conn;
}
注意:测试获取连接时的时候,服务器要启动。
参考文章:http://www.tuicool.com/articles/jQBbUz