我在MySQL工作台中创建了以下过程:
create procedure city(in CID int, out CName varchar(35), out CPopulation int)
begin
select name, Population into CName, CPopulation from city where ID = CID;
end//
以下是我的java代码:
public class CallableStmts {
public static void main(String[] args) {
Connection con = null;
CallableStatement st = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/world","root","password");
String sql ="{call city(?,?,?)}";
st = con.prepareCall(sql);
int CID = 1;
st.setInt(1, CID);
st.registerOutParameter(2, Types.VARCHAR);
st.registerOutParameter(3, Types.INTEGER);
st.execute();
String name = st.getString(2);
int Pop = st.getInt(3);
System.out.printf("%s %d", name, Pop);
st.close();
con.close();
}catch(Exception e)
{
System.out.println(e);
}finally
{
try{
if(st!=null)
st.close();
}catch(SQLException se2){ }
try{
if(con!=null)
con.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
但在执行时我得到以下错误:
java.sql.SQLException:参数编号2不是OUT参数
即使我将可调用语句更改为 - > String sql ="{? = call city(?,?,?)}";
我收到新错误:
java.sql.SQLException:无法为存储的函数调用的返回值设置IN参数。