我正在尝试使用以下代码通过Java客户端访问数据源连接:
Connection conn;
Statement stmt;
ResultSet rs;
try {
Properties prop = new Properties();
prop.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
prop.put(javax.naming.Context.PROVIDER_URL, "t3://localhost:7001");
Context ctx = new InitialContext(prop);
Object obj = ctx.lookup("test/datasource");
System.out.println("Data Source Found….");
DataSource ds = (DataSource) obj;
conn = ds.getConnection();
System.out.println("Data Source User Name::"+conn.getMetaData().getUserName());
stmt = conn.createStatement();
String query = "select 1 from dual";
System.out.println("Query " + query);
rs = stmt.executeQuery(query);
if (rs != null) {
System.out.println("Some Data Found in Query");
} else {
System.out.println("No Data Found in Query");
}
ctx.close();
} catch (Exception e) {
e.printStackTrace();
}
但我得到了以下堆栈跟踪。我无法弄清楚发生了什么。有人知道吗?
找到数据源......
2012年2月13日下午4:07:28 GMT-03:00信息JDBC BEA-001155发生以下异常:
显示java.lang.NullPointerException 在weblogic.utils.wrapper.WrapperFactory.getCachedWrapperClass(WrapperFactory.java:55) 在weblogic.utils.wrapper.WrapperFactory.getWrapperClass(WrapperFactory.java:202) 在weblogic.utils.wrapper.WrapperFactory.getWrapperClass(WrapperFactory.java:190) 在weblogic.utils.wrapper.WrapperFactory.createWrapper(WrapperFactory.java:175) 在weblogic.jdbc.wrapper.JDBCWrapperFactory.getWrapper(JDBCWrapperFactory.java:197) 在weblogic.jdbc.rmi.SerialConnection.readResolve(SerialConnection.java:140) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在java.lang.reflect.Method.invoke(Method.java:597) at java.io.ObjectStreamClass.invokeReadResolve(ObjectStreamClass.java:1061) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1762) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) at weblogic.utils.io.ChunkedObjectInputStream.readObject(ChunkedObjectInputStream.java:197) at weblogic.rjvm.MsgAbbrevInputStream.readObject(MsgAbbrevInputStream.java:564) at weblogic.utils.io.ChunkedObjectInputStream.readObject(ChunkedObjectInputStream.java:193) 在weblogic.rmi.internal.ObjectIO.readObject(ObjectIO.java:62) 在weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:240) 在weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:348) 在weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:259) 在weblogic.jdbc.common.internal.RmiDataSource_1031_WLStub.getConnection(未知来源)
提前致谢。
答案 0 :(得分:0)
如果你使用的是Weblogic 10.3及以上最好的万无一失的解决方案(这不会让你挠头)是:
只包括找到的wlfullclient.jar $ {wlserver_10.3_installation_home} \ server \ lib文件夹
你的类路径(lib)中的。这样您就不需要weblogic.jar
如果找不到wlfullclient.jar,那么构建它。以下是如何构建它:
http://docs.oracle.com/cd/E12840_01/wls/docs103/client/jarbuilder.html
然后写下这段代码:
//Some Code
weblogic.jndi.Environment env = new weblogic.jndi.Environment();
env.setInitialContextFactory(Environment.DEFAULT_INITIAL_CONTEXT_FACTORY);
env.setProviderUrl("t3://${weblogic_host_ip}:${weblogic_port_number}");
env.setSecurityPrincipal(${DB_USER});
env.setSecurityCredentials(${DB_PASSWORD});
java.lang.Object obj = null;
try {
javax.naming.Context ctx = env.getInitialContext();
obj = ctx.lookup(${JNDI_NAME_OF_DATA_SOURCE});
javax.sql.DataSource ds = (javax.sql.DataSource) obj;
java.sql.Connection conn = ds.getConnection();
java.sql.Statement stmt = conn.createStatement();
java.sql.ResultSet rs = stmt.executeQuery("select '5' from dual");
if(rs != null){
rs.next();
System.out.println(rs.getString(1));
}
} catch (javax.naming.NamingException e) {
e.printStackTrace();
}
//Other Code ....