Solved

Using JDBC

Posted on 2002-05-07
19
159 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

733 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