2012-02-03 31 views
9

Tôi đang cố thiết lập kết nối cơ sở dữ liệu thứ hai cho cơ sở dữ liệu khác trên máy chủ khác. Chúng tôi đang sử dụng khung chơi 1.2.4 và tôi đã tìm thấy tài liệu sau cho 1.2.3.Nhiều Cơ sở dữ liệu trong khung chơi

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

này không làm việc cho tôi vì vậy tôi đã làm một chút tìm kiếm hơn và tìm thấy các bài viết sau. Bài viết này nói với tôi rằng cấu hình ở trên bị rò rỉ từ các chi nhánh 1.3 thạc sĩ và sẽ có mặt trong tương lai ...

JPA.getJPAConfig method not found on Play's API

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

bất cứ ai có thể cho tôi một cách để làm một số đơn giản truy vấn đến cơ sở dữ liệu khác? Tôi nghĩ rằng tôi không phải là người duy nhất muốn sử dụng nhiều cơ sở dữ liệu.

Cảm ơn!

+0

Đối với các truy vấn để db khác trên cùng một máy chủ im sử dụng kiểm tra Danh sách này = JPA.em(). createNativeQuery ("SELECT * FROM other_db..TABLE"). getResultList(); trong sybase – n4cer

Trả lời

7

Để thỉnh thoảng đọc dữ liệu từ cơ sở dữ liệu khác, bạn cũng có thể sử dụng đồng bằng JDBC cũ:

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

Làm việc như một sự quyến rũ, cảm ơn! – dreampowder

1

Đó là cách tôi kết nối với cơ sở dữ liệu khác ngay bây giờ cho đến khi có giải pháp khác.

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(); 
} 
Các vấn đề liên quan