이것은 내 jboss/deploy/postgres-ds.xml 파일입니다. 연결 URL, 사용자 이름 및 비밀번호가 여기에 제공됩니다. 내 서블릿에서이 데이터베이스에 어떻게 연결해야합니까? 이 후, 매번 수행해야하는 경우JBoss에서 데이터베이스에 연결 하시겠습니까?
이Connection conn =null; // Create connection object
String database = "postgres"; // Name of database
String user = "postgres"; //
String password = "qwerty";
String url = "jdbc:postgresql://localhost:5432/" + database;
ResultSet rs = null;
ResultSetMetaData rsm = null;
try{
Class.forName("org.postgresql.Driver").newInstance();
//.newInstance()
} catch(Exception e)
{
System.err.println(e);
}
try{
conn = DriverManager.getConnection(url, user, password);
}catch(SQLException se)
{
System.err.println(se);
}
왜 포스트 그레스-ds.xml 파일의 URL, 사용자 이름과 암호를 입력 :
<local-tx-datasource>
<jndi-name>PostgresDS</jndi-name>
<connection-url>jdbc:postgresql://localhost:5432/postgres</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<user-name>postgres</user-name>
<password>qwerty</password>
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
</local-tx-datasource>
나는 모든 서블릿이 같은 연결을 확보해야 ?
"java.sql.Connection의 콘 = ds.getConnection (
여기에 좋은 모두 방법을 보여 니펫); ". 이 함수를 호출 할 때마다 다른 연결 객체가 반환됩니까? – suraj
시스템의 관점에서 "아니오 : 반드시 필요하지는 않습니다". JDBC 연결을 통해 DS를 사용하는 또 다른 이점은 DS를 사용하면 응용 프로그램 서버의 연결 풀링을 이용할 수 있다는 것입니다. – paulsm4
@Satya "javax.naming.Context ctx = (javax.naming.Context) ic.lookup ("java : ");" 이 라인은 어디에서 조회를합니까? 세 번째 줄은 PostGres-ds.xml 파일에서 조회를 수행합니다. 권리? – suraj