×

Java Database Connectivity with Oracle

JDBC:

A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect the database with the java program, we use JDBC. JDBC stands for Java Database Connectivity.

Out of the 4 available edition of java, JDBC is available in JavaSE (Java Standard Edition). Java Database Connectivity is an API (Application programming Interface), which uses JDBC drivers to connect with the database.

We are available with four types of JDBC drivers:

  1. JDBC-ODBC bridge Driver
  2. Native Driver
  3. Network protocol Driver
  4. Thin Driver
JAVA DATABASE CONNECTIVITY WITH ORACLE

To connect to the database, we should follow 5 steps.

Step 1: Register the driver class.

Use forName() Method for registering the driver class.

Step 2: Creating the connection to the database.

For creating the connection, we should know the Connection URL, Username and password of the database.

Step 3: Create a Statement.

Step 4: Executing the provided queries.

Using this step, we can pass the queries to work on the database and update the information in the database.

Step 5: Close connection.

The connection which is established should be closed after working with it. This helps in clearing all buffers.

The Java Application is linked with the database by following the above 5 steps.

We are available with many different databases in the market today. Some of the databases are

  • MySQL
  • Oracle
  • MongoDB
  • PostgreSQL

JDBC Connectivity with Oracle database:

For establishing a connection of java application with Oracle database, we should follow the above prescribed steps. Considering oracle 10g version, we are looking into the following examples. Before establishing a connection with the java application, we must have the following prerequisites known, which differs for every database.

  1. We should know the driver class of the database which we are using. The driver class for the Oracle database is oracle.jdbc.driver.OracleDriver.
    The driver class is unique for a particular database.
  2. We should also be aware of the Connection URL of the database which we are using. For the Oracle 10G version database, the connection URL is jdbc:oracle:thin:@localhost:1521:xe . In here, the jdbc is an API, oracle is the database, thin is the JDBC driver, localhost is the local server name on which the oracle database is running. The IP address has 1521 as the port number and XE is the Oracle service name. We can get all these information regarding the database from a file named tnsnames.ora file.
  3. We should know the Username for the oracle database.
  4. We should know the corresponding password of the database also.

These are the prerequisites which are mandatory for developing a connection with the oracle database.

Database Operations:

We can perform many operations on the database. All the DML (Data manipulation Language) keywords are used to manipulate the data which is stored in the database.

In the database the data is stored in the tabular form. All the properties of the data are allotted as the columns for the table. And rows are added into the database by defining all the columns for a particular row. So, performing operations on the database means that performing operations on various tables present in the database.

Some of the operations which are performed on the database are:

  • Creating a table - This operation is done using the create keyword.
  • Inserting the rows – This operation is done using the insert keyword.
  • Updating the row values – This operation is performed using the update keyword.
  • Deleting the rows in the table – This operation is done by using the delete Keyword.
  • Altering the columns – This operation is performed using the Alter keyword.
  • Removing the table – This operation can be performed using the Truncate keyword.
  • Print the data – This operation can be performed using the select keyword.

So, the oracle database queries using these keywords are passed into the java program as the parameters into the executeQuery Method.

Let’s look at a small example java application program which connects to the oracle database.

import java. io. *;
class DatabaseConnectionDemo
{
public static void main (String args[])
{
try 
{
//Step 1: Register the driver class
Class.forName (“ oracle.jdbc.driver.OracleDriver “);


//Step 2: Establish the connection.
Connection conn = DriverManager.getConnection (“jdbc: oracle: thin: @localhost: 1521: xe “, “scott”, “tiger”);


//Step 3: Create the statement.


Statement st = conn.createStatement();


//Step 4: Perform operations on the database in the form of queries.


ResultSet rst = st.executeQuery( “ select * from STUDENT “);
// Printing the student ID, student Name, Student Location of all the students from the students table
While (rst.next())
{
System.out.println(rst.getInt(1) + “ “ + rst.getString(2) + “ “ + rs.getString(3));
 }


//Step 5: Close the connection.


conn.close();
}
catch (Exception e)
{
System.out.println( “Exception caught” +e);
}
} //main
} //DatabaseConnectionDemo

OUTPUT:

1011  Akshara         Hyderabad   

1012  Aarush           Warangal

1013  Srihitha          Hyderabad.  

NOTE:

For connecting the java application with the database, a file should be loaded which is ojdbc14.jar file.

For loading this jar file, there are two possibilities.

1.Set classpath
This includes setting the path for the file. So, this can be done in two ways.

  1. Temporary path
  2. Permanent path

Temporary path setting:

At first, search where the ojdbc14.jar file is located. Then open the command prompt and write the following in the command prompt.

C:>set classpath = c:\folder\ojdbc14.jar;.;

folder resembles the folder in which the ojdbc14.jar file is located.

Permanent path setting:

To set a permanent path, the following steps are to be followed.

  • Go to the environment variable.
  • Click on the new tab.
  • Write the classpath in the variable name.
  • Paste the path to ojdbc14.jar file by adding ojdbc14.jar;.; in the variable value.

2. Paste the ojdbc14.jar file in JRE / lib / ext folder.

Follow the below steps to load the jar file by pasting the file in the JRE.

  • Search the ojdbc14.jar file location.
  • Then go to the JRE folder having lib. Then enter the ext file which is present in the lib folder.
  • Now paste the jar file here.

Related Topics

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.

Java Math floor() Method

The floor() method of Math class returns the largest double value that is equal to a mathematical integer and is less than or equal to the argument. Syntax: public static double floor(double...

2 minutes read.

Interface Program in Java

Interface Program in Java In the previous topic, we discussed that abstraction is possible through the interface and abstract class. An abstract class provides partial to 100% abstraction. 100 %abstraction in...

4 minutes read.

How to check data type in Java

In this section, we will learn about the methodology to verify the data type in Java. There are mainly two methods in java called as getClass() and getName(). They are...

3 minutes read.

How to Call a Method in Java

In Java, a method is a collection of statements that perform a specific task or action.It can accept data with the help ofitsarguments. It  is also called a function. In order...

9 minutes read.

Creating a Jar file in Java

The JDK's jar (Java Archive) tool offers the ability to produce jar files that can be executed. If you double-click a jar file that is executable, it will call the...

2 minutes read.

Java Case Keyword

Case keyword: In this article we are going to learn the concept of a java case keyword.Generally, java case keyword is used with the switch statements.Case keyword is implemented in conditional...

3 minutes read.

Java callable example

What is callable in Java? Callable is an interface that is present in Java. There are two methods for constructing threads: one is to inherit the Thread class, while the other...

3 minutes read.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.

Static() Function in Java

The static keyword in Java is suitable for variables, constants, and functions. The static keyword is mainly used to control storage so that it may be appropriately used. We shall...

3 minutes read.

Selection Sort in Java

Selection Sort in Java Selection sort is also a simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted portion of the input array, and placing it...

5 minutes read.

Java Generate Random String

Java Generate Random String In this tutorial, we will learn about to generate random string in Java. Random generation strings mean any string will be generated, which does not follow any...

7 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

How to remove special characters from String in Java

Strings in Java are Objects that are supported inside by a burn exhibit. Since exhibits are immutable (cannot develop), Strings are changeless too. A completely new String is made whenever...

5 minutes read.

Morris Traversal for Inorder in Java

Through Morris’s traversal, a tree is traversed without the aid of recursion or stacks. Based on the threaded binary tree, the Morris traversal is used. We perform internal modification throughout...

4 minutes read.

Count of Range Sum Problem in Java

In this article, we will discuss the basic approach or native approach used to count range sum problem in java. To solve this problem, we will check for numbers if they...

6 minutes read.

Lambda expressions in Java

A brief introduction to Lambda expression in java In this topic, we will discuss the lambda expression in java. A lambda expression in Java is an enhanced version of an anonymous...

13 minutes read.