Javaweb学习笔记之JDBC(三):使用 CallableStatement 调用存储过程

一、使用 CallableStatement 调用带有一个 输入参数 的存储过程

1、首先创建一个存储过程:

2、 使用 CallableStatement 调用存储过程:

    /**
     * 调用带有输入参数的存储过程
     * CALL pro_findById(3);
     */
    @Test
    public void test1(){
        Connection conn = null;
        CallableStatement stmt = null;
        ResultSet rs = null;
        try {
            // 1、获取数据库连接对象
            conn = JdbcUtils.getConnection();
            // 2、准备预编译的 sql 语句
            String sql = "call pro_findById(?)"; // 调用存储过程的语句,带有一个输入参数
            // 3、执行预编译 sql
            stmt = conn.prepareCall(sql);
            // 4、设置输入参数
            stmt.setInt(1, 3);
            // 5、执行 sql 语句(所有调用存储过程的 sql 语句都是使用 executeQuery 执行)
            rs = stmt.executeQuery();
            // 6、遍历结果
            while(rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                System.out.println("id:" + id + ", name:" + name + ", gender:" + gender);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JdbcUtils.close(conn, stmt, rs);
        }
    }

 

二、使用 CallableStatement 调用带有 输入和输出参数 的存储过程

1、首先创建一个存储过程:

 

2、 使用 CallableStatement 调用存储过程:

    /**
     * 调用带有输出参数的存储过程
     * CALL pro_findById2(3, @sname);
     */
    @Test
    public void test2(){
        Connection conn = null;
        CallableStatement stmt = null;
        ResultSet rs = null;
        try {
            // 1、获取数据库连接对象
            conn = JdbcUtils.getConnection();
            // 2、准备预编译的 sql 语句
            String sql = "call pro_findById2(?, ?)"; // 调用存储过程的语句,带有一个输入参数,和一个输出参数
            // 3、执行预编译 sql
            stmt = conn.prepareCall(sql);
            // 4、设置输入参数
            stmt.setInt(1, 3);
            /**
             * 5、设置输出参数
             *  参数1:参数的索引位置
             *  参数2:存储过程中的输出参数的 jdbc 类型(varchar)
             */
            stmt.registerOutParameter(2, Types.VARCHAR);
            // 6、执行 sql 语句
            stmt.executeQuery(); // 结果不是返回到结果集中,而是返回到输出参数中
            /**
             * 7、得到输出参数的值
             *  索引值:预编译 sql 中,输出参数的位置
             *  getXXX() 方法专门用于获取存储过程中的输出参数
             */
            String name = stmt.getString(2);
            System.out.println("name: " + name);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JdbcUtils.close(conn, stmt, rs);
        }
    }

JdbcUtils.java 工具类:

package com.demo.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * jdbc 工具类
 */
public class JdbcUtils {

    // 连接数据库的 url:jdbc协议:数据库子协议://主机:端口/数据库
    private static String url = "jdbc:mysql://localhost:3306/day17";
    private static String username = "root";   // 用户名
    private static String password = "root";   // 密码

    // 将注册驱动程序放入 静态代码块中,程序一启动的时候执行,只执行一次
    static {
        try {
            // 注册驱动程序
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取 数据库连接对象
     */
    public static Connection getConnection() {
        try {
            // 获取数据库连接对象
            return DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 释放资源
     *
     * @param conn Connection 对象
     * @param stmt PreparedStatement 对象
     * @param rs ResultSet 对象
     */
    public static void close(Connection conn, Statement stmt, ResultSet rs) {
        if (rs != null){
            try {
                rs.close();
            }catch (Exception e){
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值