1、创建函数
#创建函数
create function fun_add(x1 int,x2 int) returns INT
begin
return x1 + x2;
end
#查看函数
show function status;
show function status like '%add%'
#调用函数
select fun_add(1,2)
2、Java调用函数
@org.junit.Test
public void testfun_add() throws Exception {
long start = System.currentTimeMillis();
//创建连接
String driverClass = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/mysql_jdbc";
String username = "root";
String password = "root";
Class.forName(driverClass);
Connection con = DriverManager.getConnection(url, username, password);
//关闭自动提交
con.setAutoCommit(false);
//调用存储过程
CallableStatement cst = con.prepareCall("{? = call fun_add(?,?)}");
//设置输入参数
cst.setInt(2,100);
cst.setInt(3,300);
cst.registerOutParameter(1,Types.INTEGER);
//执行存储过程
cst.execute();
int result = cst.getInt(1);
System.out.println(result);
//提交
con.commit();
cst.close();
con.close();
long time = System.currentTimeMillis() - start;
System.out.println(time);//2085
}
欢迎关注我的公众号:小秋的博客
CSDN博客:https://blog.csdn.net/xiaoqiu_cr
github:https://github.com/crr121
联系邮箱:rongchen633@gmail.com
有什么问题可以给我留言噢~