?
Solved

Using JDBC

Posted on 2002-05-07
19
Medium Priority
?
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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