这是我的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 data”异常。我是存储过程的新手,我无法弄清楚我在这里缺少什么。