Connect to Oracle Database using JDBC

In this post we will see the sample code to connect to Oracle Database using JDBC drivers in java.
For connecting to Oracle database we will need the following
1. Connection URL
The format is jdbc:oracle:thin:@<SERVER_NAME>:<PORT>:<SID>
example:
jdbc:oracle:thin:@localhost:1525:kscodes

2. User Name
User Name of the database that we need to connect.

3. Password
Password of the database that we need to connect.

4. ojdbc.jar in our classpath. (we are using ojdbc12.jar) You can get this jar from ojdbc jar download

5. Oracle JDBC Driver Name
The driver used is oracle.jdbc.driver.OracleDriver

Steps: Connect to Oracle Database using JDBC

1. Register the JDBC Driver
Using the Class.forName(...) we can register the Oracle JDBC driver.

2. Create Connection using DriverManager
Using the DriverManager.getConnection(url,user,password) we can create the connection to the database.

Please remember to add the ojdbc.jar in the class path before running the code.

Example

Output

Exceptions to be caught

1. ClassNotFoundException
2. SQLException