Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

oracle connection

  Asked By: Manju    Date: Jul 21    Category: Java    Views: 1199

can anyonehelp me.how can i connect to an oracle database in



6 Answers Found

Answer #1    Answered By: Della Simpson     Answered On: Jul 21

I am not sure about other OS'es, but with Windows this is what you need
to do.

1) Setup the Data Source Name (DSN) in the Control Panel. Go to the
"ODBC Data Sources" in the Control Panel (found under "Administrative
Tools" on Win2K/XP) Select "Add" and choose the driver. You can use
either the "Microsoft ODBC for Oracle" driver or the "Oracle ODBC
Driver" (which you will have only if you have oracle  software installed
on your system) Enter the Data Source Name, (can be any name you are
comfortable using) the username (you can use scott/tiger or whatever
username/password you have) and the server (server address if the
database is on a remote machine or leave empty if the database  is on the
same computer). Once you add this DSN, you will see it listed in the
main ODBC Data Source window.

2) Code your Java program to use JDBC for connecting to the database and
working with it.
First import the java.sql package into your program.
Create a String object with the url "jdbc:odbc:dsn" where dsn is the
name of the DS you created above.
Optionally, create Strings to store usernames and password.
Let Java load the driver you are going to use using the Class.forName()
Register the driver with the DriverManager class and open a connection
to the Oracle database.
Create Statement(used for SQL statements) , ResultSet(for holding the
results of a query)

I have included a simple program here.

import java.sql.*;

class sqldemo {

public static void main(String[] args) throws SQLException {

int stNo; // For storing the EmpNo
String stName; //For storing the EmpName
Connection conn;
Statement stmt;
ResultSet rs; //To store results from Oracle (returns a

// Load the driver. This uses the JDBC-ODBC Bridge driver. There
are more drivers for you to use with Oracle. This is the most basic.

//Register with DriverManager and open a connection  to Oracle


// Create a SQL Statement to be sent

// Pass a query to Oracle
rs=stmt.executeQuery("select * from emp"); // No semicolon
after the SQL statement

System.out.println("Empno "+"Name");

// Display the results
while(rs.next()) {
stNo=rs.getInt(1); //Converts the first column to
type int
stName=rs.getString(2); // Second column converted to

If you want to execute SQL statements such as CREATE, ALTER, INSERT,
etc., use the Stmt.executeUpdate() method.
Stmt.executeUpdate("Create table test(testID number(3), name

Of course, this is merely scratching the surface, so if you want to
learn more, try out a book. Almost every other book on Java should deal
with JDBC.

Answer #2    Answered By: Devrim Yilmaz     Answered On: Jul 21

why on earth would you use odbc at all in this, just use jdbc directly
forget about the registering of odbc sources and substitute these two
lines in the example


where [server] is your oracle  server
and [database] is your database  instance/schema

this will work on any OS

Answer #3    Answered By: Ella Brown     Answered On: Jul 21

where should i put the oracle  drivers?
(jar files). my program is a multithreading/socket server which will
be handling data manipulation connecting to remote databases using
gprs. i put the jar files in the jdk\lib\ext. in the net ide, under
the database, the connection  appeared and i tested the driver and it
says in the output, warning: no tables, no views. internal testing
incomplete. but when i right clicked the connection, and execute
command, i entered a simple query and it works. but when i run the
application, i put some printlns as markers. it did print until
but the println after the
String temp = rs.getString("tablename");
did not print at all.
how will i check if the connection has been made?
thanks a lot!!!!!

Answer #4    Answered By: Liam Bouchard     Answered On: Jul 21

actually thats what i did. but still won't work. i am using
oracle thin because my project is multithreading/socket server which
will handle data manipulation in different remote oracle  database.
i tried to trace the code, i put some println in it. i inserted it

while (rs.next()){
String temp = rs.getString("name");
System.out.println("after test");

the screen only displays the word "test", so meaning, it halts on the

i also tried to put

system.out.println("connection closed");

But it bypasses the println connection  closed. so i assumed there is
a connection.

are there any way i can check if i connected successfully?
can anyon help me figure this out? thanks a lot!!!!

Answer #5    Answered By: Hababah Younis     Answered On: Jul 21

sounds like it's hanging on the rs.getString(), which is strange
perhaps "name" is a reserved word - try naming the column something else
try rs.getString(1); - you are selecting name in your select
statement ?
your dba (presuming you have one and he will talk to you) wil be able to
tell about connections.

Answer #6    Answered By: Edfu Massri     Answered On: Jul 21

I don't know too much about IDEs since I just use text editors. I
set my CLASSPATH to point to the classes111 and classes12 files in
ORACLE_HOME/jdbc/lib and it has never failed me till date.
If anyone has an answer to where exactly the classes111.jar and
classes12.jar files should go, please let us know.

Didn't find what you were looking for? Find more on oracle connection Or get search suggestion and latest updates.