Solved

Using JDBC

Posted on 2002-05-07
19
155 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 38
Java Message handling in Service Layer 3 57
rhino JavaScript import, load 25 66
Java SE 8u111  Lot of stuff broke 11 54
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now