A Java servlet calls a stored procedure with multiple returned result sets

Source: Internet
Author: User

One, MySQL stored procedures

Here I first say the logic of the implementation of this function and some of the problems encountered in the way. This stored procedure has a total of two input parameters, a total of two tables related to the query, each parameter corresponds to a query table in a judgment, so altogether returned two result sets (of course, to return more result sets is the same, if you need to judge, continue to increase the stored procedure parameters, if you do not need to judge directly in the stored procedure, Add the SQL statement for the query). There is a simpler way to do this, and you can write SQL association statements to query out the results of two tables and return a combined result set. I'm certainly here to achieve this effect, so it complicates its implementation. Continue to say that I have a problem in MySQL today, what is the reason, in fact, I do not know now, before writing this stored procedure, I have to query the table to add two fields, and then modify the name of a field, the addition of the field to no effect, but changed the name of the field is a problem, When you call this stored procedure in Java, you are prompted that the field does not exist. After that I changed the modified field back to just fine, the following paste stored procedure code.

1. Parameters

In Sheetoneaccount varchar (+), in Sheettwoaccount varchar (50)

2. Code

1 BEGIN2 /*Stored Procedures*/3 DeclareSharenameisornoexistsverifyvarchar( -);4    /*Verify that the first parameter exists in the first table*/5    SELECTName intoSharenameisornoexistsverify fromInfoSheet6     whereSheetoneaccount=name;7       if(sharenameisornoexistsverify is  not NULL) Then8            /*This adds a field to the returned query result table so that after the client accepts it, it first takes the field's9 value to determine whether the user exists, if it exists to fetch the value of the corresponding field, reducing the workload of the client*/Ten                    /*here nameexists as 0 indicates that the user exists with the table, and that the first table*/ One         Select *,0  asNameexists fromInfoSheetwhereName=Sheetoneaccount; A       ELSE  -                /*The following query person does not exist, the returned custom field value is 1, 1 as nameexists, indicating that the query user does not exist, - and it means the first sheet .*/ the  -   /*The following indicates that the person is not queried, although not queried but still return a piece of data, so in the value, you need to first take nameexists this field - determine if a queried person exists*/ -        SelectName1  asNameexists fromInfoSheet limit0,1; +      End if; -     /*Verify that the second parameter exists in the second table*/ +   SetSharenameisornoexistsverify=NULL; A   SELECTName intoSharenameisornoexistsverify fromStudentsheet at     whereName=Sheettwoaccount; -     if(sharenameisornoexistsverify is  not NULL) Then -            /*This adds a field to the returned query result table so that after the client accepts it, it first takes the field's - value to determine whether the user exists, if it exists to fetch the value of the corresponding field, reducing the workload of the client*/ -                    /*here Nameexists as 2 indicates that the user exists in the table and represents the second table*/ -         Select *,2  asNameexists fromStudentsheetwhereName=Sheettwoaccount; in       ELSE  -        /*The returned custom field value is 3, 3 as nameexists, indicating that the query user does not exist, to and it means the second table .*/ +  -     /*The following indicates that the person is not queried, although not queried but still return a piece of data, so in the value, you need to first take nameexists this field the determine if a queried person exists*/ *             SelectName3  asNameexists fromStudentsheet limit0,1; $      End if;Panax Notoginseng END

One, Java Servlet

1. Call the stored procedure method

1  //call a stored procedure with multiple returned result sets2      //Although this is the result set of the query, but I write in the database is that the query only one piece of data, so do not need to use re.next traversal in the method3     Public Staticjsonarray callprocreturnmultipleset (String sql, string[] parameters) {4Jsonarray Masterjsonarray =NewJsonarray ();5Jsonobject sharejsonobject=NewJsonobject ();6        Try {7conn =getconnection ();8CS =conn.preparecall (SQL); 9               for(inti=0;i<parameters.length;i++)Ten                 { OneCs.setobject (i+1, Parameters[i]);  A                 } - Cs.execute (); -rs =Cs.getresultset (); the             //If you are returning more than one piece of data here, you need to use Jsonarray to receive it.  -sharejsonobject=Resulttojsontool.resultsettojsonobject (RS); - Masterjsonarray.put (sharejsonobject); -             +             //The following method is to continue to loop out of the RS data set table, Java this function did not. NET good,. NET returns a dataset using the corresponding data set directly with the subscript -             //If you've added more tables to your query, you're not going to use this method to loop out the dataset. +            if(Cs.getmoreresults () = =true) { Ars =Cs.getresultset (); atsharejsonobject=Resulttojsontool.resultsettojsonobject (RS); - Masterjsonarray.put (sharejsonobject); -                  //below if there is a third party that table, and so on, the same method, or you can write a recursive method here to encapsulate less code -                  //if (cs.getmoreresults () = = True) { -                   -                 // } in                  -             }  to              +              -         } the         Catch(Exception e) { * e.printstacktrace (); $             Throw Newruntimeexception (E.getmessage ());Panax Notoginseng}finally{ -             the         } +         returnMasterjsonarray; A}

2. Call

1 // call a stored procedure that returns multiple result sets 2            String[] getresultsetparameter=new string[]{"hang", "haha"}; 3            String  executproducestr= "Call Getmultipleresultsetprocudure (?,?)" ; 4            Jsonarray rest=(jsonarray) mysqlhepler.callprocreturnmultipleset (Executproducestr, getResultSetParameter); 5            Out.println (Rest.tostring ());

3. Realize

Because I also began to learn Java, if there is unreasonable, please timely pointed out.

A Java servlet calls a stored procedure with multiple returned result sets

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.