Test a JDBC connection to Oracle database

Many tools use JDBC to talk SQL to a database server. To verify your Oracle database’s ability to respond to JDBC calls, compile the following small Java program into a file called JdbcCheckup.java.

// import the Oracle JDBC drivers
import oracle.jdbc.pool.OracleDataSource;

// You need to import the java.sql package to use JDBC
import java.sql.*;

// We import java.io to be able to read from the command line
import java.io.*;

class JdbcCheckup
{
public static void main (String args [])
throws SQLException, IOException
{

// Create an OracleDataSource and set URL
OracleDataSource ods = new OracleDataSource();
ods.setURL(“jdbc:oracle:thin:system/[email protected]:1525:apodevs”);

System.out.print (“Connecting to the database…”);
System.out.flush ();

// Connect to the database

System.out.println (“Connecting…”);
Connection conn = ods.getConnection();

System.out.println (“connected.”);

System.out.println (“Your JDBC configuration is correct.”);

// Close the connection
conn.close();
}
}

Before this program will compile, you need three things:

1) Oracle credentials:
The name of the database instance, the port it is listening on, the system account password, and the Oracle server name. These variables need to be placed in the JDBC connection string URL in the Java code. The syntax of a JDBC URL for Oracle is:

jdbc:oracle:thin:system/@::

In practice, it might look like this:
jdbc:oracle:thin:system/[email protected]:1525:apodevs

2) You will need to be running a JDK on the machine where you will be running this Java code. Any version will do.

3) Download the Oracle thin JDBC driver called “classes12.jar” from Oracle’s website:
http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/index.html

Once downloaded, place the JAR file in the jre|lib|ext directory of your installed JDK.

On Linux, place the JAR file in: /usr/java/jdk1.5.0_07/jre/lib/ext/
On Windows, place the JAR file in: C:Program FilesJavajdk1.6.0jrelibext

Now compile the .java file into a working class file: javac JdbcCheckup.java

Once compiled, you can run the program using the “java JdbcCheckup” command.