play框架 多个MySQL,游戏框架中的多个数据库

在尝试使用Play Framework 1.2.4建立到另一台服务器上不同数据库的第二个连接时遇到问题。官方1.2.3的配置不适用于1.2.4版本。通过纯JDBC方式实现了跨数据库查询的功能,提供了一个示例代码片段来展示如何创建连接、预编译SQL、执行查询以及关闭资源。这个解决方案适用于需要从多个数据库读取数据的情况。
摘要由CSDN通过智能技术生成

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.

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()

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...

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.

Thanks!

解决方案

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(...);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值