Goodnight. Tell me please, has anyone encountered such a problem that in the H2 database it is impossible to create a TABLE ?

Actually method :

 public static void maketable(){ Statement statement = null; try { statement = conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } try { statement.executeQuery("CREATE TABLE SETTINGS_BACKUP (\n" + " PARAM CLOB,\n" + " VALUE CLOB\n" + " )"); } catch (SQLException e) { e.printStackTrace(); } } 

Exception :

Database already in use: null. Possible solutions: close all other connections; use server mode.

Of course, I understand everything, but I don’t understand one thing ... Why is it necessary to disconnect everyone (and, by the way, it’s not clear how, or rather, which team?)? That is, if for example at the height of the working day I need to create some TABLE through the program, then I have to disconnect 10 clients from the database, so that they wait until TABLE is created and then reconnect everyone back to their places? And as a rule, 2-4 clients will not survive such frauds and just fly out.

  • where does this error occur? - Mikhail Vaysman

1 answer 1

Try this code here.

 public class H2 { public static void main(String[] args) { try { Connection conn = DriverManager.getConnection("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "", ""); Statement st = conn.createStatement(); st.execute("CREATE TABLE SETTINGS_BACKUP (PARAM CLOB, VALUE CLOB)"); PreparedStatement prep = conn.prepareStatement("INSERT INTO SETTINGS_BACKUP (PARAM, VALUE) VALUES (?,?)"); prep.setString(1, "a"); prep.setString(2, "b"); prep.executeUpdate(); prep = conn.prepareStatement("SELECT * FROM SETTINGS_BACKUP"); ResultSet rs = prep.executeQuery(); while (rs.next()) { System.out.println("PARAM " + rs.getBlob("PARAM") + " VALUE " + rs.getBlob("VALUE")); } } catch (Exception e) { e.printStackTrace(); } } } 

It works for me without errors. The table is created and the values ​​fall into it.