Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Databases in play framework

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!

like image 410
n4cer Avatar asked Feb 03 '12 07:02

n4cer


1 Answers

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(...);
like image 179
fdelsert Avatar answered Dec 29 '22 17:12

fdelsert