在名为chapter06的数据库中成功创建了一个存储过程,代码如下:
create procedure addcount (out count int)
begin
declare itmp int;
declare cur_id cursor for select id from stu;
declare exit handler for not found close cur_id;
select count(*) into count from stu;
set @sum =0;
open cur_id;
repeat
fetch cur_id into itmp;
if itmp <10
then set @sum = @sum +itmp;
end if;
until 0 end repeat;
close cur_id;
end //
然后编写了一个Java程序,连接到chapter06,执行存储过程,代码如下:
import java.sql.*;
public class TestProc {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection cnn = DriverManager.getConnection("jdbc:mysql://localhost/chapter06?user=root&password");
CallableStatement cs = cnn.prepareCall("{call addcount(?)");
cs.registerOutParameter(1, Types.INTEGER);
cs.execute();
System.out.println(cs.getInt(1));
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行该程序,命令行窗口提示java.sql.SQLException: Parameter number 1 is not an OUT parameter,请问怎么回事?
漏掉了一个 }
修改前
CallableStatement cs = cnn.prepareCall("{call addcount(?)");
修改后
CallableStatement cs = cnn.prepareCall("{call addcount(?)}");
https://zhidao.baidu.com/question/1638187235659335420.html
因为 CallableStatement cs = cnn.prepareCall("{call addcount(?)");
在此处少了一个 '}' 导致查找 存储过程不存在 所以报错
因为 CallableStatement cs = cnn.prepareCall("{call addcount(?)");在这少了一个 '}'
端口号没写也可以连接吗
jdbc:mysql://localhost:3306/chapter06?user=root&password
CallableStatement cs = cnn.prepareCall("{call addcount(?)");
在此处少了一个 '}'