Java CallableStatement接口的用法(附带实例)
CallableStatement 是 Statement 接口的子接口,Java 程序在通过 JDBC 调用存储过程时,需要先通过一个数据库连接创建一个 CallableStatement 对象,该对象包含对存储过程的调用以及执行。
CallableStatement 接口中的常用方法如下表所示:
CallableStatement 对象为所有的数据库系统提供了一种标准的形式去调用数据库中已存在的存储过程,调用存储过程的语法格式如下:
接下来,通过案例来演示 CallableStatement 接口的使用(【实例 1】)。
程序中很多地方都需要获取数据库的连接并关闭连接,所以将这部分代码提到一个公共的 BaseDAO 类中,并将这个类放到 com.aaa.util 中,代码如下:
实例 1 中的存储过程的内容如下:
CallableStatement 接口中的常用方法如下表所示:
| 方法 | 方法描述 |
|---|---|
| int getInt(int parameterIndex) | 按照索引获取指定的过程的返回值 |
| int getInt(String parameterName) | 按照名称获取指定的过程的返回值 |
| void registerOutParameter(int parameterIndex, int sqlType) | 设置返回值的类型,需要指定Types类 |
| String getString(int parameterIndex) | 按照索引获取指定的过程的返回值 |
| String getString(String parameterName) | 按照名称获取指定的过程的返回值 |
CallableStatement 对象为所有的数据库系统提供了一种标准的形式去调用数据库中已存在的存储过程,调用存储过程的语法格式如下:
{ call 存储过程名(?, ?, …)} // 其中,?是参数占位符
接下来,通过案例来演示 CallableStatement 接口的使用(【实例 1】)。
import com.aaa.util.BaseDAO;
import java.sql.*;
public class Demo {
public static void main(String[] args) {
Connection conn = null;
CallableStatement callableStatement = null;
try {
conn = BaseDAO.getConnection();
// 调用存储过程
String sql = "{call pro_getNameById(?,?)}"; // 获取存储过程执行对象
callableStatement = conn.prepareCall(sql); // 对占位符进行数据填充
callableStatement.setInt(1, 1);
// 注册out参数,第1个参数代表的是参数的位置,第2个参数代表的是参数的类型
callableStatement.registerOutParameter(2, Types.VARCHAR);
callableStatement.execute(); // 执行存储过程
// 根据位置获取输出参数,输出参数的位置为2
String sname = callableStatement.getString(2);
System.out.println(sname);
} catch (Exception e) {
e.printStackTrace();
} finally {
BaseDAO.closeAll(conn, callableStatement);
}
}
}
程序中很多地方都需要获取数据库的连接并关闭连接,所以将这部分代码提到一个公共的 BaseDAO 类中,并将这个类放到 com.aaa.util 中,代码如下:
package com.aaa.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDAO {
// 定义MySQL的数据库驱动程序
public static final String DRIVER = "com.mysql.jdbc.Driver";
// 数据库连接字符串
public static final String URL = "jdbc:mysql://school?useUnicode=true&characterEncoding=utf-8";
public static final String USER = "root"; // 数据库服务器账号
public static final String PWSD = "root"; // 数据库服务器密码
// 数据库连接方法
public static Connection getConnection() {
Connection con = null;
try {
Class.forName(DRIVER); // 加载驱动类
con = DriverManager.getConnection(URL, USER, PWSD); // 获取连接
} catch (ClassNotFoundException e) {
System.out.println("加载失败"); // 如果有异常会执行
e.printStackTrace();
} catch (SQLException e) {
System.out.println("连接数据库错误");
e.printStackTrace();
}
return con;
}
public static void closeAll(Connection con) { // 关闭数据库对象方法
try {
if (con != null) {
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
程序的运行结果如下:
张三
实例 1 中的存储过程的内容如下:
drop procedure if EXISTS pro_getNameById; create PROCEDURE pro_getNameById(sid int,out sname varchar(50)) BEGIN select 'name' into sname from students where id=sid; end;实例 1 中,使用 Connection 接口中的 prepareCall() 方法获取 CallableStatement 对象,并对 SQL 语句进行预编译。然后,对 SQL 语句中的占位符进行赋值,并注册参数的类型,最后调用 CallableStatement 接口中的 execute() 方法执行存储过程。
ICP备案:
公安联网备案: