使用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 }

 

posted on 2016-11-11 18:58  eaglezb  阅读(1562)  评论(0编辑  收藏  举报

导航