• Java程序设计——PreparedStatement接口和CallableStatement接口(JDBC编程)


    目录

    一、PreparedStatement接口

    二、CallableStatement接口

    三、实例演示(CallableStatement接口)


    一、PreparedStatement接口

    PreparedStatement两大特点:

    • 将含参数的SQL语句进行预编译,当需要执行多次SQL语句时,直接执行预先编译好的SQL语句,执行速度比Statement对象快
    • PreparedStatement可执行动态的SQL语句,即传递参数给SQL语句即可,提高了程序的灵活性和效率

    执行带参数的SQL语句之前需要对占位符‘?’参数进行赋值,PreparedStatement提供setXXX()方法进行赋值

    1. import java.sql.*;
    2. public class PreparedStatementDemo {
    3. // PreparedStatement:防止sql注入
    4. private static String diver = "com.mysql.cj.jdbc.Driver";
    5. private static String url = "jdbc:mysql://localhost:3306/de?serverTimezone=UTC&useSSL=false";
    6. private static String user = "root";
    7. private static String password = "root";
    8. public static void main(String[] args) {
    9. Connection connection = null;
    10. PreparedStatement pstmt = null;
    11. ResultSet rs = null;
    12. int count = 0;
    13. try {
    14. connection = DriverManager.getConnection(url,user,password);// 获取数据库连接对象
    15. String sql_insert = "INSERT INTO tb_student VALUES(?,?,?,?,?,?,?)";
    16. pstmt = connection.prepareStatement(sql_insert); // 将sql语句进行预编译
    17. pstmt.setInt(1,2014310103);
    18. pstmt.setString(2,"新来的");
    19. pstmt.setString(3,"男");
    20. pstmt.setString(4,"2022-7-3");
    21. pstmt.setString(5,"广东");
    22. pstmt.setString(6,"汉");
    23. pstmt.setString(7,"AC1234");
    24. count = pstmt.executeUpdate();
    25. System.out.println("插入" + count + "条记录");
    26. } catch (SQLException ex) {
    27. ex.printStackTrace();
    28. }
    29. finally {
    30. if (rs != null){
    31. try {
    32. rs.close();
    33. } catch (SQLException e) {
    34. e.printStackTrace();
    35. }
    36. }
    37. if (pstmt != null){
    38. try {
    39. pstmt.close();
    40. } catch (SQLException e) {
    41. e.printStackTrace();
    42. }
    43. }
    44. if (connection != null){
    45. try {
    46. connection.close();
    47. } catch (SQLException e) {
    48. e.printStackTrace();
    49. }
    50. }
    51. }
    52. }
    53. }


    二、CallableStatement接口

    CallableStatement接口:用于执行数据库中的存储过程(数据库中一种特殊的预编译的SQL语句)

    三种带参数的存储过程:

    • IN:输入参数
    • OUT:输出参数
    • IN OUT:输入输出参数

    调用无返回值的存储过程:

    {call 存储过程名([参数占位符])}

    调用有返回值的存储过程:

    {参数占位符 = call 存储过程名([参数占位符])} 

    示例:

    1. // 不带参数
    2. CallableStatement cstmt1 = con.prepareCall("{call P()}");
    3. // 带2个参数
    4. CallableStatement Cstmt2 = con.prepareCall("{call P(?,?)}");
    5. // 带2个参数且有返回值
    6. CallableStatement cstmt3 = con.prepareCall("{? = call P(?,?)}");

    CallableStatement接口通过setXXX()方法对IN参数进行赋值,通过registerOutParameter()方法对OUT参数进行类型注册


    1.OUT参数类型注册的方法

    registerOutParameter(int index, int sqlType)
    • index:对应参数占位符的序号
    • sqlType:对应参数的类型,通常使用java.sql.Types的静态常量来指定,如Types.FLOAT...

    如:注册第3个OUT输出参数是DOUBLE类型

    cstmt.registerOutParameter(3, java.sql.Types.DOUBLE)

    2.检索结果的获取

    CallableStatement接口:提供了getXXX()方法来获取OUT参数的值

    IN OUT 参数具有IN和OUT参数的全部功能,需先使用setXXX()对IN OUT参数值进行设置,

    再对该IN OUT参数进行类型注册,执行后再使用getXXX()方法来获取执行结果

    如:创建带IN和OUT参数的CallableStatement对象

    1. // 1.获取CallableStatement对象
    2. CallableStatement cstmt = con.prepareCall("{? = call P(?,?)}");
    3. // 2.注册OUT参数的类型
    4. cstmt.registerOutParameter(1, java.sql.Types.INTEGER);
    5. // 3.为IN参数赋值
    6. cstmt.setInt(2, 123);
    7. cstmt.setString(3, "zhangsan");
    8. // 4.执行
    9. cstmt.execute();
    10. // 5.根据索引(参数名称)获取返回值
    11. int res = cstmt.getInt(1);

    CallableStatement接口一般用于执行存储过程,返回结果可为多个结果集或多个记录行,所以一般用execure()执行SQL语句


    三、实例演示(CallableStatement接口)

    ①.准备好一张要操作的数据表

    ②.创建带有INOUTIN OUT参数的存储过程

    ③.源代码:

    1. import java.sql.*;
    2. public class CallableStatementDemo {
    3. private static String diver = "com.mysql.cj.jdbc.Driver";
    4. private static String url = "jdbc:mysql://localhost:3306/de?serverTimezone=UTC&useSSL=false";
    5. private static String user = "root";
    6. private static String password = "root";
    7. public static void main(String[] args) {
    8. Connection connection = null;
    9. CallableStatement cstmt = null;
    10. try {
    11. connection = DriverManager.getConnection(url,user,password);// 获取数据库连接对象
    12. // 1.创建CallableStatement对象
    13. cstmt = connection.prepareCall("{call P(?,?,?)}");
    14. // 2.给IN参数赋值
    15. cstmt.setInt(1,2013110101);
    16. // 3.给OUT参数注册类型
    17. cstmt.registerOutParameter(2, Types.VARCHAR);
    18. // 4.给IN OUT参数的IN参数赋值,OUT参数注册类型
    19. cstmt.setString(3,"This is a good name!");
    20. cstmt.registerOutParameter(3, Types.VARCHAR);
    21. // 5.执行
    22. cstmt.execute();
    23. // 6.通过参数索引获取值
    24. String name = cstmt.getString(2);
    25. String comment = cstmt.getString(3);
    26. // 7.输出执行结果
    27. System.out.println(name + "------" + comment);
    28. } catch (SQLException ex) {
    29. ex.printStackTrace();
    30. }
    31. finally {
    32. if (cstmt != null){
    33. try {
    34. cstmt.close();
    35. } catch (SQLException e) {
    36. e.printStackTrace();
    37. }
    38. }
    39. if (connection != null){
    40. try {
    41. connection.close();
    42. } catch (SQLException e) {
    43. e.printStackTrace();
    44. }
    45. }
    46. }
    47. }
    48. }

    (28条消息) MySQL数据库——存储过程(八)_Stuttering Guy的博客-CSDN博客https://blog.csdn.net/Mr_Morgans/article/details/124721950?spm=1001.2014.3001.5501

  • 相关阅读:
    Python实现的公众号系统介绍及其中间件分析
    图像分类学习笔记(七)——MobileNet
    软件设计原则-开闭原则讲解以及代码示例
    Eolink 治愈了后端开发者的痛
    windows的批量解锁
    CH06_第一组重构(下)
    【CS231N】b站同济子豪兄全视频笔记
    完全解析Array.apply(null, { length: 1000 })
    [从零开始学习FPGA编程-54]:高阶篇 - 基于IP核的FPGA开发-PLL锁相环IP核的原理与配置(Altera)
    4G dtu远程无线抄表
  • 原文地址:https://blog.csdn.net/Mr_Morgans/article/details/125583750