简体   繁体   English

通过WebLogic进行数据库连接

[英]Database Connection via WebLogic

So I have a MySQL database, and I have a datasource on a local instance of WebLogic which is connected to that database. 因此,我有一个MySQL数据库,并且在与该数据库连接的WebLogic本地实例上有一个数据源。 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. 我正在运行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$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. 我的构建路径中有wlclient.jar,wlsafclient.jar和weblogic.jar。 I have tried all sorts of combinations of adding/removing these jars, but I still get the same error regardless of what I do. 我已经尝试了添加/删除这些jar的各种组合,但是无论我做什么,我仍然会遇到相同的错误。 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 . Oracle Doc for WebLogic独立客户端中有大量的客户端类型表。 For each type of client, listed, the table shows the required jar files. 对于列出的每种类型的客户端,该表显示所需的jar文件。 For certain types of clients, you need to build an additional jar (wlfullclient.jar) and include that. 对于某些类型的客户端,您需要构建一个附加的jar(wlfullclient.jar)并将其包含在内。

Hope this helps. 希望这可以帮助。

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. 但是最后,我使用setDomainEnv.cmd从weblogic设置了所有必需的jar文件,并且效果很好。 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. 在这里,我们不必关心是否需要哪些jar文件,只需为程序中所有必需的jar文件设置类路径即可。

I am using Weblogic 11g. 我正在使用Weblogic 11g。

In Weblogic 12c, copy the weblogic.jar file to some other directory. 在Weblogic 12c中,将weblogic.jar文件复制到其他目录。 Rename the file to weblogic-classes.jar and then build the jar file using wljarbuilder. 将文件重命名为weblogic-classes.jar,然后使用wljarbuilder构建jar文件。

Add the newly created wlfullclient.jar file to your Class Path in eclipse. 将新创建的wlfullclient.jar文件添加到eclipse中的类路径中。

Build wlfullclient.jar and add just this jar to the build path. 生成wlfullclient.jar并将此jar添加到生成路径。 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 与Weblogic 10.3的weblogic.jar相比,Weblogic 12的weblogic.jar缺少了一些类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM