游戏框架中的多个数据库 [英] Multiple Databases in play framework

查看:145
本文介绍了游戏框架中的多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与另一个服务器上的另一个数据库建立第二个数据库连接.我们使用的是Play框架1.2.4,我发现了以下有关1.2.3的文档.

I'm trying to establish a second database connection to another database on another server. We're using play framework 1.2.4 and I found the following documentation for 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()

这对我不起作用,因此我进行了更多搜索,找到了以下文章. 本文告诉我,上述配置是从1.3 master分支泄漏出来的,将来将可用...

This didn't worked for me so I did a little more search and found the following article. This article told me that the above configuration leaked in from the 1.3 master branch and will be available in the future...

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

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

有人可以给我一种对另一个数据库进行一些简单查询的方法吗?我想我不是唯一想使用多个数据库的人.

Can anyone give me a way to do some simple queries to that other database? I think I'm not the only one who wants to use multiple databases.

谢谢!

推荐答案

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

To occasionally read data from other database, you can also use plain old 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(...);

这篇关于游戏框架中的多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆