zoukankan      html  css  js  c++  java
  • 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();
                    }
                }
                
            }
        }
    }
  • 相关阅读:
    python入门的120个基础练习
    python日志打印模块
    自动化测试总结
    Http_requests
    安装electron-ssr出现的问题
    豆瓣油猴脚本
    ubuntu 16.04 無法進入tty1-6(未解決)
    如何用firefox chrome chromium看只支持IE浏览器的视频 通过wine 安装IE
    python reverse 和reversed
    python 编码问题
  • 原文地址:https://www.cnblogs.com/Guang09/p/13554228.html
Copyright © 2011-2022 走看看