· 9 years ago · Aug 03, 2017, 03:40 PM
1"jdbc:h2:tcp://localhost/~/test"
2
3"jdbc:h2:tcp://localhost/~/"
4
5import java.sql.*;
6
7public 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}