This blog explains how to connect to the Oracle10g database in Java.
(u can use netbeans as well then no need of classpath)
Connect To Oracle10g database In Java
There are 5 steps to create a connection with an Oracle10g database in Java.
- First register the driver class
- Create the connection
- Create a statement
- Execute queries
- Close the connection
The class.forName() method of the class registers the driver class.
Syntax
public static void forName(String classname) throws ClassNotFoundException
Example
Class.forName("oracle.jdbc.
2. Create the connection object
The getConnection() method of the DriverManager class establishes the connection with the Oracle database.
Syntax
public static Connection getConnection(String url) throws Exception
public static Connection getConnetion(String name, String url, String password)
Example
Connection connection=DriverManager.
Instead of localhost we provide our system name there.
Explanation:
Connection URL: The connection URL for the oracle10G database is jdbc:oracle:thin:@localhost:1521:xe where
jdbc is the API,
oracle is the database,
thin is the driver,
localhost is the server name on which oracle is running,
we may also use IP address,
1521 is the port number
and
XE is the Oracle service name.
3. Create the Statement object
The createStatement() method of the Connection interface is used to create the statement. This object is responsible for executing queries with the Oracle database.
Syntax
public Statement createStatement() throws Exception
Example
Statement statement=connection.
4. Execute the Queries
This method of the Statement interface executes the queries to the Oracle database. This method returns the object of ResultSet that can be used to get all the records of a table.
Syntax
public ResultSet executeQuery(String s) throws Exception
Example
ResultSet resultset=statement.
while(resultset.next());
{
System.out.println(resultset.
}
5. Close the connection object
By closing the connection object the statement and ResultSet will be closed automatically. The close() method of the Connection interface closes the connection.
Syntax
public void close() throws Exception
Example
connection.close();
Now, take an example that shows a connection with Oracle10g database
For making a Java application with the Oracle database, we need to use the above 5 steps to perform database connectivity. In this example, we are using Oracle10g as the database. So we need to know the following information for the Oracle database.
Driver class
For Oracle; the driver class is in oracle.jdbc.driver.
Connection URL
For Oracle10g the URL is "jdbc:oracle:thin:@localhost:
username
The default username for the Oracle database is SYSTEM.
Password
This is user define given at the time of installing the Oracle database.
Now, start creating a connection with the Oracle10g database.
Create a table
Now open the Run SQL command line and write the following:
create table student(s_id number(5), s_name varchar2(35),s_age number(3));
Our table is created, now insert some data into it.
insert into student values(10,'Sandeep',22);
insert into student values(11,'Rahul',20);
Now we have inserted two rows into the student table that we have to display using Java.
To connect a Java application with the Oracle database we need the ojdbc14.jar file.
There are two ways to load this jar file
- paste the jar ojdbc14.jar in C:\Program Files\Java\jre7\lib\ext folder.
- set classpath for this jar file.
First search the ojdbc14.jar file (Like C:\oraclexe\app\oracle\
2. Set the classpath
Go to environment variable then click on the new tab. In the variable name write the classpath and in the variable value paste the path to the ojdbc14.jar (like C:\oraclexe\app\oracle\
See the following image:
Display the student record The following example Java file displays the student record.
Example OracleConnectionEx.java
This example fetches all the records of the student table.
import java.sql.*;
class OracleConnectionEx
{
public static void main(String args[]) throws Exception
{
try
{
Class.forName("oracle.jdbc. driver.OracleDriver");
Connection connection=DriverManager.getCo nnection("jdbc:oracle:thin:@ mcndesktop07:1521:xe","system" ,"sandeep");
Statement statement=connection.createSta tement();
ResultSet resultset=statement.executeQue ry("select * from student");
while(resultset.next())
{
System.out.println(resultset.g etInt("S_ID")+" "+resultset.getString("S_NAME" )+" "+resultset.getInt("S_AGE"));
}
connection.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
} Output
thank you........................
Comments
Post a Comment