Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "jdbc:h2:tcp://localhost/~/test"
- "jdbc:h2:tcp://localhost/~/"
- import java.sql.*;
- public class TestH2 {
- public static void main(String[] a) throws Exception {
- try (Connection conn = DriverManager.getConnection("jdbc:h2:./test", "sa", "")) {
- System.out.println(conn);
- String createTable = "CREATE TABLE IF NOT EXISTS TEST_TABLE(ID INT, NAME VARCHAR(255))";
- conn.createStatement().executeUpdate(createTable);
- String insertStmnt = "INSERT INTO TEST_TABLE VALUES(1, 'CAFEBABE');";
- int count = conn.createStatement().executeUpdate(insertStmnt);
- System.out.printf("inserted rows: %d%n", count);
- }
- // will fail with org.h2.jdbc.JdbcSQLException: Wrong user name or password [28000-181]
- try (Connection conn = DriverManager.getConnection("jdbc:h2:./test", "", "")) {
- System.out.println(conn);
- String sql = "SELECT id, name FROM TEST_TABLE";
- ResultSet rs = conn.createStatement().executeQuery(sql);
- while (rs.next()) {
- System.out.printf("id: %d name: %s%n", rs.getInt("ID"), rs.getString("NAME"));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement