How to connect to in-memory Play Framework database using JDBC?

I use the in-memory database that comes with the Play Framework when I have db=mem in the configuration file for development.

How to connect to this database using JDBC? not JPA, which is the standard way.

I tried this method in the controller:

 public static void addToDB() { try { Connection conn = DriverManager.getConnection("jdbc:h2:mem:play"); Statement stmt = conn.createStatement(); stmt.execute(sql); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } 

But I get an error that I need to provide a username and password:

 org.h2.jdbc.JdbcSQLException: Wrong user name or password [8004-149] 

If I am in the web console on /@db , then the username sa and there is no password.


Now I logged in via the /@db interface and created a users table.

Then I connected to the database in the controller method and used this jdbc line: jdbc:h2:mem:play:sa , and then tried to insert the users into the table, but I get this error message:

 org.h2.jdbc.JdbcSQLException: Table "USERS" not found; SQL statement: 

How do I connect to a database in H2 memory in the Play Framework using JDBC?

+6
source share
2 answers

DB.getConnection() , should complete the job.

+8
source

Try:

 Connection conn = DriverManager.getConnection("jdbc:h2:mem:play", "sa", ""); 

because, as you wrote, "the username sa is used and the password does not exist."

+6
source

Source: https://habr.com/ru/post/892870/


All Articles