Introduction
What is JDBC?
Java Database Connectivity (JDBC) is an API that enables Java applications to interact with databases. It provides methods to query and update data in a database, as well as to retrieve metadata about the database itself.
Oracle Database
Oracle Database is a multi-model database management system produced and marketed by Oracle Corporation. It is one of the most widely used databases in the world, known for its robustness, scalability, and security features.
Table of Contents
- Setting Up the Oracle Database
- Adding Oracle JDBC Driver to Your Project
- Establishing a Database Connection
- Executing SQL Queries
- Retrieving Data
- Closing the Connection
- Conclusion
1. Setting Up the Oracle Database
First, ensure you have an Oracle database installed and running. Create a database user and a table for demonstration purposes. Use the Oracle SQL*Plus or any Oracle database client to execute the following commands:
CREATE USER jdbc_user IDENTIFIED BY jdbc_password;
GRANT CONNECT, RESOURCE TO jdbc_user;
CREATE TABLE products (
id NUMBER GENERATED BY DEFAULT AS IDENTITY,
name VARCHAR2(100),
description VARCHAR2(255),
price NUMBER(10, 2),
PRIMARY KEY (id)
);
2. Adding Oracle JDBC Driver to Your Project
To interact with an Oracle database, you need to add the Oracle JDBC driver to your project. If you are using Maven, add the following dependency to your pom.xml
file:
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.8.0.0</version>
</dependency>
3. Establishing a Database Connection
We will start by establishing a connection to the Oracle database using JDBC.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCOracleExample {
private static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
private static final String USER = "jdbc_user";
private static final String PASSWORD = "jdbc_password";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
public static void main(String[] args) {
try (Connection connection = getConnection()) {
if (connection != null) {
System.out.println("Connected to the Oracle database!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
4. Executing SQL Queries
We can use the Connection
interface to create a Statement
object and execute SQL queries. Here, we will insert a record into the products
table.
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class InsertProductExample {
public static void main(String[] args) {
String insertSQL = "INSERT INTO products (name, description, price) VALUES ('Laptop', 'Dell Inspiron', 75000.00)";
try (Connection connection = JDBCOracleExample.getConnection();
Statement statement = connection.createStatement()) {
int rowsInserted = statement.executeUpdate(insertSQL);
System.out.println(rowsInserted + " row(s) inserted!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
5. Retrieving Data
We can use the Connection
interface to create a Statement
object and execute a query to retrieve data from the products
table.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SelectProductExample {
public static void main(String[] args) {
String selectSQL = "SELECT * FROM products";
try (Connection connection = JDBCOracleExample.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSQL)) {
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String description = resultSet.getString("description");
double price = resultSet.getDouble("price");
System.out.println("ID: " + id + ", Name: " + name + ", Description: " + description + ", Price: " + price);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
6. Closing the Connection
Using the try-with-resources statement ensures that the connection is closed automatically. This is important to free up database resources.
import java.sql.Connection;
import java.sql.SQLException;
public class CloseConnectionExample {
public static void main(String[] args) {
try (Connection connection = JDBCOracleExample.getConnection()) {
if (connection != null) {
System.out.println("Connected to the Oracle database!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Conclusion
In this tutorial, we have covered the basics of using the JDBC Connection
interface to interact with an Oracle database. We demonstrated how to establish a connection, execute SQL queries, retrieve data, and close the connection using the try-with-resources statement. This guide should help you get started with JDBC and understand how to use the Connection
interface effectively with Oracle.
Comments
Post a Comment
Leave Comment