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