Home > Database > Mysql Tutorial > body text

jdbc调用mysql存储过程实现代码_MySQL

WBOY
Release: 2016-06-01 13:23:55
Original
1308 people have browsed it

bitsCN.com 1. 创建存储过程
建立一个MySQL的存储过程 add_pro

delimiter //
drop procedure add_pro //
create procedure add_pro(a int , b int , out sum int )
begin
set sum = a * b;
end;
//

2. 调用存储过程

package com.zhanggaosong;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Types;
public class CallableStatementTest {
public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://127.0.0.1:3306/test";
public static final String USERNAME = "root";
public static final String PASSWORD = "123456";
public static void main(String[] args) throws Exception {
Class.forName(DRIVER_CLASS);
Connection connection = DriverManager.getConnection(URL, USERNAME,
PASSWORD);
String sql = "{CALL add_pro(?,?,?)}"; //调用存储过程
CallableStatement cstm = connection.prepareCall(sql); //实例化对象cstm
cstm.setInt(1, 122);
cstm.setInt(2, 2); //
cstm.registerOutParameter(3, Types.INTEGER); // 设置返回值类型
cstm.execute(); // 执行存储过程
System.out.println(cstm.getInt(3));
cstm.close();
connection.close();
}
}
bitsCN.com

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!