Java servlet calls a stored procedure with multiple returned result sets, assumervlet
I. mysql Stored Procedure
Here I will talk about the logic of implementing this function and some problems encountered on the way. This stored procedure has two input parameters associated with the query of two tables. Each parameter corresponds to a judgment in the query table, therefore, a total of two result sets are returned (of course, the same is true for returning more results sets. If you need to determine the result sets, you can continue to add the stored procedure parameters, if no judgment is required, add the SQL statement to be queried directly in the stored procedure ). To achieve this function, you can write an SQL join statement to query the results of two tables and return a combined result set. Of course I want to achieve this effect, so I complicate it. Let's continue to talk about a problem I encountered in mysql today. What is the cause? In fact, I have not figured out yet. before writing this stored procedure, I added two fields to the table to be queried, and modified the name of a field. The added field has no effect, but the field with the name changed has a problem, when calling this stored procedure in java, the system prompts that this field does not exist. Then I will modify the modified field and paste the Stored Procedure Code below.
1. Parameters
In sheetOneAccount varchar (50), in sheetTwoAccount varchar (50)
2. Code
1 BEGIN 2/* Stored Procedure */3 declare shareNameIsOrNoExistsVerify varchar (50 ); 4/* verify whether the first parameter exists in the first table */5 SELECT name into shareNameIsOrNoExistsVerify from infosheet 6 where sheetOneAccount = name; 7 if (shareNameIsOrNoExistsVerify is not null) THEN 8/* here, a field is added to the returned query result table so that the client can determine whether the user exists by taking the 9 value of this field after receiving it, if yes, the value of the corresponding field is retrieved, reducing the workload on the client */10/* Here nameExists AS 0 indicates that the user exists and the table, and indicates the first table */11 select *, 0 AS nameExists from infosheet where name = sheetOneAccount; 12 ELSE 13/* the query personnel below does not exist. The returned Custom Field Values are 1 and 1 AS nameExists, indicating that the query user does not exist, 14. In addition, the first table */15 16/* indicates that the person is not queried. Although the person is not queried, a data record is returned. Therefore, first, take the nameExists field 17 to check whether the queried person exists */18 select name, 1 AS nameExists from infosheet limit; 19 end if; 20/* verify whether the second parameter exists in the second table */21 set shareNameIsOrNoExistsVerify = null; 22 SELECT name into shareNameIsOrNoExistsVerify from studentsheet23 where name = sheetTwoAccount; 24 if (shareNameIsOrNoExistsVerify is not null) THEN25/* adds a field to the returned query result table so that the client can determine the result by taking the 26 value of this field, query whether the user exists. If the user exists, the value of the corresponding field is obtained. This reduces the workload on the client */27/*. Here nameExists AS 2 indicates that the user exists in the table, it also indicates the second table */28 select *, 2 AS nameExists from studentsheet where name = sheetTwoAccount; 29 ELSE 30/* The returned Custom field value is 3, 3 AS nameExists, the query user does not exist. 31 indicates that the user is not found in the second table */32 33/*. Although the user is not found, a data record is returned, therefore, when the value is set, take the nameExists field 34 to check whether the queried person exists */35 select name, 3 AS nameExists from studentsheet limit 0, 1; 36 end if; 37 END
I. java Servlet
1. Call the Stored Procedure Method
1 // call the stored procedure with multiple returned result sets. 2 // although this is the query result set, my judgment in the database is that only one piece of data is queried, therefore, you do not need to use re in the method. next traverse 3 public static JSONArray callProcReturnMultipleSet (String SQL, String [] parameters) {4 JSONArray masterJSONArray = new JSONArray (); 5 JSONObject shareJSONObject = new JSONObject (); 6 try {7 conn = getConnection (); 8 cs = conn. prepareCall (SQL); 9 for (int I = 0; I <parameters. length; I ++) 10 {11 cs. setObject (I + 1, parameters [I]); 12} 13 cs.exe cute (); 14 rs = cs. getResultSet (); 15 // if multiple data entries are returned, JSONArray is used to receive them. 16 shareJSONObject = ResultToJsonTool. resultSetToJsonObject (rs); 17 masterJSONArray. put (shareJSONObject); 18 19 // The following method continues to loop out the dataset table in rs. java does not provide this function. NET is good ,. NET returns a dataSet and uses the subscript to retrieve the corresponding dataSet 20 // if you add more tables in the query, continue to use the following method to loop out the dataSet 21 if (cs. getMoreResults () = true) {22 rs = cs. getResultSet (); 23 shareJSONObject = ResultToJsonTool. resultSetToJsonObject (rs); 24 masterJSONArray. put (shareJSONObject); 25 // if there is a third-party table below, and so on, the same method, or here you can write a recursive method to encapsulate less code 26 // if (cs. getMoreResults () = true) {27 28 //} 29 30} 31 32 33} 34 catch (Exception e) {35 e. printStackTrace (); 36 throw new RuntimeException (e. getMessage (); 37} finally {38 39} 40 return masterJSONArray; 41}
2. Call
1 // call the 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. Implementation