Solved

Using JDBC

Posted on 2002-05-07
19
154 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
Comment Utility
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
Comment Utility
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
Comment Utility
 Have a look here: http://www.orafaq.com/faqjdbc.htm

  Hope it helps.
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
which jar file is it ? whats the location i mean
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
yeah Driver Loaded got printed
0
 

Author Comment

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

Expert Comment

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

Author Comment

by:rahulkothari
Comment Utility
yeah
0
 
LVL 3

Expert Comment

by:rjackman
Comment Utility
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
Comment Utility
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
Comment Utility
i finally got it , maybe it was coz i had multiple entries in my PATH....thanx u all
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

743 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

12 Experts available now in Live!

Get 1:1 Help Now