JDBC调用存储过程基本流程(SXT)

来源:互联网 发布:手机查看淘宝店铺号 编辑:程序博客网 时间:2024/06/07 05:05

 JDBC调用存储过程基本流程(SXT)

 

Demo

 

  1. import java.sql.*;
  2. public class TestProcedure
  3. {
  4.     /**
  5.      * @JDBC调用存储过程基本流程, procedure p 是4个参数的。第3和第4个为输出参数
  6.      */
  7.     public static void main(String[] args)
  8.     {
  9.         // TODO Auto-generated method stub
  10.         Connection conn = null;
  11.         CallableStatement cstmt = null;
  12.         try
  13.         {
  14.             Class.forName("com.mysql.jdbc.Driver");
  15.             conn = DriverManager.getConnection(
  16.                     "jdbc:mysql://localhost:3306/college""root""123456");
  17.             cstmt = conn.prepareCall("{call p (?,?,?,?)}");
  18.             cstmt.registerOutParameter(3, Types.INTEGER);
  19.             cstmt.registerOutParameter(4, Types.INTEGER);
  20.             cstmt.setInt(120);
  21.             cstmt.setInt(230);
  22.             cstmt.setInt(440);
  23.             cstmt.execute();
  24.             System.out.println(cstmt.getInt(3));
  25.             System.out.println(cstmt.getInt(4));
  26.         } catch (ClassNotFoundException e)
  27.         {
  28.             // TODO Auto-generated catch block
  29.             e.printStackTrace();
  30.         } catch (SQLException e)
  31.         {
  32.             // TODO Auto-generated catch block
  33.             e.printStackTrace();
  34.         } finally
  35.         {
  36.             try
  37.             {
  38.                 if (cstmt != null)
  39.                 {
  40.                     cstmt.close();
  41.                     cstmt = null;
  42.                 }
  43.                 
  44.                 if(conn != null)
  45.                 {
  46.                     conn.close();
  47.                     conn = null;
  48.                 }
  49.             } catch (SQLException e)
  50.             {
  51.                 // TODO Auto-generated catch block
  52.                 e.printStackTrace();
  53.             }
  54.         }
  55.     }
  56. }

 

 

原创粉丝点击