播放框架中的多个数据库

n4c*_*cer 9 multiple-databases playframework

我正在尝试与另一台服务器上的另一个数据库建立第二个数据库连接.我们正在使用play framework 1.2.4,我找到了1.2.3的以下文档.

http://www.playframework.org/documentation/1.2.3/model#multiple

application.conf:
db_other.url=jdbc:mysql://localhost/test
db_other.driver=com.mysql.jdbc.Driver
db_other.user=root
db_other.pass=

Connection conn = DB.getDBConfig("other").getConnection()
Run Code Online (Sandbox Code Playgroud)

这对我没有用,所以我做了一些搜索并找到了以下文章.这篇文章告诉我,上面的配置从1.3主分支泄露,将来可用...

在Play的API上找不到JPA.getJPAConfig方法

https://play.lighthouseapp.com/projects/57987/tickets/706

任何人都可以给我一个方法来对其他数据库做一些简单的查询吗?我想我不是唯一想要使用多个数据库的人.

谢谢!

fde*_*ert 7

要偶尔从其他数据库读取数据,您还可以使用普通的旧JDBC:

    Connection c = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    try {
        String url = "YourJdbcUrl";
        Class.forName("YourDriver").newInstance();
        c = DriverManager.getConnection(url, "XXX", "XXX");
        pstmt = c.prepareStatement("SELECT * FROM TABLE");
        rs = pstmt.executeQuery();
        while (rs.next()) {
            // Fill your data into Play Model instances here.
        }

    }catch(Exception e){
        e.printStackTrace();
    } finally {
        try { if (rs != null) rs.close(); } catch (Exception e) {};
        try { if (pstmt != null) pstmt.close(); } catch (Exception e) {};
        try { if (c != null) c.close(); } catch (Exception e) {};
    }

    render(...);
Run Code Online (Sandbox Code Playgroud)