Thursday, 17 January 2013

How to Setup Oracle Database Connectivity with Java using JDBC

Hai dude Now i am going to Share about how we can connect Oracle database with our java program. here i will use Type 2 JDBC Connection..

Step 1:
         Download and Install Oracle
Step 2:
         Next download "ojdbc14.jar" file.. You can download here(download)
Step 3:
         Download Eclipse IDE

Step 4:
         Create new Java Project in Eclipse
Step 5:
          Setup ojdbc.14 jar file into Eclipse project folder build path.
          (You can refer here how to set build path in eclipse)
Step 6:
         Create new table in Oracle and enter sample data into Oracle table
         (You can refer here how to create new table in oracle)

Step 7:
         Create new Class in Eclipse and copy and paste below coding into that.
         Set class name as "OracleEX"

Program:
OracleEx.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class OracleEx {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Connection con=null;
  Statement st;
  ResultSet rs;
  try
  {
  Class.forName("oracle.jdbc.driver.OracleDriver");
  System.out.println("Driver Loaded");
  }
  catch(Exception e)
  {
   System.out.println("Driver Problem");
  }
  try
  {
  con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","testuser","test");
  System.out.println("Connection connected");
  }
  catch(Exception e)
  {
   System.out.println("Connection failed");
  }
  try
  {
  st=con.createStatement();
  rs=st.executeQuery("select * from test");
  while(rs.next())
  {
   System.out.println(rs.getString("Name"));
  }
  }
  catch(Exception e)
  {
   System.out.println("retriving problem");
  }
  }
}
Step 8
Now you will get data from database..


2 comments:

  1. It gives following message:... Please help...

    Prints the ASM code to generate the given class.
    Usage: ASMifierClassVisitor [-debug]

    ReplyDelete
    Replies
    1. its working fine for me.. may i know your system configuration

      Delete