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
   System.out.println("Driver Loaded");
   Connection con=DriverManager.getConnection("jdbc:odbc:oradsn","system","admin");
  System.out.println("Connection established");
  System.out.println("connection not established");

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...

About Author

My Photo
Muralidhar Nayani
Hi this is Muralidhar, i am living in Chennai. I have been working in Java and JEE technologies. I like to share the java programs and materials from different institutes and free eBooks to download.
View my complete profile

Random Posts

© javabynataraj.blogspot.com from 2009 - 2014. All rights reserved.