如何使用weblogic中的jndi数据源连接数据库。我正在使用以下代码,但它为连接提供空值
Context ctx = null;
Hashtable evn = new Hashtable();
evn.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
evn.put(Context.PROVIDER_URL,"t3://localhost:7001");
Connection conn = null;
try {
ctx = new InitialContext(evn);
javax.sql.DataSource ds
= (javax.sql.DataSource) ctx.lookup ("mydatasource");
conn = ds.getConnection();
}catch (Exception e) {
System.out.println();
// TODO: handle exception
}
答案 0 :(得分:0)
附加此内容以澄清有关异常原因的更多信息
catch(Exception sqlExp)
{
throw new SQLException("getConnection :: Exception"+sqlExp);
}
答案 1 :(得分:0)
测试连接 http://docs.oracle.com/cd/E23943_01/web.1111/e13737/jdbc_datasources.htm#CHDIIFHH
您是否将数据源定位到应用程序运行的weblogic服务器?
答案 2 :(得分:0)
//try this code:
Connection con = null;
DataSource datasource = null;
Context initialContext = new InitialContext();
// "jdbc/MyDBname" >> is a JNDI Name of DataSource on weblogic
datasource = (DataSource) initialContext.lookup("jdbc/MyDBname");
con = datasource.getConnection();