Powered by Blogger.

Oracle JDBC Connection Test

>> Saturday, June 22, 2013

Oracle JDBC Connection Test logoHere we are starting our first JDBC program after installing Oracle 10g XE in our local machine. Connecting Oracle database through java is little bit interesting. We need a medium to connect Oracle throug java for that java has provided four types of drivers in API. JDBC having four types of drivers to connect Databases.Here we are using Type 1 driver (JDBC-ODBC Bridge Driver) "sun.jdbc.odbc.jdbcOdbcDriver". This is common driver to connect all databases.
Oracle database through java is little bit interesting. We need a medium to connect Oracle throug java for that java has provided four

 In Our program we are using DriverManager class to get the Connection for Oracle database. For this we use getConnection( ) method. This is a Static method.
This driver is dependent on native libraries of the Operating System. Not recommended for all projects like applets and server client projects.

Here Oracle data source name is: oradsn
Type I driver : sun.jdbc.odbc.JdbcOdbcDriver
Oracle DB username: SYSTEM
password: admin

Program to Connect Oracle Database via Type I JDBC driver

import java.sql.Connection;
import java.sql.DriverManager;

public class  ConnTest
{
 public static void main(String[] args) throws Exception
 { 
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   System.out.println("------***-------");
   System.out.println("Driver Loaded");
   Connection con=DriverManager.getConnection("jdbc:odbc:oradsn","system","admin");
   System.out.println("------***-------");
   if(con!=null)
  System.out.println("Connection established");
   else
  System.out.println("connection not established");
 }
}

Output:
Oracle JDBC Connection Test Output_001

Oracle JDBC Connection Test Output_002

Oracle JDBC Connection Test Output_003

Yes, the Driver Loaded and Connection established Successfully. We didn't get any issues to connect with Oracle Database.

While running this program you may get some exceptions like NullPointerException or SQLException.

Different Exceptions while Connecting Oracle DB given below:

Invalid username/password:
Exception in thread "main" java.sql.SQLException: [Oracle][ODBC][Ora]ORA-01017: invalid username/password; logon denied

Wrong oracle dsn name:
Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC Driver Manage r] Data source name not found and no default driver specified

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.