无法在jetty应用程序中将Oracle数据库与JNDI查找连接起来

时间:2017-03-09 14:03:35

标签: java oracle11g jetty embedded-jetty jetty-9

我正在使用Jetty ver。 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的字符串的任何提示?

非常感谢:)

1 个答案:

答案 0 :(得分:0)

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

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

应该是:

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

我不知道为什么我把字系统放在那里。