(6) JDBC的CallableStatement对象使用
来源:互联网 发布:米尔斯海默 知乎 编辑:程序博客网 时间:2024/06/01 08:55
调用数据库中的存储过程,可以使用CallableStatement对象。
通过Connection对象的prepareCall()方法,创建CallableStatement对象,
调用存储过程
通过execute()或executeQuery()方法执行。
eg:
1. 建表
CREATE TABLE `t_user_main` ( `oid` varchar(32) NOT NULL COMMENT '主键', `userName` varchar(255) DEFAULT NULL COMMENT '用户名', `userPwd` varchar(255) DEFAULT NULL COMMENT '登陆密码', PRIMARY KEY (`oid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2. 插入数据
INSERT INTO t_user_main (oid, userName,userPwd)VALUES ('1','one','111111'),('2','two','222222');
3. 编写测试的存储过程
# 每次调用,插入一条测试数据DELIMITER $$CREATE PROCEDURE user_main_pro()BEGIN INSERT INTO t_user_main (oid, userName, userPwd) VALUES (REPLACE(UUID(),'-',''), 'testPro', 'test000000');END $$DELIMITER;
4. java代码调用存储过程
package com.lanhuigu.JavaBase.jdbc;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;public class JDBCCallableStatement {public static void main(String[] args) {String user = "root"; String password = "lanhuigu"; /* * useUnicode=true:表示使用Unicode字符集 * characterEncoding=UTF8:字符编码方式 * allowMultiQueries=true:是否允许批量处理 */ String url = "jdbc:mysql://127.0.0.1:3306/lanhuigu_web?useUnicode=true&characterEncoding=UTF8&allowMultiQueries=true"; String driver = "com.mysql.jdbc.Driver"; try { //1.加载驱动程序 Class.forName(driver); //2.建立连接 Connection conn = DriverManager.getConnection(url, user, password); //3.创建CallableStatement对象 CallableStatement cs = conn.prepareCall("{call user_main_pro()}"); //4.执行调用存储过程 ResultSet rs = cs.executeQuery(); //5.打印查询结果 //6.关闭连接 conn.close(); } catch (Exception e) { System.out.println("数据库连接失败"); }}}
当运行程序之后,可以看到t_user_main表中多了一条测试数据。
阅读全文
0 0
- (6) JDBC的CallableStatement对象使用
- JDBC之CallableStatement 的使用
- JDBC 之 CallableStatement 对象
- JDBC中操作数据库的三个对象:Statement;PreparedStatement;CallableStatement
- JDBC中Statement、PreparedStatement和CallableStatement的使用
- JDBC中的语句对象 statement prepareStatement CallableStatement
- 创建并使用CallableStatement对象;
- JDBC CallableStatement
- jdbc----CallableStatement
- JDBC之Statement,PreparedStatement,CallableStatement的区别
- JDBC--Statement,PreparedStatement,CallableStatement的区别
- JDBC--Statement,PreparedStatement,CallableStatement的区别
- JDBC--Statement,PreparedStatement,CallableStatement的区别
- JDBC--Statement,PreparedStatement,CallableStatement的区别
- JDBC之Statement,PreparedStatement,CallableStatement的区别
- JDBC Statement,PreparedStatement,CallableStatement的区别
- JDBC:Statement,PreparedStatement,CallableStatement的区别
- JDBC之Statement,PreparedStatement,CallableStatement的区别
- 06_高级painter、保存、打印、预览
- Mina实时框架的使用
- MongoDB基本的操作及C接口使用-MongoDB C Driver
- 树莓派(Raspberry Pi 3)
- windows程序设计——多重文件介面
- (6) JDBC的CallableStatement对象使用
- Spark编程指南
- Unix时间与Excel时间转换
- 决策树转规则
- 牛客网-剑指offer
- Ubuntu64位下搭建javaWeb小型服务器
- vue2.0 axios前后端数据处理
- 大学回忆录
- HDU 4135 Co-prime (容斥原理)