So I have a MySQL database, and I have a datasource on a local instance of WebLogic which is connected to that database. I am trying to write some client code which will simply connect and query. I am having issues with obtaining a connection from the datasource. Here's my code thus far. I am running WebLogic 12c.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class ConnectionTest {
public static void main(String... args) {
ConnectionTest tCon = new ConnectionTest();
tCon.TestConnection();
}
public void TestConnection() {
Context ctx = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Hashtable<String, String> props = new Hashtable<String, String>();
props.put("java.naming.factory.initial",
"weblogic.jndi.WLInitialContextFactory");
props.put("java.naming.provider.url", "t3://localhost:7001");
props.put("java.naming.security.principal", "weblogic");
props.put("java.naming.security.credentials", "welcome1");
ctx = new InitialContext(props);
DataSource ds = (DataSource) ctx.lookup("RegexDB");
System.out.println(ds);
DAO dao = new DAO();
conn = ds.getConnection();
stmt = conn.createStatement();
stmt.execute("select * from regular_ex");
rs = stmt.getResultSet();
ArrayList<HashMap<String, Object>> results = dao
.resultSetToArrayList(rs);
dao.printArrayList(results);
stmt.close();
conn.close();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
This fails at ds.getConnection()
with the following exception:
java.lang.ClassCastException: weblogic.jdbc.common.internal.ConnectionEnv cannot be cast to java.io.Serializable
at weblogic.iiop.IIOPOutputStream.writeObject(IIOPOutputStream.java:2285)
at weblogic.utils.io.ObjectStreamClass.writeFields(ObjectStreamClass.java:414)
at weblogic.corba.utils.ValueHandlerImpl.writeValueData(ValueHandlerImpl.java:235)
at weblogic.corba.utils.ValueHandlerImpl.writeValueData(ValueHandlerImpl.java:225)
at weblogic.corba.utils.ValueHandlerImpl.writeValue(ValueHandlerImpl.java:182)
at weblogic.iiop.IIOPOutputStream.write_value(IIOPOutputStream.java:1983)
at weblogic.iiop.IIOPOutputStream.write_value(IIOPOutputStream.java:2021)
at weblogic.iiop.IIOPOutputStream.writeObject(IIOPOutputStream.java:2285)
at weblogic.jdbc.common.internal.RmiDataSource_WLSkel.invoke(Unknown Source)
at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:695)
at weblogic.rmi.cluster.ClusterableServerRef.invoke(ClusterableServerRef.java:230)
at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:520)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:146)
at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:516)
at weblogic.rmi.internal.wls.WLSExecuteRequest.run(WLSExecuteRequest.java:118)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
I have wlclient.jar, wlsafclient.jar, and weblogic.jar in my buildpath. I have tried all sorts of combinations of adding/removing these jars, but I still get the same error regardless of what I do. Any help would be greatly appreciated.
After doing some research, I am deleting my old answer and starting over.
There is a large table of client types in the Oracle Doc for WebLogic Standalone Clients. For each type of client, listed, the table shows the required jar files. For certain types of clients, you need to build an additional jar (wlfullclient.jar) and include that.
Hope this helps.