簡體   English   中英

無法在Jetty應用程序中將Oracle數據庫與JNDI查找連接

[英]Fail to connect Oracle db with JNDI lookup in jetty app

我正在使用Jetty版本。 9.4.2.v20170220和oracle.jdbc.pool.OracleDataSource(版本11.2.0.4)連接(然后插入)Oracle Database 11g Express Edition。 我為數據源的查找設置感到困惑。 我仍然錯在Oracle URL地址上。 我的連接對象的tostring如下所示:oracle.jdbc.pool.OracleDataSource@3d10bb5a。 我確定驅動程序已正確安裝,因為直接連接如下所示:

private static Connection getDBConnection() {

    Connection dbConnection = null;
    try {

        Class.forName(DB_DRIVER);
    } catch (ClassNotFoundException e) {
        System.out.println(e.getMessage());
    }

    try {
        dbConnection = DriverManager.getConnection(
                DB_CONNECTION, DB_USER, DB_PASSWORD);
        return dbConnection;
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return dbConnection;

完美的作品(我可以使插入沒有任何問題)。 但是我不想對連接屬性進行硬編碼,我想從xml配置文件中讀取它們。

我的jetty-env.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"    "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">

<New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg><Ref refid="wac"/></Arg>
    <Arg>jdbc/DSTest</Arg>
    <Arg>
        <New class="oracle.jdbc.pool.OracleDataSource">
            <Set name="DriverType">thin</Set>
            <Set name="URL">jdbc:oracle:system:thin:@localhost:1521:xe</Set>
            <Set name="User">mylogin</Set>
            <Set name="Password">mypass</Set>
        </New>
    </Arg>
</New>

</Configure>

web.xml的片段:

<resource-ref>
    <description>My DataSource Reference</description>
    <res-ref-name>jdbc/DSTest</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

和Servlet代碼片段(用於連接,然后插入):

private static Connection connTest() throws NamingException, SQLException {

    Context initContext = new InitialContext();
    DataSource dataSource = (DataSource) initContext.lookup("java:comp/env/jdbc/DSTest");
    System.out.println(dataSource);
    System.out.println(dataSource.toString());
    Connection connection = dataSource.getConnection();
    return connection;
}


@POST
@Path("/testinsert")
@Consumes(MediaType.APPLICATION_JSON)
public Response testinsert(SimpleJsonModel simpleJsonModel) {

    String result = "JSON saved test: " + simpleJsonModel;

    try {
        insertRecord(simpleJsonModel.getJsonId(), simpleJsonModel.getNumber());
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }

    return Response.status(201).entity(result).build();
}

private static void insertRecord(String val1, String val2) throws SQLException {

    Connection dbConnection = null;
    Statement statement = null;
    String insertTableSQL = "insert into json.johny_table ( jsonId, numberJ) values ('" + val1 + "', '" + val2 + "')";

    try {
        dbConnection = connTest();
        statement = dbConnection.createStatement();
        System.out.println(insertTableSQL);
        statement.executeUpdate(insertTableSQL);
        System.out.println("inserted");

    } catch (SQLException e) {
        System.out.println(e.getMessage());
    } catch (NamingException e) {
        e.printStackTrace();
    } finally {

        if (statement != null) {
            statement.close();
        }
        if (dbConnection != null) {
            dbConnection.close();
        }
    }
}
}

因此,問題是如何通過查找正確讀取jndi屬性以及如何建立與它的連接。 還有任何技巧如何從jetty-env.xml讀取類似url的字符串?

非常感謝 :)

一切都很好,除了jetty-env.xml中的一行:

<Set name="URL">jdbc:oracle:system:thin:@localhost:1521:xe</Set>

應該是:

<Set name="URL">jdbc:oracle:thin:@localhost:1521:xe</Set>

我不知道為什么將詞系統放在這里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM