8.24 JDBC 调用存储过程

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

/**
 *
 * Title: Demo04
 *
 * Description: 
 * JDBC调用存储过程
 * CallableStatement 继承PreparedStatement,可以预处理
 * @version v0.01
 *
 * @author ByChai
 *
 * @date 2020年8月24日 下午3:24:37
 *
 *
 */
public class Demo04 {
    public static void main(String[] args) {
        Connection connection=null;
        CallableStatement csmt=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/teach?serverTimezone=Asia/Shanghai", "root", "root");
            csmt=connection.prepareCall("call pro1(?,?)");
            //设置参数
            csmt.setInt(1, 8);
            csmt.registerOutParameter(2, Types.VARCHAR);
            //执行
            csmt.execute();
            String week=csmt.getString(2);//获取输出参数
            System.out.println(week);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            //关闭资源
            if(csmt!=null) {
                try {
                    csmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(connection!=null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }
    }
}
原文地址:https://www.cnblogs.com/Guang09/p/13554228.html