JDBC: CallableStatement

Table of Contents

java.sql.CallableStatement is used to call stored procedures in a database.

A stored procedure is like a function or method in a class, except it lives inside the database. Some database heavy operations may benefit performance-wise from being executed inside the same memory space as the database server, as a stored procedure.

Creating a CallableStatement

You create an instance of a CallableStatement by calling the prepareCall() method on a connection object. Here is an example:

CallableStatement callableStatement =
    connection.prepareCall("{call calculateStatistics(?, ?)}");

If the stored procedure returns a ResultSet, and you need a non-default ResultSet (e.g. with different holdability, concurrency etc. characteristics), you will need to specify these characteristics already when creating the CallableStatement. Here is an example:

CallableStatement callableStatement =
    connection.prepareCall("{call calculateStatistics(?, ?)}",
        ResultSet.TYPE_FORWARD_ONLY,
        ResultSet.CONCUR_READ_ONLY,
        ResultSet.CLOSE_CURSORS_OVER_COMMIT
    );

Setting Parameter Values

Once created, a CallableStatement is very similar to a PreparedStatement. For instance, you can set parameters into the SQL, at the places where you put a ? . Here is an example:

CallableStatement callableStatement =
    connection.prepareCall("{call calculateStatistics(?, ?)}");

callableStatement.setString(1, "param1");
callableStatement.setInt   (2, 123);

Executing the CallableStatement

Once you have set the parameter values you need to set, you are ready to execute the CallableStatement. Here is how that is done:

ResultSet result = callableStatement.executeQuery();

The executeQuery() method is used if the stored procedure returns a ResultSet.

If the stored procedure just updates the database, you can call the executeUpdate() method instead, like this:

callableStatement.executeUpdate();

Batch Updates

You can group multiple calls to a stored procedure into a batch update. Here is how that is done:

CallableStatement callableStatement =
    connection.prepareCall("{call calculateStatistics(?, ?)}");

callableStatement.setString(1, "param1");
callableStatement.setInt   (2, 123);
callableStatement.addBatch();

callableStatement.setString(1, "param2");
callableStatement.setInt   (2, 456);
callableStatement.addBatch();

int[] updateCounts = callableStatement.executeBatch();

OUT Parameters

A stored procedure may return OUT parameters. That is, values that are returned instead of, or in addition to, aResultSet. After executing the CallableStatement you can then access these OUT parameters from theCallableStatement object. Here is an example:

CallableStatement callableStatement =
    connection.prepareCall("{call calculateStatistics(?, ?)}");

callableStatement.setString(1, "param1");
callableStatement.setInt   (2, 123);

callableStatement.registerOutParameter(1, java.sql.Types.VARCHAR);
callableStatement.registerOutParameter(2, java.sql.Types.INTEGER);

ResultSet result = callableStatement.executeQuery();
while(result.next()) { ... }

String out1 = callableStatement.getString(1);
int    out2 = callableStatement.getInt   (2);

It is recommended that you first process the ResultSet before trying to access any OUT parameters. This is recommended for database compatibility reasons.

时间: 2024-11-15 00:39:05

JDBC: CallableStatement的相关文章

JDBC使用MySQL存储过程错误

JDBC连接执行 MySQL 存储过程报权限错误:User does not have access to metadata required to determine stored procedure parameter types. If rights can not be granted, 执行存储过程时,出现如下错误: java.sql.SQLException: User does not have access to metadata required to determine st

Enhancing the Application: Advanced JDBC Features(转)

Enhancing the Application: Advanced JDBC Features This chapter describes additional functionality that you can use in your Java application. Some of these features have not been implemented in the sample application, while some features are enhanceme

java se之File类

遍历某个目录路径下的所有文件并打印输出: package com.led.file; import java.io.File; public class File_List { public static void listFiles(File file){ if(file!=null){ if(file.isDirectory()){//是目录 File[] f=file.listFiles(); if(f!=null){ for(int i=0;i<f.length;i++){ listFi

elasticsearch river 参数文档

JDBC River parameters Jörg Prante edited this page on 23 Jan 2014 · 3 revisions Pages 15 Home Bulk indexing How bulk indexing is used by the JDBC river JDBC plugin feeder mode as an alternative to the deprecated Elasticsearch River API JDBC River par

SQL调用存储过程错误Internal error when parsing callable statement metadata (missing parameter type)

[6 Apr 2005 15:29] Chadwick Baatz Description: When calling a stored procedure with datatype Decimal(m,d) (i.e. Decimal(18,0)) the Connector/J library throws the exception below. After looking into the code it appears that the getCallStmtParameterTyp

jdbc java数据库连接 5)CallableStatement 接口

CallableStatement执行存储过程(也是预编译语言) 首先在sql中执行以下带有输入参数的代码: 1 DELIMITER $ 2 CREATE PROCEDURE pro_findById(IN sid INT) 3 BEGIN 4 SELECT * FROM person WHERE id = sid; 5 END $ 那么,这条语句的存储过程代码就是 CALL pro_findById (?); 使用CallableStatement来执行: 1 /** 2 带有输入参数的存储语

JDBC PreparedStatement ,CallableStatement,以及事务,回滚举例

程序中用到的类,文件,jar 代码: 1,文件:db.properties文件内容 user=rootpassword=123url=jdbc:mysql:///student_dbdriver=com.mysql.jdbc.Driveraaa 2,类Utils.class import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException

老调重弹:JDBC系列 之 存储过程 CallableStatement(创建和使用)

前言 最近在研究Mybatis框架,由于该框架基于JDBC,想要很好地理解和学习Mybatis,必须要对JDBC有较深入的了解.所以便把JDBC 这个东东翻出来,老调重弹,好好总结一番,作为自己的笔记,也是给读者一个参考--- 本文主要通过 使用JDBC创建存储过程 和使用JDBC调用存储过程两部分 阐述JDBC 对存储过程的支持.本文将在Oracle数据库下创建一个可以表示岗位信息的基本表Jobs为例, 然后通过存储过程对这个Jobs表进行各种操作.表JOBS的建表语句如下: -- Creat

JDBC连接数据库中CallableStatement执行有参存储过程及注解其他

Oracle的建有参存储过程的过程 procedure pro_01(v_01 in number,v_02 out varchar2) as begin select name into v_02 from test where id = v_01; exception when no_data_found then dbms_output.put_line('no data'); when too_many_rows then dbms_output.put_line('too many r