java 通过 WebLogic 连接数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10838403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Database Connection via WebLogic
提问by Reid Mac
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.
所以我有一个 MySQL 数据库,并且我在连接到该数据库的 WebLogic 的本地实例上有一个数据源。我正在尝试编写一些客户端代码,这些代码将简单地连接和查询。我在从数据源获取连接时遇到问题。到目前为止,这是我的代码。我正在运行 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:
这失败了ds.getConnection()
,但有以下例外:
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.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.
我的构建路径中有 wlclient.jar、wlsafclient.jar 和 weblogic.jar。我已经尝试了各种添加/删除这些 jar 的组合,但无论我做什么,我仍然遇到相同的错误。任何帮助将不胜感激。
采纳答案by Jeremy Goodell
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.
Oracle Doc for WebLogic Standalone Clients 中有一个很大的客户端类型表。对于列出的每种类型的客户端,该表显示了所需的 jar 文件。对于某些类型的客户端,您需要构建一个额外的 jar (wlfullclient.jar) 并将其包含在内。
Hope this helps.
希望这可以帮助。
回答by userBI
I have also face this problem and I tried to add "wlfullclient.jar" to my directory to fix it out but I didn't find this jar file in weblogic installation folder.
我也遇到了这个问题,我尝试将“wlfullclient.jar”添加到我的目录中以修复它,但我没有在 weblogic 安装文件夹中找到这个 jar 文件。
But at the last I have set all required jar files form weblogic by using setDomainEnv.cmd and it works fine. Here we don't have to care about which jar files required or not it'll simply set classpath for all required jar file for your program.
但最后我已经使用 setDomainEnv.cmd 从 weblogic 中设置了所有必需的 jar 文件并且它工作正常。在这里,我们不必关心需要哪些 jar 文件,它会简单地为程序所需的所有 jar 文件设置类路径。
I am using Weblogic 11g.
我正在使用 Weblogic 11g。
回答by Pramit
In Weblogic 12c, copy the weblogic.jar file to some other directory. Rename the file to weblogic-classes.jar and then build the jar file using wljarbuilder.
在 Weblogic 12c 中,将 weblogic.jar 文件复制到某个其他目录。将文件重命名为 weblogic-classes.jar,然后使用 wljarbuilder 构建 jar 文件。
Add the newly created wlfullclient.jar file to your Class Path in eclipse.
将新创建的 wlfullclient.jar 文件添加到 eclipse 中的类路径。
回答by user5020099
Build wlfullclient.jar and add just this jar to the build path. It solved the problem for me. By the way weblogic.jar from Weblogic 12 is missing some classes as compared to weblogic.jar from Weblogic 10.3
构建 wlfullclient.jar 并将这个 jar 添加到构建路径中。它为我解决了这个问题。顺便说一下,与来自 Weblogic 10.3 的 weblogic.jar 相比,来自 Weblogic 12 的 weblogic.jar 缺少一些类