9

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

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

JPA.getJPAConfig method not found on Play's API

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.

Thanks!

1
  • For queries to another db on the same server im using this List test = JPA.em().createNativeQuery("SELECT * FROM other_db..TABLE").getResultList(); in sybase Commented Feb 3, 2012 at 8:37

2 Answers 2

7

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(...);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.