Multiple Databases in play framework

安稳与你 提交于 2019-12-04 02:35:06

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

That's how I'm connecting to other databases now until there is another solution.

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!