1

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

4

1 回答 1

1

正如Luiggi所提到的,您需要返回结果,在您的情况下,我假设它o_ido_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;
于 2013-06-05T14:35:16.353 回答