课时7:CallableStatement语句
来源:互联网 发布:toad oracle数据库管理 编辑:程序博客网 时间:2024/05/21 19:42
1.CallableStatement继承preparedStatement,提供了调用存储过程的能力
2.CallableStatement用法:
-调用简单的存储过程
-调用有输入参数的存储过程
-调用有输入、输出参数的存储过程
3.实例
命令行创建存储过程
create procedure all_customers() select* from CustomerTbl;
命令行调用
call all_customers();
使用CallableStatement调用
创建有输入输出参数的存储过程
create procedure insert_customer(in myname varchar(20),in myemail varchar(20)) insert into CustomerTbl(name,email)values(myname,myemail);
在MySQL中执行上面那句话是为了创建insert_customer方法,如果不这么创建的话,在.java文件中不能使用
CallableStatement cstmt = conn
.prepareCall("{call insert_customer(?,?)}");
create procedure getnamebyid(in cid int, out return_name varchar(20)) select name into return_name from CustomerTbl where id=cid;
package com.geek99.demo;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
public class Test5 {
public static void main(String[] args) {
// test1();
// test2();
test3();
}
static void test1() {
Connection conn = DBUtil.open();
try {
CallableStatement cstmt = conn
.prepareCall("{call all_customers()}");
ResultSet rs = cstmt.executeQuery();
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String email = rs.getString(3);
System.out.println(id + ":" + name + ":" + email);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
}
static void test2() {
Connection conn = DBUtil.open();
try {
CallableStatement cstmt = conn
.prepareCall("{call insert_customer(?,?)}");
cstmt.setString(1, "kite123");
cstmt.setString(2, "kite@123.com");
cstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
}
static void test3() {
Connection conn = DBUtil.open();
try {
CallableStatement cstmt = conn
.prepareCall("{call getnamebyid(?,?)}");
cstmt.setInt(1,7);
cstmt.registerOutParameter(2, Types.CHAR);
cstmt.execute();
String name=cstmt.getString(2);
cstmt.executeQuery();
System.out.println(name);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(conn);
}
}
}
0 0
- 课时7:CallableStatement语句
- 课时4:Statement语句
- 课时6:PrepareStatement语句
- CallableStatement
- CallableStatement
- SQL语句的执行:Statement、PrepareStatement、CallableStatement
- JDBC中的语句对象 statement prepareStatement CallableStatement
- SQL语句的执行:Statement、PrepareStatement、CallableStatement
- JDBCTM 指南:入门7-CallableStatement
- JDBCTM 指南:入门7-CallableStatement
- H5基础知识第九课时(JS判断语句)
- H5基础知识第九课时(JS循环语句)
- 第7课时《osg交互》
- 课时7 Linux的常用命令
- J2SE第三课时(运算符,表达式,控制语句)
- 实验教学内容(16课时)实验2选择语句练习
- CallableStatement(转载)
- CallableStatement 介绍
- poj 1362 Skew Binary 进制转换
- ubuntu安装phpMyAdmin
- iOS开发多线程篇—多线程简单介绍
- phantomjs基础
- Java知识点之单例设计模式
- 课时7:CallableStatement语句
- 2Char (Codeforces Round #329 (Div. 2) A)
- CSRF简单介绍
- 10009--淘宝开源dubbo
- [Leetcode]Swap Nodes in Pairs
- 非MFC的界面编程 - 复选框CHECKBOX
- PowerDesigner设置外键
- 疯转,值得一看-----最牛B的编码套路
- 如何判断一个div是否在页面可视区域内