Guest User

Untitled

a guest
Apr 25th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. package com.h2.examples;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. // H2 In-Memory Database Example shows about storing the database contents into memory.
  11.  
  12. public class H2MemoryDatabaseExample {
  13.  
  14. private static final String DB_DRIVER = "org.h2.Driver";
  15. private static final String DB_CONNECTION = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1";
  16. private static final String DB_USER = "";
  17. private static final String DB_PASSWORD = "";
  18.  
  19. public static void main(String[] args) throws Exception {
  20. try {
  21. insertWithStatement();
  22. insertWithPreparedStatement();
  23.  
  24. } catch (SQLException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28.  
  29. private static void insertWithPreparedStatement() throws SQLException {
  30. Connection connection = getDBConnection();
  31. PreparedStatement createPreparedStatement = null;
  32. PreparedStatement insertPreparedStatement = null;
  33. PreparedStatement selectPreparedStatement = null;
  34.  
  35. String CreateQuery = "CREATE TABLE PERSON(id int primary key, name varchar(255))";
  36. String InsertQuery = "INSERT INTO PERSON" + "(id, name) values" + "(?,?)";
  37. String SelectQuery = "select * from PERSON";
  38.  
  39. try {
  40. connection.setAutoCommit(false);
  41.  
  42. createPreparedStatement = connection.prepareStatement(CreateQuery);
  43. createPreparedStatement.executeUpdate();
  44. createPreparedStatement.close();
  45.  
  46. insertPreparedStatement = connection.prepareStatement(InsertQuery);
  47. insertPreparedStatement.setInt(1, 1);
  48. insertPreparedStatement.setString(2, "Jose");
  49. insertPreparedStatement.executeUpdate();
  50. insertPreparedStatement.close();
  51.  
  52. selectPreparedStatement = connection.prepareStatement(SelectQuery);
  53. ResultSet rs = selectPreparedStatement.executeQuery();
  54. System.out.println("H2 In-Memory Database inserted through PreparedStatement");
  55. while (rs.next()) {
  56. System.out.println("Id " + rs.getInt("id") + " Name " + rs.getString("name"));
  57. }
  58. selectPreparedStatement.close();
  59.  
  60. connection.commit();
  61. } catch (SQLException e) {
  62. System.out.println("Exception Message " + e.getLocalizedMessage());
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. } finally {
  66. connection.close();
  67. }
  68. }
  69.  
  70. private static void insertWithStatement() throws SQLException {
  71. Connection connection = getDBConnection();
  72. Statement stmt = null;
  73. try {
  74. connection.setAutoCommit(false);
  75. stmt = connection.createStatement();
  76. stmt.execute("CREATE TABLE PERSON(id int primary key, name varchar(255))");
  77. stmt.execute("INSERT INTO PERSON(id, name) VALUES(1, 'Anju')");
  78. stmt.execute("INSERT INTO PERSON(id, name) VALUES(2, 'Sonia')");
  79. stmt.execute("INSERT INTO PERSON(id, name) VALUES(3, 'Asha')");
  80.  
  81. ResultSet rs = stmt.executeQuery("select * from PERSON");
  82. System.out.println("H2 In-Memory Database inserted through Statement");
  83. while (rs.next()) {
  84. System.out.println("Id " + rs.getInt("id") + " Name " + rs.getString("name"));
  85. }
  86.  
  87. stmt.execute("DROP TABLE PERSON");
  88. stmt.close();
  89. connection.commit();
  90. } catch (SQLException e) {
  91. System.out.println("Exception Message " + e.getLocalizedMessage());
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. } finally {
  95. connection.close();
  96. }
  97. }
  98.  
  99. private static Connection getDBConnection() {
  100. Connection dbConnection = null;
  101. try {
  102. Class.forName(DB_DRIVER);
  103. } catch (ClassNotFoundException e) {
  104. System.out.println(e.getMessage());
  105. }
  106. try {
  107. dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
  108. return dbConnection;
  109. } catch (SQLException e) {
  110. System.out.println(e.getMessage());
  111. }
  112. return dbConnection;
  113. }
  114. }
Add Comment
Please, Sign In to add comment