Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. "jdbc:h2:tcp://localhost/~/test"
  2.  
  3. "jdbc:h2:tcp://localhost/~/"
  4.  
  5. import java.sql.*;
  6.  
  7. public class TestH2 {
  8.  
  9. public static void main(String[] a) throws Exception {
  10. try (Connection conn = DriverManager.getConnection("jdbc:h2:./test", "sa", "")) {
  11. System.out.println(conn);
  12. String createTable = "CREATE TABLE IF NOT EXISTS TEST_TABLE(ID INT, NAME VARCHAR(255))";
  13. conn.createStatement().executeUpdate(createTable);
  14. String insertStmnt = "INSERT INTO TEST_TABLE VALUES(1, 'CAFEBABE');";
  15. int count = conn.createStatement().executeUpdate(insertStmnt);
  16. System.out.printf("inserted rows: %d%n", count);
  17. }
  18.  
  19. // will fail with org.h2.jdbc.JdbcSQLException: Wrong user name or password [28000-181]
  20. try (Connection conn = DriverManager.getConnection("jdbc:h2:./test", "", "")) {
  21. System.out.println(conn);
  22. String sql = "SELECT id, name FROM TEST_TABLE";
  23. ResultSet rs = conn.createStatement().executeQuery(sql);
  24. while (rs.next()) {
  25. System.out.printf("id: %d name: %s%n", rs.getInt("ID"), rs.getString("NAME"));
  26. }
  27. }
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement