Pre-Requisites:
- jdk1.6.0_11
- Oracle 10g
- ojdbc14.jar
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 inE:\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();
}
}
}
*******************************************************************************
Ηellο to eveгy body, it's my first pay a quick visit of this web site; this weblog includes amazing and actually fine information for readers.
ResponderEliminarmy site; comparatif ssd
Excellent items from you, man. I've be aware your stuff previous to and you are just too magnificent. I really like what you have received here, certainly like what you're stating and the best
ResponderEliminarway in which you are saying it. You make it enjoyable and
you continue to take care of to stay it wise. I can not wait to learn far
more from you. That is really a great web site.
My website: voyance gratuite
Wow, superb blog layout! How long have you been blogging for?
ResponderEliminaryou make blogging look easy. The overall look of your website
is great, as well as the content!
Also visit my homepage; voyance par telephone (http://www.irisvoyance.com)
Great post. I will be experiencing a few of these issues as well.
ResponderEliminar.
Feel free to surf to my site :: voyance par telephone