×

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 String equalsIgnoreCase() method

equalsIgnoreCase() method compares two Strings based on the their content but ignore the case. Syntax public boolean equalsIgnoreCase(Objects anObject) Parameter anObject: Object to be compared with the current String without case consideration. Returns It returns true...

1 minute read.

Set Value to Enum in Java

In this article, you will be acknowledged about Enum in java. Most importantly you will learn how to set value to Enum or how to practically customize a value to...

3 minutes read.

Prime Points in Java

The points that divide an integer into two halves containing a prime number are known as prime points. Printing every prime point of a specific number is the task. Let's...

6 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

Java Math asin() Method

The asin() method of Math class computes the trigonometric Arc Sine (inverse of sine ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double asin(double a) Parameters: The...

1 minute read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.

What is new in Java 17?

Java 17 LTS is the most recent long-term support release for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, which stands for long-term support, JDK 17...

6 minutes read.

Difference between final, finally, and finalize()

Difference between final, finally, and finalize() In Java, final, finally and finalize sound similar they are totally different in functionality. Final and finally are the two keywords but the finalize is...

4 minutes read.

Tower of Hanoi Program in Java

Tower of Hanoi Program in Java The Tower of Hanoi program in Java is written to solve a mathematical puzzle, called Tower of Hanoi, where we have three poles and n...

4 minutes read.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

Java Password Generator

Generally, we must create a strong password for security reasons. In Java, there are numerous strategies for creating secure passwords. We will learn how to create a strong password in...

4 minutes read.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 minutes read.

Java Generics Questions

Introduction We'll walk through a few real-world examples of interview questions and responses for Java generics in this article. Java 5 saw the debut of the fundamental idea of generics. Due to...

9 minutes read.

Cast Operator in Java

A cast is a unique operator that completely converts one type of data into another. Casts are unary operators and have the same priority as other unary operators. Type casting in...

3 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.

Difference between JDK, JRE and JVM In Java

In the Java ecosystem, three primary components are often mentioned: JDK, JRE, and JVM. Here’s a breakdown of each: Java Development Kit (JDK) The Java Development Kit (JDK) is a comprehensive software...

2 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

How to Install Java on MAC

There are many possible ways to install java on mac. This article is based on the installation of java on mac. The operating system platform is Mac OS X, macOS and...

3 minutes read.

Determine the Upper Bound of a Two-Dimensional Array in Java

Multi-Dimensional Array Multi-faceted exhibits in Java are regular and can be named various clusters. Information in a two-layered bunch in Java is put away in 2D even structure. A two-layered collection...

3 minutes read.