Java Reference
In-Depth Information
databases within Java applications. Even if you will not work directly with the JDBC
API, it is good to know these foundational basics.
13-5.
Simplifying
Connection
Manage-
ment
Problem
Your application requires the use of a database, and in order to work with the database,
you need to open a connection for each interaction. Rather than code the logic to open
a database connection every time you need to access the database, you want to use a
single class to perform that task.
Solution
Write a class to handle all the connection management within your application. Doing
so will allow you to call that class in order to obtain a connection, rather than setting up
a new Connection object each time you need access to the database. Perform the
following steps to set up a connection management environment for your JDBC applic-
ation:
1.
Create a class named CreateConnection.java that will encap-
sulate all the connection logic for your application.
2.
Create a PROPERTIES file to store your connection information.
Place the file somewhere on your CLASSPATH so that the
CreateConnection class can load it.
3.
Use the CreateConnection class to obtain your database connec-
tions.
The following code is a listing of the CreateConnection class that can be used
for centralized connection management:
import java.io.File;
import java.io.IOException;
Search WWH ::




Custom Search