Java Reference
In-Depth Information
/**
* Demonstrates obtaining a connection via
a DataSource object
*
* @return
*/
public Connection getDSConnection() {
Connection conn = null;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource)
ctx.lookup(this.jndi);
conn = ds.getConnection();
} catch (NamingException | SQLException ex) {
ex.printStackTrace();
}
return conn;
}
}
Next, the following lines of text are an example of what should be contained in the
properties file that is used for obtaining a connection to the database. For this example,
the properties file is named db_props.properties :
host_name=your_db_server_name
db_name=your_db_name
username=db_username
password=db_username_password
port_number=db_port_number
#driver = derby or oracle
driver=db_driver
jndi=jndi_connection_string
Finally, use the CreateConnection class to obtain connections for your applic-
ation. The following code demonstrates this concept:
CreateConnection createConn = new CreateConnection();
try(Connection conn = createConn.getConnection();) {
performDbTask();
Search WWH ::




Custom Search