CallableStatement的实例
来源:互联网 发布:网页游戏挂机软件 编辑:程序博客网 时间:2024/05/01 15:20
一、概念
CallableStatement 对象用于执行对数据库已存在的存储过程的调用。
二、先对存储过程语法说明
1、先配置一个存储过程
http://blog.csdn.net/u010439954/article/details/46475969
上面网页对存储过程的说明
2、表的结构test
idvarchar(12) primary key
name varchar(12) not null
先插入一条数据
insert into test (id,name)values("001","wang");
3、创建测试用的存储过程
>delimiter //
>create proceduce test_proc(in user_id varchar(12), out user_name varchar(12))
->begin
->select name into user_name from test where id = user_id;
->end;
->//
->delimiter ;
4、查看存储过程是否存在 ,可忽略
select name from mysql.proc where db='自己的数据库名' and name='存储过程名如(test_proc)'
5、测试存储过程是否有效 ,可忽略
定义两个变量,并初始化
>set @user_id = '001';
>set @user_name = null;
调用存储过程
>call test_proc(@user_id,@user_name);
查看返回结果
>select @user_name;
有返回值则成功。
三、 CallableStatement 语法说明
其主要语法有 IN 和 OUT。
IN 参数传给 CallableStatement 对象是通过 setXXX 方法完成的。所传入参数的类型决定了所用的 setXXX 方法,如使用setInt(1,12);对数据库表中第一个整型类型的传入一个整型。
OUT 参数返回 储存数据。在执行 CallableStatement 对象以前必须先了解其数据表数据类型,注册每个 OUT 参数返回的 JDBC 类型。语句执行完后,CallableStatement 的 getXXX 方法将取回参数值。正确的 getXXX 方法是为各参数所注册的 JDBC 类型所对应的 Java 类型。
四、 CallableStatement 的java案例
java连接mysql的驱动包这里就不说了
package test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Types;import java.sql.CallableStatement;public class mysqltest {public static void main(String args[]) {Connection conn = null;CallableStatement statement = null;String url = "jdbc:mysql://localhost:3306/le?characterEncoding=UTF-8";String username = "root";String password = "123456"; try {// 加载驱动程序以连接数据库Class.forName("com.mysql.jdbc.Driver");//使用各自的账号密码,连接数据库conn = DriverManager.getConnection(url, username, password);//数据库存储过程的方法String sql = "{call test_proc(?,?)}";//调用数据库存储过程statement = conn.prepareCall(sql);//对第一个参数传入字符串类型statement.setString(1,"001");//注册第二个参数为返回值,并定义为字符串类型statement.registerOutParameter(2, Types.VARCHAR);//执行数据库操作int issucess = statement.executeUpdate();if(issucess>0){//返回第二个参数返回值String msg=statement.getString(2);System.out.println(msg);}} catch (Exception cnfex) {cnfex.printStackTrace();}}}
- CallableStatement的实例
- JDBC实例 Statement,PreparedStatement,CallableStatement三个方法的实例
- JDBC实例 Statement,PreparedStatement,CallableStatement三个方法的实例
- CallableStatement的使用实例以及PreparedStatement的批处理
- CallableStatement的用法
- CallableStatement 的用法
- CallableStatement的用法
- CallableStatement的用法
- CallableStatement的用法
- CallableStatement
- CallableStatement
- statement、prepareStatement、callableStatement的使用
- Statement、CallableStatement、preparedStatement的区别
- statement、prepareStatement、callableStatement的使用
- JDBC之CallableStatement 的使用
- Statement、PreparedStatement、CallableStatement的作用
- CallableStatement的相关用法解疑
- Statement,PrepareStatement,CallableStatement的区别
- python hadoop 在streaming中获取文件名的方法
- Class^=,Class*= ,Class$=含义
- SQL Server数据库
- ZigBee网络体系结构(图文)
- Android网络编程之如何去掉WebView【浏览器】中的进度条和网址
- CallableStatement的实例
- 字符的排序(冒泡排序,选择排序)
- 如何应用QQ分享
- Codeforces 570 B. Simple Game ( 概率 )
- iOS中利用NSURLSession进行文件断点下载
- hdu 5387 Clock 2015多校联合训练赛#8
- 简易信息管理系统——增删改查知识总结
- OpenSessionInViewFilter解决数据库操作表(一对多)延迟加载时session关闭的异常
- C++11的线程库(二)