Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
JDBCUtil is a class I wrote that contains useful utility methods that you will add to as and when you learn different concepts in this book. In this chapter, you learned how to obtain a connection to a database and release resources associated with Connection, Statement, and ResultSet objects. These are ideal candidates for being incorporated in the JDBCUtil class for use in code written in later chapters.
I have put the JDBCUtil class in the package book.util. You can download this class from the Downloads area of the Apress website (http://www.apress.com). Let's look at the method getConnection() in this class, which takes as parameters a username, password, and database name (SID or service name):
public static Connection getConnection( String username,
String password, String dbName)
throws SQLException
{
OracleDataSource ods = null;
Connection connection = null;
ods = new OracleDataSource();
// set the properties that define the connection
ods.setDriverType ( "thin" ); // type of driver
ods.setServerName ( "rmenon-lap" ); // database server name
ods.setNetworkProtocol("tcp"); // tcp is the default anyway
ods.setDatabaseName(dbName); // Oracle SID
ods.setPortNumber(1521); // my 10g listener port number
ods.setUser(username); // username
ods.setPassword(password); // password
System.out.println( "URL:" + ods.getURL());System.out.flush();
connection = ods.getConnection();
connection.setAutoCommit( false );
return connection;
}