For Windows users only
Pre-Requisites:
- jdk1.6.0_11
- Oracle 10g
- ojdbc14.jar
If you want to access oracle10g via JDBC you need to set CLASSPATH
environment variable to find the ojdbc14.jar file provided by Oracle.
The file is located in:
E:\oracle\product\10.1.0\Db_1\jdbc\lib\ojdbc14.jar
beneath you Oracle home directory, or it’s available for download
from
http://technet.oracle.com
or
http://www.findjar.com/
If you are using older version of JDK(1.2 or below). You need to set
CLASSPATH for classes12.zip
E:\oracle\product\10.1.0\Db_1\jdbc\lib\classes12.zip
Step:1
Setting CLASSPATH
1. Right click the My Computer icon. In that click properties
2. You will see a window System Properties. In that window click
Advanced
3. You will see a button Environment Variables. Click that button.
4. In the popup window you will see User variables for XXXXXX (your
name) and System variable. Select the New button which is below the User
variables for XXXXX.
5. You will see the window New User Variable. In Variable name
type CLASSPATH and in Variable value type
.;E:\oracle\product\10.1.0\Db_1\jdbc\lib\ojdbc14.jar
The first entry must be a period, which denotes the current directory.
The second entry must be the directory for the ojdbc14.jar or
classess12.zip (jdk 1.2 or below).
If CLASSPATH is not set correctly, you will get a NoClassDefFoundError
error when you run a compile class.
NOTE
Be sure to use a version of the JDK that is compatible with the Oracle
release you are using. If you use a new release of the JDK with an
older release of Oracle’s drivers, you may encounter “access
violation” errors when executing your programs.
Step 2
The PATH environment variable should already be set for JDK
Step 3
JDK is already provided by Oracle itself(You can even use your
own JDK). If you install Oracle10g It comes automatically. It lies in
E:\oracle\product\10.1.0\Db_1\jdk(I have
installed oracle in E: in your case it might be c: or d: ….)
Setting PATH
1. Right click the My Computer icon. In that click properties
2. You will see a window System Properties. In that window click
Advanced
3. You will see a button Environment Variables. Click that button.
4. In the popup window you will see User variables for XXXXXX (your
name) and System variable. Select the New button which is below the User
variables for XXXXX.
5. You will see the window New User Variable. In Variable name
type PATH and in Variable value type
E:\oracle\product\10.1.0\Db_1\jdk\bin;
Click ok. You have finished setting PATH and CLASSPATH.
For further reading on path and classpath refer
Click Here To
Know How To Set Classpath
Click
Here To Know How To Install MySQL
Testing your Connection
Oracle provides a sample program called JdbcCheckup.java that you
can use to verify your JDBC configuration. This file may be in a zip
file (demo.zip on the E:\oracle\product\10.1.0\Db_1\jdbc\demo.zip.
After unzipping you will get
E:\oracle\product\10.1.0\Db_1\jdbc\demo\samples\generic\. You will need
to extract it before running the program. Go to the directory where the
sample file is, then compile and execute the JdbcCheckup.iava class:
javac JdbcCheckup.java
Java JdbcCheckup
NOTE
Java commands are case sensitive. When you execute
jdbcCheckup, you are prompted for usename, password, and connect string
for a database. That connection Information will be used to attempt a
connection; if successful, that attempt will return the following
output:
Connecting to the database...Connecting…
connected.
Hello World.
Your JDBC Installation is correct.
If you don't receive feedback telling you that your Installation
is correct, you need to check your configuration. Common problems
include incorrectly set environment variables (PATH and CLASSPATH) and
mismatched versions of database connection drivers. It you change the
environment variable values, you need to shut down and restart the
command windows for the changes to take effect.
If you are not able to find demo.zip then try the below
programs.
Sample programs
Sample program: 1
ConnectOracle.java
*******************************************************************************
/**
* ConnectOracle.java
*/
package com.javaworkspace.connectoracle;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* @author www.javaworkspace.com
*
*/
public class ConnectOracle {
public static void main(String[] args) {
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("Connecting to the database...");
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
Statement statement = connection.createStatement();
ResultSet resultset = statement.executeQuery("select 'Connected' from dual");
resultset.next();
String s = resultset.getString(1);
System.out.println(s);
statement.close();
connection.close();
} catch (Exception e) {
System.out.println("The exception raised is:" + e);
}
}
}
********************************************************************************
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
In the above line, username is “scott” and password is
“tiger”.
Thin is the JDBC driver
1521 is the default port number which the connection is to be
established and orcl is the database name.
Sample program: 2
ConnectOracle.java
*******************************************************************************
/**
* ConnectOracle.java
*/
package com.javaworkspace.connectoracle;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* @author www.javaworkspace.com
*
*/
public class ConnectOracle {
public static void main(String[] args) {
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "scott", "t");
Statement statement = connection.createStatement();
ResultSet resultSet = statement
.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
while (resultSet.next()) {
System.out.println("EMPLOYEE NAME:"
+ resultSet.getString("EMPNAME"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
*******************************************************************************