0
我正在使用Jetty版本。 9.4.2.v20170220與oracle.jdbc.pool.OracleDataSource(版本11.2.0.4)連接(然後插入)Oracle Database 11g Express Edition。我很困惑與我的數據源的查找設置問題。我仍然遇到錯誤的Oracle URL地址。我的連接對象的tostring如下所示:[email protected]。我敢肯定,司機被正確安裝becouse就像直接連接:未能在jetty應用程序中使用JNDI查找連接Oracle數據庫
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配置文件中讀取它們。
我的碼頭-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之類的字符串?
感謝很多:)