2012-02-03 2 views
9

다른 서버의 다른 데이터베이스에 두 번째 데이터베이스 연결을 설정하려고합니다. 우리는 플레이 프레임 워크 1.2.4를 사용 중이며 1.2.3에 대한 다음 설명서를 발견했습니다. 이것은 나를 위해 일하지 않았다재생 프레임 워크의 다중 데이터베이스

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

http://www.playframework.org/documentation/1.2.3/model#multiple

그래서 좀 더 검색을했고, 다음 문서를 발견했다. 이 문서에서는 위의 구성은 ...

JPA.getJPAConfig method not found on Play's API를 1.3 마스터 지점에서의 유출과 미래에 사용할 수 있다는 얘기

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

사람이 나에게 몇 가지 간단한을 할 수있는 방법을 제공 할 수 다른 데이터베이스에 대한 쿼리? 나는 여러 데이터베이스를 사용하고자하는 유일한 사람이 아니라고 생각합니다.

감사합니다.

+0

= JPA.em(). createNativeQuery ("SELECT * FROM other_db..TABLE"). getResultList(); sybase에서 – n4cer

답변

7

가끔 다른 데이터베이스에서, 당신은 또한 평범하고 오래된 JDBC를 사용할 수있는 데이터를 읽으려면 :이 목록 테스트를 사용하여 동일한 서버 메신저에있는 다른 DB에 쿼리의

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

매력처럼 작동합니다, 감사합니다! – dreampowder

1

다른 해결책이있을 때까지 다른 데이터베이스에 연결하는 방법입니다.

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(); 
} 
관련 문제