Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

How can I connect to this database using JDBC? and not the JPA that is the default way.

I have tried with this method in my 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 message that I need to provide an username and password:

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

If I visit the web-console on /@db the username sa is used and no password.


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

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

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

How should I connect to the in-memory H2 database in Play Framework using JDBC?

like image 467
Jonas Avatar asked Jul 15 '11 23:07

Jonas


2 Answers

DB.getConnection(), should do the job.

like image 126
niels Avatar answered Nov 05 '22 20:11

niels


Try:

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

because as you wrote, "the username sa is used and no password."

like image 30
Thomas Mueller Avatar answered Nov 05 '22 19:11

Thomas Mueller