Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Saturday 6 January 2018

Oracle Database Connection in Java

How To Connect Oracle Database in Java

Oracle Database Connection in Java

Now here, we are going to see the program of Oracle database connection in java through JDBC(Java Database Connectivity) concept.

Here we will use Oracle10g database version for writing java JDBC example. We can connect our java application to any database such as MySQL, MSOffice, DB2, etc but here we will take only Oracle database example for connectivity to the java program.


5 Steps To Database Connectivity

As we know, we are going to use Oracle database here so we have to follow 5 steps for the oracle database connective. 5 steps are given below.

Driver Class : oracle.jdbc.driver.OracleDriver.

Where oracle is a package and jdbc is a sub package of oracle package and driver is a sub package of jdbc package and OracleDriver is a class which is defined in the driver package.

Connection Url : jdbc:oracle:thin:@localhost:1521:xe.

Where jdbc is an API and oracle is a database and thin is a driver and @localhost is the name of the server in which oracle is running we can also use IP address and 1521 is the port number and xe is the service name of oracle database.

Username : The default user name of Oracle db is system.

Password : At the time of installing Oracle database, the password is given by the user. You can give the desired password for your database.

Let's see how to connect java application with oracle database with a simple example.

First we have to create the table in oracle database for fetching all the records from the oracle database. 

create table student(rollno number(10), name varchar2(20));


by using above command you can create the table in oracle db.



Java Program For Oracle Database Connectivity

This is simple oracle jdbc connection example in java where we will use some steps to connect to the oracle database and fetch all the records from the particular table.

Here "system" is the default user name of oracle and "mca" is the password for oracle.

import java.sql.*;
class FetchRecords
{
public static void main(String args[])
{
try
{
//Step First, load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");

//Step Second, create the object of connection
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "system", "mca");

//Step Third, create the statement object
Statement stm = con.createStatement();

//Step Four, execute query
ResultSet rs = stm.executeQuery("select * from student");

while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}

//Step Five, close the connection object
con.close();
}

catch(Exception e)
{
System.out.println(e);
}
}
}

The above java jdbc oracle example will fetch all the records from the student table.

This is simple java program to connect oracle database to fetch all the records from the table.

We will see leter how to create table and insert, update and delete data from the table through java jdbc program.

In this post we saw java jdbc oracle connection example with step-by-step.

Share:

2 comments:

  1. Thank you so much for this great aticle about jdbc.

    Please provide tutorial on networking

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate