zoukankan      html  css  js  c++  java
  • 使用CallableStatement接口调用存储过程

    直接上下代码:

     1 package com.learn.jdbc.chap07;
     2 
     3 import java.sql.CallableStatement;
     4 import java.sql.Connection;
     5 import java.sql.Types;
     6 
     7 import com.learn.jdbc.util.DbUtil;
     8 
     9 /**
    10  * 使用CallableStatement接口调用存储过程
    11  * @author Administrator
    12  *
    13  */
    14 public class Demo1 {
    15     private static DbUtil dbUtil=new DbUtil();
    16     /**
    17      * 调用存储过程,通过id查询name
    18      * @param id
    19      * @return
    20      * @throws Exception
    21      */
    22     private static String getNameById(int id) throws Exception{
    23         Connection con = dbUtil.getCon();
    24         String sql="{CALL sp_getNameById(?,?)}";
    25         CallableStatement cstmt = con.prepareCall(sql);
    26         cstmt.setInt(1, id);
    27         cstmt.registerOutParameter(2, Types.VARCHAR);
    28         cstmt.execute();
    29         String name = cstmt.getString("nM");// nM: 数据库新建存储过程时name对应的命名
    30         dbUtil.close(cstmt, con);
    31         
    32         return name;
    33     }
    34     
    35     public static void main(String[] args) throws Exception {
    36         System.out.println("名称是: "+getNameById(1));
    37     }
    38 }
  • 相关阅读:
    11.10 日志
    1120day户别确认
    10.28代码
    hdu 1695 GCD (莫比乌斯反演)
    8月19日
    多态在子父类中的成员上的体现的特点
    JAVA学习日报 10/29
    JAVA学习日报 10/26
    JAVA学习日报 11/2
    JAVA学习日报 10/30
  • 原文地址:https://www.cnblogs.com/eaglezb/p/6055328.html
Copyright © 2011-2022 走看看