我正在“尝试查找'jdbc.LogDB'时没有找到子上下文'jdbc'。当我尝试从Weblogic获取连接时,已解决''”错误。我创建并测试了数据源,它正在工作。还创建了目标服务器。数据源名称为“jdbc / LogDb”。下面是我写的测试代码。
public class TestUtils {
private static final Logger logger = LoggerFactory.getLogger(TestUtils.class.getName());
public DataSource ds = null;
public String testConnectionPool() throws SQLException {
Context ctx = null;
Hashtable ht = new Hashtable();
Connection conn;
String result = "";
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, "weblogic");
try {
ctx = new InitialContext(ht);
ds = (DataSource) ctx.lookup("jdbc/LogDB");
logger.debug("Weblogic Connection Pool Created");
conn = ds.getConnection();
Statement stmt = conn.createStatement();
stmt.execute("some sql");
ResultSet rs = stmt.getResultSet();
if(rs.next()){
result = result + rs.getString(1);
}
stmt.close();
conn.close();
} catch (NamingException e) {
logger.error("Naming Exception occured at connect: " + e.getMessage());
} catch (Exception e){
logger.error("Exception occured at connect: "+ e.getMessage());
} finally {
try {
if (ctx != null) {
ctx.close();
}
}
catch (Exception e) {
logger.error("Ctx Error");
}
}
return result;
}
}
我尝试了以下名字 “Java的:JDBC / LogDb” “java:comp / env”变种等。
感谢您的回答