|
|
|
The Basic Steps to Connect Oracle and Java Program
|
By Budi Raharjo Jan 17, 2006
|
digg!
Print
Email to Friend
Note: This article was written for educational purpose only. Please refer to the related vendor documentation for detail.
|
|
|
The Basic Steps to Connect Oracle and Java Program
Here, I will show you what the steps to make a connection between Oracle
database and your Java program. To do this work, you need the JDBC (Java
DataBase Connectivity) driver. The file name of Oracle's JDBC driver is
classes12.zip or classes12.jar. By default, Oracle have included this driver at
the software installation process. Its location is in the ORACLE_HOME\jdbc\lib
directory. For example, if our ORACLE_HOME is C:\Oracle\Ora90 then the JDBC
driver will be placed in C:\Oracle\Ora90\jdbc\lib directory.
Make sure to set the Java CLASSPATH correctly. Here is the command line to do
it.
(Note: Assume the ORACLE_HOME is C:\Oracle\Ora90)
set CLASSPATH=.;C:\Oracle\Ora90\jdbc\lib\classes12.jar
or
set CLASSPATH=.;C:\Oracle\Ora90\jdbc\lib\classes12.zip
In addition, you can also set the CLASSPATH in your autoexec.bat file on your
Windows operating system.
Now, just follow these steps:
STEP 1. Import the java.sql package into your program.
------------------------------------------------------
import java.sql.*;
The syntax above will import all classes in the java.sql package. If you want to
import a few of them, you can write the syntax like this
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Connection;
It only imports the Driver, DriverManager, and Connection class into your Java
program.
STEP 2. Load the Oracle's JDBC driver.
--------------------------------------
There are two ways to load your JDBC driver. The first, use the forName() method
of java.lang.Class class.
Class.forName("oracle.jdbc.driver.OracleDriver");
And the second way is use the registerDriver() method of DriverManager class.
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
STEP 3. Create a Connection object.
-----------------------------------
To create a Connection object, use the getConnection() method of DriverManager
class. This method takes three parameters: URL, username, and password.
Connection conn =
DriverManager.getConnection(
"jdbc:oracle:thin:@mylaptop:1521:ORADB", // URL
"budiraharjo", // username
"SDDNBandung" // password
);
STEP 4. Create a Statement object.
----------------------------------
The Statement object can be created by call the createStatement() method of
Connection object that you made before. So, you can write the following code
Statement mystat = conn.createStatement();
STEP 5. Execute your SQL statement.
-----------------------------------
After you create a Statement object successfully, you can execute a query
(SELECT statement) from your Java program using the executeQuery() method of
Statement class. The result of execution process will be stored in ResultSet
object, so you need to declare an object of ResultSet first. Here is the code.
ResultSet rs = mystat.executeQuery("select custno, custname from customer");
STEP 6. Display your data.
--------------------------
The next step is display your data using the looping control.
while (rs.next()) {
System.out.println(
rs.getInt(1) + // first column
"\t" + // the horizontal tab
rs.getString(2) // second column
);
}
STEP 7. Close your statement and connection.
--------------------------------------------
mystat.close();
conn.close();
Here is the complete code.
/*********************************************************************
* File name : SimpleOraJava.java
* Author : Budi Raharjo (PT. Sigma Delta Duta Nusantara, Bandung)
* Blog :
http://mbraharjo.blogspot.com
*
*********************************************************************/
import java.sql.*;
class SimpleOraJava {
public static void main(String args[]) throws SQLException {
DriverManager.registerDriver(
new oracle.jdbc.driver.OracleDriver()
);
String serverName = "mylaptop";
int port = 1521;
String user = "budi";
String password = "SDDNBandung";
String SID = "ORADB";
String URL = "jdbc:oracle:thin:@" + serverName + ":" + port + ":" + SID;
Connection conn = DriverManager.getConnection(URL, user, password);
String SQL = "SELECT CUSTNO, CUSTNAME FROM CUSTOMER";
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(SQL);
while (rs.next()) {
System.out.println(
rs.getInt(1) +
"\t" +
rs.getString(2)
);
}
stat.close();
conn.close();
}
}
|
Comments/Reviews on this article: |
deepak sony Apr 03, 2006 |
very nice |
vikas Feb 02, 2006 |
Very clearly written article. Thank for sharing knowledge with us. |
siddharth chougule Jan 25, 2009 |
hi..can i connect oracle 9i to sdk1.4 i m using appserver with which i want to connect servlets to oracle 9i...i m not able to do tat i m using local host 8080 to run my servlet application...if it possible plz some one help me out with detailed explaination ....thank u |
siddharth chougule Jan 25, 2009 |
hi..can i connect oracle 9i to j2ee sdk1.4 i m using appserver with which i want to connect servlets to oracle 9i...i m not able to do tat i m using local host 8080 to run my servlet application...if it possible plz some one help me out with detailed explaination ....thank u
|
vicky jain Jun 30, 2008 |
good work
|
vicky jain Jun 30, 2008 |
hi buddi...plz help me out wid this problem....i tried the steps u told but its not working....the code is as follows
import java.awt.*;
import java.sql.*;
public class DbConn
{
Statement s;
Connection c;
public DbConn()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
//c=DriverManager.getConnection("jdbc:odbc:ooad:@god:5620:orcl1","vicky","turion64");
c=DriverManager.getConnection("jdbc:oracle:thin:@god:5620:orcl1","vicky","turion64");
s=c.createStatement();
//s.execute("create table student (id number,name text(15),addr text(30))");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String a[])
{
DbConn d=new DbConn();
}
}
it is giving me an error saying the network adapter could not establish the connection
....plz help asap....plzzzzz |
Arindam Ray Mar 16, 2006 |
Very Nice |
Vigyan Kaushik Mar 22, 2006 |
Thanks for great article Budi. |
supratim chaudhury Mar 23, 2006 |
This is a very simple article and of great help
very easily understood and thanks to Budi for this article |
|
About author:
Budi Raharjo is a professional software engineer from Indonesia (PT. Sigma Delta Duta Nusantara, Bandung). He is an author of C++, C++Builder, Pascal, and Oracle books in his country.
|
|
|