<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
DataSource dataSource = new JdbcDataSource();
dataSource.setURL("jdbc:h2:" + path);
DataSource dataSource = new JdbcDataSource();
dataSource.setURL("jdbc:h2:mem:test");
try(Connection connection = dataSource.getConnection()) {
...
}
Connection.setAutoCommit commit rollback
try(Connection connection = dataSource.getConnection()) {
connection.setAutoCommit(false);
try {
...
connection.commit();
} catch(...) {
connection.rollback();
}
}
TRANSACTION_READ_COMMITTED see modification from other transactions
TRANSACTION_REPEATABLE_READ don't see modification from other transactions
Connection.createStatement() Connection.prepareStatement()
Connection connection = ...
String sqlQuery = """
INSERT INTO FOO (id, name) VALUES (42, 'James Bond');
""";
try(Statement statement = connection.createStatement()) {
statement.executeUpdate(query);
}
connection.commit();
Connection connection = ...
String sqlQuery = """
INSERT INTO FOO (id, name) VALUES (?, ?);
""";
try(PreparedStement statement = connection.prepareStatement(query)) {
statement.setObject(1, 42);
statement.setObject(2, "James Bond");
statement.executeUpdate();
}
connection.commit();
Connection connection = ...
String query = """
CREATE TABLE FOO (
id BIGINT,
name VARCHAR(255),
PRIMARY KEY (id)
);
""";
try(Statement statement = connection.createStatement()) {
statement.executeUpdate(query);
}
connection.commit();
Connection connection = ...
String sqlQuery = """
INSERT INTO FOO (id, name) VALUES (?, ?);
""";
try(PreparedStatement statement = connection.prepareStatement(sqlQuery)) {
statement.setObject(1, 42);
statement.setObject(2, "James Bond");
statement.executeUpdate();
}
connection.commit();
Connection connection = ...
String sqlQuery = """
MERGE INTO FOO (id, name) VALUES (?, ?);
""";
try(PreparedStatement statement = connection.prepareStatement(sqlQuery)) {
statement.setObject(1, 42);
statement.setObject(2, "James Bond");
statement.executeUpdate();
}
connection.commit();
String query = """
CREATE TABLE FOO (
id BIGINT AUTO_INCREMENT,
name VARCHAR(255),
PRIMARY KEY (id)
);
""";
Connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)
Connection connection = ...
String sqlQuery = """
INSERT INTO FOO (name) VALUES (?);
""";
try(PreparedStatement statement = connection.prepareStatement(sqlQuery, Statement.RETURN_GENERATED_KEYS)) {
statement.setObject(1, 42);
statement.setObject(2, "James Bond");
statement.executeUpdate(query);
try(ResultSet resultSet = statement.getGeneratedKeys()) {
if (resultSet.next()) {
Long key = (Long) resultSet.getObject(1);
...
}
}
}
connection.commit();
PreparedStatement.executeQuery
Connection connection = ...
String sqlQuery = """
SELECT (id, name) FROM FOO WHERE name = ?;
""";
try(PreparedStatement statement = connection.prepareStatement(sqlQuery)) {
statement.setObject(1, "James Bond");
try(ResultSet resultSet = statement.executeQuery()) {
while(resultSet.next()) {
Long id = (Long) resultSet.getObject(1);
String name = (String) resultSet.getObject(2);
...
}
}
}
connection.commit();