这是我的Java方法:
public static List<Company> selectCompanies() {
List<Company> companies=new ArrayList<Company>();
CallableStatement cs = null;
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/java?user=root&password=root";
try {
conn = DriverManager.getConnection(url);
cs = conn.prepareCall("{call select_companies()}");
ResultSet rs=cs.executeQuery();
while(rs.next()){
Company company=new Company();
company.setId(rs.getInt(1));
company.setName(rs.getString(2));
companies.add(company); }
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
cs.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return companies;
}
这是MySql db上的存储过程:
CREATE PROCEDURE select_companies ()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE o_id SMALLINT;
DECLARE o_name VARCHAR(52);
DECLARE cur1 CURSOR FOR SELECT ID,NAME FROM b_company;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO o_id, o_name;
IF done THEN
LEAVE read_loop;
END IF;
END LOOP;
CLOSE cur1;
当我尝试运行java代码时,我一直得到“结果集来自UPDATE.No数据”异常。我是存储过程的新手,我无法弄清楚我在这里缺少什么。
答案 0 :(得分:1)
正如 Luiggi 提到的那样你需要返回结果,在你的情况下我假设它是o_id
和o_name
:
CREATE PROCEDURE select_companies ()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE o_id SMALLINT;
DECLARE o_name VARCHAR(52);
DECLARE cur1 CURSOR FOR SELECT ID,NAME FROM b_company;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO o_id, o_name;
SELECT o_id, o_name;
IF done THEN
LEAVE read_loop;
END IF;
END LOOP;
CLOSE cur1;