设为首页 加入收藏

TOP

JDBC对MySQL数据库存储过程的调用(二)
2015-07-24 11:08:15 来源: 作者: 【 】 浏览:2
Tags:JDBC MySQL 数据库 存储 过程 调用
onnection connection = LinkDB.getMySqlConnection(); String proStr = "{call outTwoParam(?,?)}"; CallableStatement callableStatement = connection.prepareCall(proStr); callableStatement.registerOutParameter(1, Types.VARCHAR); callableStatement.registerOutParameter(2, Types.DECIMAL); callableStatement.execute(); String fruitName = callableStatement.getString(1); double fruitPrice = callableStatement.getDouble(2);// 获取DECIMAL类型的属性要使用getDouble方法。 System.out.println("水果名称:" + fruitName +"、水果价格:" + fruitPrice + "元"); LinkDB.close(connection, callableStatement, null); } }

4、含有一个输入参数和一个输出参数的存储过程

package com.ghj.packageoftest;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

import com.ghj.packageoftool.LinkDB;

/*
运行本程序前需执行的脚本:
DELIMITER $$
CREATE PROCEDURE inOneParamAndOutOneParam(IN fruitName VARCHAR(12),OUT fruitPrice DECIMAL(7,3))
BEGIN
SELECT price FROM fruit WHERE NAME=fruitName INTO fruitPrice;
END $$
DELIMITER ;
*/
 
/**
 * 含有一个输入参数和一个输出参数的存储过程
 * 
 * @author GaoHuanjie
 */
public class InOneParamAndOutOneParam {
	public static  void main(String args[]) throws SQLException {
		Connection connection=LinkDB.getMySqlConnection();
		CallableStatement callableStatement=null;
		String procStr="{call inOneParamAndOutOneParam(?,?)}";
		callableStatement=connection.prepareCall(procStr);
		String fruitName = "莲雾";
		callableStatement.setString(1, fruitName);
		callableStatement.registerOutParameter(2, Types.DECIMAL);
		callableStatement.execute();
		double fruitPrice=callableStatement.getDouble(2);//获取DECIMAL类型的属性要使用getDouble方法。
		System.out.println(fruitName+"的价格为:"+fruitPrice+"元");
		LinkDB.close(connection, callableStatement, null);
	}
}

5、输入参数即输出参数的存储过程

package com.ghj.packageoftest;

import java.sql.*;

import com.ghj.packageoftool.LinkDB;

/*
 运行本程序前需执行的脚本:
DELIMITER $$
CREATE PROCEDURE inOneParamISOutOneParam(INOUT fruitName VARCHAR(12))
BEGIN
SELECT NAME INTO fruitName FROM fruit WHERE NAME LIKE CONCAT('%', fruitName, '%') LIMIT 0,1;
END $$
DELIMITER ;
或
DELIMITER $$
CREATE PROCEDURE inOneParamISOutOneParam(INOUT fruitName VARCHAR(12))
BEGIN
SELECT NAME FROM fruit WHERE NAME LIKE CONCAT('%', fruitName, '%') LIMIT 0,1 INTO fruitName;
END $$
DELIMITER ;

注意上面查询语句不能这样写:SELECT NAME FROM fruit WHERE NAME LIKE CONCAT('%', fruitName, '%') INTO fruitName LIMIT 0,1;
注意:对比3、4和5Java文件内创建存储过程脚本中“INTO”关键字的位置你一定深有收获,呵呵呵,偷点懒,在此就不总结了。
*/

/**
 * 输入参数即输出参数的存储过程
 * 
 * @author GaoHuanjie
 */
public class InOneParamISOutOneParam {
	public static void main(String args[]) throws SQLException {
		Connection con = LinkDB.getMySqlConnection();
		CallableStatement callableStatement = null;
		String procStr = "{call inOneParamISOutOneParam(?)}";
		callableStatement = con.prepareCall(procStr);
		callableStatement.setString(1, "莲");
		callableStatement.registerOutParameter(1, Types.VARCHAR);
		callableStatement.execute();
		String fruitName = callableStatement.getString(1);
		System.out.println("表中水果名称含有‘莲’字的一中水果的名称是:" + fruitName);
		LinkDB.close(con, callableStatement, n
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇mysql在linux上的安装之一yum安装 下一篇MySQL性能优化(二)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Java 学习线路图是怎 (2025-12-25 15:19:15)
·关于 Java 学习,有 (2025-12-25 15:19:12)
·有没有Java swing教 (2025-12-25 15:19:09)
·Start, Stop, and Di (2025-12-25 14:50:57)
·C语言入门教程:零基 (2025-12-25 14:50:54)