?
Solved

Using JDBC

Posted on 2002-05-07
19
Medium Priority
?
164 Views
Last Modified: 2010-03-31
I installed SDK 2.0 v 1.2.2 and Oracle 8i.
I need detailed info on how connect from Java to Oracle database. Considering i am starting from scratch.
0
Comment
Question by:rahulkothari
19 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6994136
it depends on the driver you decide to use... once you've made that decision then i'm sure we can get you on your way:

for a list of drivers, see:

http://industry.java.sun.com/products/jdbc/drivers
0
 

Author Comment

by:rahulkothari
ID: 6994151
well....what drivers already come with the stuff i have installed ?

i think jdbc:odbc comes with SDK
and Oracle gives its set of drivers. Just tell me how to work with these.
0
 
LVL 35

Expert Comment

by:girionis
ID: 6994183
 Have a look here: http://www.orafaq.com/faqjdbc.htm

  Hope it helps.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 18

Accepted Solution

by:
bobbit31 earned 200 total points
ID: 6994188
The latest SDK includes a JDBC-ODBC Bridge driver that makes most Open Database Connectivity (ODBC) drivers available to programmers using the JDBC API. JDBC-ODBC Bridge Driver describes the current status of this software. Note that the bridge driver included in the SDK is appropriate only for experimental use or when no other driver is available.

here's how to do it using the Oracle Thin Driver:

     
try {
   // Load the driver class (make sure this is in your classpath)
   Class.forName (oracle.jdbc.driver.OracleDriver).newInstance();
   Connection con = DriverManager.getConnection(jdbc:oracle:thin:@myHost:port:sID); // myHost = your server, port = port (default = 1521), sID = serverID

   Statement stmt = null;
   ResultSet rs = null;

   stmt = con.createStatement();
   rs = stmt.executeQuery(<strSQL>);

   while (rs.next()) {
      // do what you want w/ result set
   }

   rs.close();
   stmt.close();
} catch (Exception e) {
   // error handling
} finally {
   if (con != null) { con.close(); }
}

     
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6994192
also, this line:
Connection con = DriverManager.getConnection(jdbc:oracle:thin:@myHost:port:sID);

should be:
Connection con = DriverManager.getConnection(jdbc:oracle:thin:@myHost:port:sID, <user>, <password>);

if you want to include a user name and password
 
0
 

Author Comment

by:rahulkothari
ID: 6994313
well.....do i have to do anything to the CLASSPATH on my computer. And since i am using Win 2000 professional and my computer name is WTC , should i used WTC as "myhost".
0
 

Author Comment

by:rahulkothari
ID: 6994318
when i write this in my code :
c = DriverManager.getConnection("jdbc:oracle:thin:@WTC:1521:pappu", "scott", "tiger");

i get a error saying : no suitable driver
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6994320
> do i have to do anything to the CLASSPATH on my computer
yes, add the jar file containing oracle.jdbc.driver.OracleDriver to your classpath

> should i used WTC as "myhost".
yes
0
 

Author Comment

by:rahulkothari
ID: 6994340
which jar file is it ? whats the location i mean
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6994377
i don't use oracle, so just from looking around,

see if OracleDriver.class is in either:
classes111.jar or classes12.jar

(ie. jar -tf classes111.jar)
0
 

Author Comment

by:rahulkothari
ID: 6994396
import java.sql.*;
import java.util.*;

public class Connect {
        public static void main(String args[]) {
        Connection  c = null;
       
        try {
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                System.out.println("Driver Loaded");
                c = DriverManager.getConnection("jdbc:odbc:rahul","scott","tiger");
                                    System.out.println("Connection created");

                Statement st = c.createStatement();
                ResultSet rs = st.executeQuery("Select * from department");
                while(rs.next()) {
                        System.out.println("Connection created");
                              System.out.print(rs.getInt(1));
                        System.out.print("!!!!");
                        System.out.print(rs.getString(2));
                        System.out.print("!!!!");
                        System.out.print(rs.getString(3));
                        System.out.println();
                        System.out.println("----------------------------");
                }
                st.close();
                c.close();
        }catch(Exception e) {
                System.out.println(e);
        }
        }
}

               
thats my code , i get an error....no suitable driver.
I included the rt.jar file in the CLASSPATH too.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6994414
does "Driver Loaded" get printed?

also instead of
this: c = DriverManager.getConnection("jdbc:odbc:rahul","scott","tiger");
                                   
try: c = DriverManager.getConnection("jdbc:odbc:rahul";"scott";"tiger");


                                   
0
 

Author Comment

by:rahulkothari
ID: 6994427
yeah Driver Loaded got printed
0
 

Author Comment

by:rahulkothari
ID: 6994434
c = DriverManager.getConnection("jdbc:odbc:rahul";"scott";"tiger");
 gives a compile time error
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6994467
you do have the rahul dsn set up in odbc datasources right?
0
 

Author Comment

by:rahulkothari
ID: 6994476
yeah
0
 
LVL 3

Expert Comment

by:rjackman
ID: 6995605
well.. well.. well.. MR Rahul i suggest u should read about JDBC first and then ask for help

also if u r using the

jdbc:oracle:thin:@myHost:port:sID as ur URL for DB then ushould have the classessxxx.zip
in your classpath xxx stands for the version No depending on the JDK u are using
classes111.zip for JDK1.1.x
classes12.zip---->JDK1.2

Never use the ODBC driver its a low quality unreliable
pls read on the JDBC type of Drivers from following

http://scitec.uwichill.edu.bb/cmp/online/CS36L/jdbc/jdbcDrivers.html
cheers
RJ
0
 

Author Comment

by:rahulkothari
ID: 6996264
Dear Jackman,
I had tried all that stuff before but it didnt work , so i decided to start from scratch and it still doesnt work. The problem isnt that subtle , if you read the code that i have typed above , the following things happen when i run it :
Driver Loaded is printed.
And Then the computer sort of hangs...i have to kill the Dos - Prompt.

see if u make something out of this.
0
 

Author Comment

by:rahulkothari
ID: 6996350
i finally got it , maybe it was coz i had multiple entries in my PATH....thanx u all
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question