use of org.h2.command.dml.Merge in project h2database by h2database.
the class TestGetGeneratedKeys method testBatchAndMergeInto.
/**
* Test for batch updates and MERGE INTO operator.
*
* @param conn
* connection
* @throws Exception
* on exception
*/
private void testBatchAndMergeInto(Connection conn) throws Exception {
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID BIGINT AUTO_INCREMENT, UID UUID DEFAULT RANDOM_UUID(), VALUE INT)");
PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (?), (?)", Statement.RETURN_GENERATED_KEYS);
prep.setInt(1, 1);
prep.setInt(2, 2);
prep.addBatch();
prep.setInt(1, 3);
prep.setInt(1, 4);
prep.addBatch();
prep.executeBatch();
ResultSet rs = prep.getGeneratedKeys();
ResultSetMetaData meta = rs.getMetaData();
assertEquals("BIGINT", meta.getColumnTypeName(1));
assertEquals("UUID", meta.getColumnTypeName(2));
rs.next();
assertEquals(1L, rs.getLong(1));
UUID u1 = (UUID) rs.getObject(2);
assertNotNull(u1);
rs.next();
assertEquals(2L, rs.getLong(1));
UUID u2 = (UUID) rs.getObject(2);
assertNotNull(u2);
rs.next();
assertEquals(3L, rs.getLong(1));
UUID u3 = (UUID) rs.getObject(2);
assertNotNull(u3);
rs.next();
assertEquals(4L, rs.getLong(1));
UUID u4 = (UUID) rs.getObject(2);
assertNotNull(u4);
assertFalse(rs.next());
assertFalse(u1.equals(u2));
assertFalse(u2.equals(u3));
assertFalse(u3.equals(u4));
prep = conn.prepareStatement("MERGE INTO TEST(ID, VALUE) KEY(ID) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS);
prep.setInt(1, 2);
prep.setInt(2, 10);
prep.execute();
rs = prep.getGeneratedKeys();
assertFalse(rs.next());
prep.setInt(1, 5);
prep.executeUpdate();
rs = prep.getGeneratedKeys();
rs.next();
assertEquals(UUID.class, rs.getObject(1).getClass());
assertFalse(rs.next());
stat.execute("DROP TABLE TEST");
}
use of org.h2.command.dml.Merge in project h2database by h2database.
the class MixedMode method main.
/**
* This method is called when executing this sample application from the
* command line.
*
* @param args the command line parameters
*/
public static void main(String... args) throws Exception {
// start the server, allows to access the database remotely
Server server = Server.createTcpServer("-tcpPort", "9081");
server.start();
System.out.println("You can access the database remotely now, using the URL:");
System.out.println("jdbc:h2:tcp://localhost:9081/~/test (user: sa, password: sa)");
// now use the database in your application in embedded mode
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "sa");
// some simple 'business usage'
Statement stat = conn.createStatement();
stat.execute("DROP TABLE TIMER IF EXISTS");
stat.execute("CREATE TABLE TIMER(ID INT PRIMARY KEY, TIME VARCHAR)");
System.out.println("Execute this a few times: " + "SELECT TIME FROM TIMER");
System.out.println("To stop this application " + "(and the server), run: DROP TABLE TIMER");
try {
while (true) {
// runs forever, except if you drop the table remotely
stat.execute("MERGE INTO TIMER VALUES(1, NOW())");
Thread.sleep(1000);
}
} catch (SQLException e) {
System.out.println("Error: " + e.toString());
}
conn.close();
// stop the server
server.stop();
}
Aggregations