here is my Java method:
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;
}
Here is stored procedure on 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;
When i try to run the java code i keep getting "resultset is from UPDATE.No data" exception. I am new to stored procedures and i cant figure out what i am missing here.
As Luiggi well mentioned you need to return the results , in your case i assume its o_id
and 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;