【问题标题】:Multiple Databases in play framework游戏框架中的多个数据库
【发布时间】:2012-02-25 20:51:46
【问题描述】:

我正在尝试与另一台服务器上的另一个数据库建立第二个数据库连接。我们正在使用播放框架 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()

这对我不起作用,所以我进行了更多搜索并找到了以下文章。 这篇文章告诉我,上面的配置是从1.3的master分支泄露进来的,以后会有用...

JPA.getJPAConfig method not found on Play's API

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

谁能给我一种方法来对其他数据库进行一些简单的查询?我想我不是唯一一个想要使用多个数据库的人。

谢谢!

【问题讨论】:

  • 使用此列表查询同一服务器上的另一个数据库 test = JPA.em().createNativeQuery("SELECT * FROM other_db..TABLE").getResultList();在sybase中

标签: playframework multiple-databases


【解决方案1】:

偶尔从其他数据库读取数据,你也可以使用普通的旧 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(...);

【讨论】:

    【解决方案2】:

    这就是我现在连接到其他数据库的方式,直到有另一个解决方案。

    Connection c = null;
    try {
        ComboPooledDataSource ds = new ComboPooledDataSource();
        ds.setDriverClass("com.sybase.jdbc3.jdbc.SybDriver");
        ds.setJdbcUrl("jdbc:sybase:Tds:server:4100/db");
        ds.setUser("user");
        ds.setPassword("pass");
        ds.setAcquireRetryAttempts(10);
        ds.setCheckoutTimeout(5000);
        ds.setBreakAfterAcquireFailure(false);
        ds.setMaxPoolSize(30);
        ds.setMinPoolSize(1);
        ds.setMaxIdleTimeExcessConnections(0);
        ds.setIdleConnectionTestPeriod(10);
        ds.setTestConnectionOnCheckin(true);
    
        DB.datasource = ds;
        try {
            c = ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        } 
    
        } catch (PropertyVetoException e) {
            e.printStackTrace();
    }
    
    String sql = "SELECT * FROM TABLE";
    
    try {
        PreparedStatement pstmt = c.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
    
        while (rs.next()) {
            System.out.println(rs.getString(1)+"\n");
        }
    
        c.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-09
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 2012-04-01
      • 1970-01-01
      相关资源
      最近更新 更多