Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 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>
- <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>
- 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();
- }
- }
- }
- }
- <Set name="URL">jdbc:oracle:system:thin:@localhost:1521:xe</Set>
- <Set name="URL">jdbc:oracle:thin:@localhost:1521:xe</Set>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement