Java示例中的CallableStatement

Java中的CallableStatement用于调用存储过程。本文档详细介绍了如何使用CallableStatement进行存储过程的IN和OUT参数操作,包括Oracle数据库的CURSOR和STRUCT示例。示例包括创建表、插入数据、通过ID获取员工信息以及使用Oracle CURSOR和STRUCT处理数据库对象。
摘要由CSDN通过智能技术生成

CallableStatement in java is used to call stored procedure from java program. Stored Procedures are group of statements that we compile in the database for some task. Stored procedures are beneficial when we are dealing with multiple tables with complex scenario and rather than sending multiple queries to the database, we can send required data to the stored procedure and have the logic executed in the database server itself.

Java中的CallableStatement用于从Java程序调用存储过程。 存储过程是我们在数据库中为某些任务编译的一组语句。 当我们处理具有复杂场景的多个表时,存储过程是有益的,而不是向数据库发送多个查询,我们可以将所需的数据发送到存储过程,并在数据库服务器本身中执行逻辑。

CallableStatement (CallableStatement)

CallableStatement, CallableStatement in Java, CallableStatement Example

JDBC API provides support to execute Stored Procedures through CallableStatement interface.


JDBC API支持通过CallableStatement接口执行存储过程。

Stored Procedures requires to be written in the database specific syntax and for my tutorial, I will use Oracle database. We will look into standard features of CallableStatement with IN and OUT parameters.

存储过程需要使用数据库特定的语法编写,对于本教程,我将使用Oracle数据库。 我们将研究带有IN和OUT参数的CallableStatement的标准功能。

Later on we will look into Oracle specific STRUCT and Cursor examples.

稍后,我们将研究Oracle特定的STRUCTCursor示例。

Let’s first create a table for our CallableStatement example programs with below SQL query.

首先,使用下面SQL查询为我们的CallableStatement示例程序创建一个表。

create_employee.sql

create_employee.sql

-- For Oracle DB
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")
  );

Let’s first create a utility class to get the Oracle database Connection object. Make sure Oracle OJDBC jar is in the build path of the project.

首先,我们创建一个实用程序类来获取Oracle数据库Connection对象。 确保Oracle OJDBC jar位于项目的构建路径中。

DBConnection.java

DBConnection.java

package com.journaldev.jdbc.storedproc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection {

	private static final String DB_DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver";
	private static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:orcl";
	private static final String DB_USERNAME = "HR";
	private static final String DB_PASSWORD = "oracle";
	
	public static Connection getConnection() {
		Connection con = null;
		try {
			// load the Driver Class
			Class.forName(DB_DRIVER_CLASS);

			// create the connection now
			con = DriverManager.getConnection(DB_URL,DB_USERNAME,DB_PASSWORD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLExceptio
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值