简体   繁体   中英

Java mysql next result from stored procedure

I have following stored procedure:

CREATE DEFINER=`root`@`localhost` PROCEDURE `blast10`()
BEGIN
SELECT concat("ROUND ",draws.izvlacenje_id,": ", draws.1,",",draws.2,",",draws.3,",",draws.4,",",draws.5,",",draws.6,",",draws.7,",",draws.8,",",draws.9,",",draws.10,",",draws.11,",",draws.12,",",draws.13,",",draws.14,",\n",draws.15,",",draws.16,",",draws.17,",",draws.18,",",draws.19,",",draws.20,",",
draws.21,",",draws.22,",",draws.23,",",draws.24,",",draws.25,",",draws.26,",",draws.27,",",draws.28,",",draws.29,",",draws.30,",",draws.31,",",draws.32,",",draws.33,",",draws.34,",",draws.35,"")
rzlt from macau.draws order by iddraws desc limit 10;
END`

This procedure returns 10 rows of data and i want to send that data through TCP/IP communication, this is what i achieved by now:

public void blastTen() throws SQLException {
    String blastTen = "";
    String bl10 = "LAST 10 RESULTS";
    try {
        Statement st = Conex.conn.createStatement();
        String SQLEdit = "{ call blast10() }";
        ResultSet rs = st.executeQuery(SQLEdit);
        while (rs.next()) {
            blastTen = bl10 + "\n" + rs.getString("rzlt") + "\n";
        }
        os.println(blastTen + "\n");
    }
    catch (Exception e2) {
        System.out.println(e2);
    }
}

The problem is I can send only one row and can't seem to get the other rows.

Any help is appreciated

The code

while (rs.next())
{
    blastTen=bl10+"\n"+rs.getString("rzlt")+"\n";
}

won't assign anything to bl10 so you'll always only get the latest row... Try

   ...
   bl10 = bl10 + "\n" + rs.getString( "rzlt" );
}
os.println( bl10 );

instead.

Cheers,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM