多个数据库在玩框架 [英] Multiple Databases in play framework
问题描述
我正在尝试与另一台服务器上的另一个数据库建立第二个数据库连接.我们正在使用 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屋!