开发者

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

开发者 https://www.devze.com 2023-03-20 21:29 出处:网络
I use the in-memory database that comes with Play Framework, when I have db=mem in the configuration file, for development.

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.clos开发者_如何转开发e();
    } 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?


DB.getConnection(), should do the job.


Try:

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号