Class.forName("org.h2.Driver");
Connection connection = DriverManager.getConnection("jdbc:h2:mem:;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;");
Connection connection2 = DriverManager.getConnection("jdbc:h2:mem:;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;");
Statement s = connection.createStatement();
s.execute("create schema testdb");
s.execute("create table testdb.testt(id bigint not null primary key auto_increment, a int)");
PreparedStatement ps = connection.prepareStatement("insert into testdb.testt values (111, 33)", Statement.RETURN_GENERATED_KEYS);
int in = ps.executeUpdate();
System.out.println("use connection1 test:" + tableExists(connection, "testdb", "testt"));
System.out.println("use connection2 test:" + tableExists(connection2, "testdb", "testt"));
public static boolean tableExists(Connection conn, String dbName, String tableName) throws SQLException {
String[] types = {"TABLE"};
ResultSet rs = null;
try {
rs = conn.getMetaData().getTables(null, dbName, tableName, types);
if(rs.next()) {
return true;
}
else {
return false;
}
}
finally {
if (rs != null) {
rs.close();
}
}
}
Sometimes multiple connections to the same in-memory database are required
jdbc:h2:mem:jdbc:h2:mem:someDatabaseNamejdbc:h2:mem:someDatabaseName but my problem is our code will create some database dynamically during runtime at same vm. SELECT * FROM SCHEMA1.TABLE1CREATE SCHEMA schemaName