The previous article addressed the Oracle database creation. This article will handle the construction of a Java class for accessing the database.
We will be using NetBeans IDE 7.0.1, which may be downloaded from http://netbeans.org/community/releases/70/.
After installing NetBeans IDE 7.0.1, we must create a new "Java Class Library" project, which we will name HelperDB, because it will consist only of a single class for helping us access the database.
We also create a Java class, that we named DBAccessObj, in file DBAccessObj.java.
It is needed to include two or three (depending on the ORACLE version) JAR library classes to access the Oracle database by using a JDBC driver:
These jar files may be found in directory
oraclexe\app\oracle\product\10.2.0\server\jdbc\lib
being oraclexe the directory where Oracle XE has been installed.
Let's now create a package, which we name HelperDB, that will contain our class, and let's import package java.sql, which contains classes for connecting and sending SQL statements to the database:
package HelperDB;
import java.sql.*;
public class DBAccessObj{
private Connection conn;
public DBAccessObj()
{
}
}
As the only instance variable in our class, we shall create conn of "type" java.sql.Connection.
Actually, Connection is a Java interface, and so conn may contain an object of any type (class) that implements that interface. So, conn may contain an instance of any type of connection. The type will be given by the DriverManager according to the driver that is being used.
The class constructor will then be modified to create a database connection: