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)