Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. private static Connection getDBConnection() {
  2.  
  3. Connection dbConnection = null;
  4. try {
  5.  
  6. Class.forName(DB_DRIVER);
  7. } catch (ClassNotFoundException e) {
  8. System.out.println(e.getMessage());
  9. }
  10.  
  11. try {
  12. dbConnection = DriverManager.getConnection(
  13. DB_CONNECTION, DB_USER, DB_PASSWORD);
  14. return dbConnection;
  15. } catch (SQLException e) {
  16. System.out.println(e.getMessage());
  17. }
  18. return dbConnection;
  19.  
  20. <?xml version="1.0" encoding="UTF-8"?>
  21. <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
  22. <Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
  23.  
  24. <New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
  25. <Arg><Ref refid="wac"/></Arg>
  26. <Arg>jdbc/DSTest</Arg>
  27. <Arg>
  28. <New class="oracle.jdbc.pool.OracleDataSource">
  29. <Set name="DriverType">thin</Set>
  30. <Set name="URL">jdbc:oracle:system:thin:@localhost:1521:xe</Set>
  31. <Set name="User">mylogin</Set>
  32. <Set name="Password">mypass</Set>
  33. </New>
  34. </Arg>
  35. </New>
  36.  
  37. </Configure>
  38.  
  39. <resource-ref>
  40. <description>My DataSource Reference</description>
  41. <res-ref-name>jdbc/DSTest</res-ref-name>
  42. <res-type>javax.sql.DataSource</res-type>
  43. <res-auth>Container</res-auth>
  44. </resource-ref>
  45.  
  46. private static Connection connTest() throws NamingException, SQLException {
  47.  
  48. Context initContext = new InitialContext();
  49. DataSource dataSource = (DataSource) initContext.lookup("java:comp/env/jdbc/DSTest");
  50. System.out.println(dataSource);
  51. System.out.println(dataSource.toString());
  52. Connection connection = dataSource.getConnection();
  53. return connection;
  54. }
  55.  
  56.  
  57. @POST
  58. @Path("/testinsert")
  59. @Consumes(MediaType.APPLICATION_JSON)
  60. public Response testinsert(SimpleJsonModel simpleJsonModel) {
  61.  
  62. String result = "JSON saved test: " + simpleJsonModel;
  63.  
  64. try {
  65. insertRecord(simpleJsonModel.getJsonId(), simpleJsonModel.getNumber());
  66. } catch (SQLException e) {
  67. System.out.println(e.getMessage());
  68. }
  69.  
  70. return Response.status(201).entity(result).build();
  71. }
  72.  
  73. private static void insertRecord(String val1, String val2) throws SQLException {
  74.  
  75. Connection dbConnection = null;
  76. Statement statement = null;
  77. String insertTableSQL = "insert into json.johny_table ( jsonId, numberJ) values ('" + val1 + "', '" + val2 + "')";
  78.  
  79. try {
  80. dbConnection = connTest();
  81. statement = dbConnection.createStatement();
  82. System.out.println(insertTableSQL);
  83. statement.executeUpdate(insertTableSQL);
  84. System.out.println("inserted");
  85.  
  86. } catch (SQLException e) {
  87. System.out.println(e.getMessage());
  88. } catch (NamingException e) {
  89. e.printStackTrace();
  90. } finally {
  91.  
  92. if (statement != null) {
  93. statement.close();
  94. }
  95. if (dbConnection != null) {
  96. dbConnection.close();
  97. }
  98. }
  99. }
  100. }
  101.  
  102. <Set name="URL">jdbc:oracle:system:thin:@localhost:1521:xe</Set>
  103.  
  104. <Set name="URL">jdbc:oracle:thin:@localhost:1521:xe</Set>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement