JDBC handles multiple result sets returned by MySQL procedure

Source: Internet
Author: User

1: Test database table User

mysql> desc user$$+-------+-------------+------+-----+---------+----------------+| Field | Type        | Null | Key | Default | Extra          ID    int(one)     | NO   | PRI | NULL    | auto_increment | | name  | varchar | YES  |     | NULL    |                | | age   int(one)     | YES  |     | NULL    |                | +-------+-------------+------+-----+---------+----------------+3 in Set (0.00 sec )

2: Stored procedure, returns two result sets of id>n_id and id<n_id

delimiter $$ Create procedure p_get_user_list (inint.)begin    selectfrom  userwhere>  n_id;     Select  from User where < n_id; End $$

3:JDBC operation

/** To change the license header, choose License Headers in the Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ Packagejdbctest;Importjava.sql.CallableStatement;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;ImportJava.util.logging.Level;ImportJava.util.logging.Logger;/** * * @authory*/ Public classJdbctest {/**     * @paramargs the command line arguments*/     Public Static voidMain (string[] args) {//TODO Code application logic herefuntest (); }             Public Static voidfuntest () {Connection conn=NULL; CallableStatement calstate=NULL; ResultSet RS=NULL; Try{class.forname ("Com.mysql.jdbc.Driver"); Conn= (Connection) drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test", "Root", "" "); Calstate= Conn.preparecall ("{Call P_get_user_list (?)}")); Calstate.setint (1, 3); BooleanOprflag =Calstate.execute (); //use outer loops to control the number of result sets, within which loops control the records of each result set             while(Oprflag) {RS=Calstate.getresultset (); System.out.println ("=================");  while(Rs.next ()) {System.out.println ("ID:" +rs.getint ("id") + "\ T" + "Name:" +rs.getstring ("name") + "\ T" + "Age:" The +rs.getint ("Age")); } Oprflag=calstate.getmoreresults (); }        }Catch(ClassNotFoundException |SQLException ex) {Logger.getlogger (jdbctest).class. GetName ()). log (Level.severe,NULL, ex); }finally{            if(NULL!=RS) {                Try{rs.close (); } Catch(SQLException ex) {Logger.getlogger (jdbctest.class. GetName ()). log (Level.severe,NULL, ex); }            }            if(NULL!=calstate) {                Try{calstate.close (); } Catch(SQLException ex) {Logger.getlogger (jdbctest.class. GetName ()). log (Level.severe,NULL, ex); }            }            if(NULL!=conn) {                Try{conn.close (); } Catch(SQLException ex) {Logger.getlogger (jdbctest.class. GetName ()). log (Level.severe,NULL, ex); }            }        }            }    }

4: Test Results

run:=================ID:4    name:test2 age    :ID:5    name:test3 Age    :=================ID:1    name: Reese Age    :25  ID:2    name: Harry Age    :0 seconds)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.