Reputation: 1898
This is my jboss/deploy/postgres-ds.xml file. The connection url, username and password is given here. How do I obtain a connection to this database in my servlet.
<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>
Should I obtain the connection like this in every servlet :
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);
}
If this has to be done everytime, then why give the url, username and password in the postgres-ds.xml file?
Upvotes: 3
Views: 11009
Reputation: 61538
If you are working with JBoss, it is advisable to take advantage of the included EE APIs like JPA.
Thus you would not need to retype your connection information anywhere. Just let the container inject an EntityManager
into your servlet (provided you are using EE 6 with CDI) or create something like a DAO (without EE6).
You might want to take a look at this JPA example using Hibernate on JBoss.
Upvotes: 0
Reputation: 121649
No - using a "data source" in a J2EE app (like a JBoss-based app) and opening a standard JDBC connection (as you'd do in a simple Java application) are more or less mutually exclusive.
Your app would generally do one or the other. In your case, use the data source.
Here's a great snippet that illustrates both approaches: using a JNDI datasource, and opening a JDBC connection directly:
http://www.javapractices.com/topic/TopicAction.do?Id=127
/** Uses JNDI and Datasource (preferred style). */
static Connection getJNDIConnection(){
String DATASOURCE_CONTEXT = "java:comp/env/jdbc/blah";
Connection result = null;
try {
Context initialContext = new InitialContext();
if ( initialContext == null){
log("JNDI problem. Cannot get InitialContext.");
}
DataSource datasource = (DataSource)initialContext.lookup(DATASOURCE_CONTEXT);
if (datasource != null) {
result = datasource.getConnection();
}
else {
log("Failed to lookup datasource.");
}
}
catch ( NamingException ex ) {
log("Cannot get connection: " + ex);
}
catch(SQLException ex){
log("Cannot get connection: " + ex);
}
return result;
Upvotes: 2
Reputation: 8881
you can use DataSource to get Connection like
javax.naming.Context ic = new javax.naming.InitialContext();
javax.naming.Context ctx = (javax.naming.Context) ic.lookup("java:");
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("PostgresDS");
java.sql.Connection con = ds.getConnection();
Upvotes: 7