Tutorials  /  Databases

Efficient Database Interaction with Java: CallableStatement for Stored Procedures

Ccentron Redaktion · October 2024 ·3 min read ·Databases, Tutorial

In our blog post, learn how to call stored procedures in an Oracle database using Java and CallableStatement. From connecting to the database to efficient data manipulation—discover practical examples and tips for optimized database interaction. Dive in and maximize the performance of your applications with this comprehensive guide! Understanding how to effectively manage database interactions can significantly enhance the performance of your applications, allowing for more responsive and efficient user experiences.

Standard Functions of CallableStatement

The CallableStatement interface supports the execution of stored procedures with IN and OUT parameters. In this example, we use an Oracle database and first look at the basic functions of CallableStatement for IN and OUT parameters. These functions are crucial for retrieving and manipulating data effectively, enabling seamless interaction between your Java applications and the database layer.

DB

Matching infrastructure at centron

Databases need care in production: centron can take over updates, backups and monitoring – from a single instance to a full cluster. Explore managed clusters →

Example of a CallableStatement Statement

We start by creating a simple table for our example programs. Then, we create a helper class to establish a connection to the Oracle database. This setup is essential for ensuring that our application can communicate with the database reliably and perform the necessary operations without interruptions or errors.

SQL
CREATE TABLE EMPLOYEE (
  "EMPID"   NUMBER NOT NULL ENABLE,
  "NAME"    VARCHAR2(10 BYTE) DEFAULT NULL,
  "ROLE"    VARCHAR2(10 BYTE) DEFAULT NULL,
  "CITY"    VARCHAR2(10 BYTE) DEFAULT NULL,
  "COUNTRY" VARCHAR2(10 BYTE) DEFAULT NULL,
  PRIMARY KEY ("EMPID")
);
Code
// DBConnection.java
// Helper class for establishing the database connection

Example for CallableStatement: Stored Procedure with IN and OUT Parameters

We create a simple stored procedure to insert data into the employee table. The stored procedure expects inputs from the caller that should be inserted into the table. Understanding the structure of stored procedures is vital for leveraging the full power of database management systems, allowing developers to create reusable code that simplifies complex operations.

Go
CREATE OR REPLACE PROCEDURE insertEmployee (
  in_id IN EMPLOYEE.EMPID%TYPE,
  in_name IN EMPLOYEE.NAME%TYPE,
  in_role IN EMPLOYEE.ROLE%TYPE,
  in_city IN EMPLOYEE.CITY%TYPE,
  in_country IN EMPLOYEE.COUNTRY%TYPE,
  out_result OUT VARCHAR2
)
AS
BEGIN
  INSERT INTO EMPLOYEE (EMPID, NAME, ROLE, CITY, COUNTRY) 
  VALUES (in_id, in_name, in_role, in_city, in_country);
  COMMIT;
  
  out_result := 'TRUE';
  
EXCEPTION
  WHEN OTHERS THEN 
  out_result := 'FALSE';
  ROLLBACK;
END;

In the Java program, we use CallableStatement to execute the stored procedure insertEmployee and insert employee data. We use IN and OUT parameters to process the results. This approach not only streamlines the data entry process but also improves error handling, making the application more robust and reliable.

The program reads user inputs for employee data, calls the stored procedure, and outputs the result. By following these steps, you can ensure that your application maintains high data integrity and provides users with accurate feedback.

This example demonstrates the fundamentals of using CallableStatement in Java for executing stored procedures. It shows how IN and OUT parameters can be used to interact with the database and perform complex tasks, enhancing overall application efficiency.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Databases
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren