In the project, you might encounter Sybase porting to MySQL, because Sybase supports stored procedures with mutable parameters, and MySQL cannot support it, so when you call MySQL, you need to have a few parameters for the stored procedure to reasonably configure the number of parameters:
Here is the code
Package Com.xxx.util;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.resultset;import Java.sql.statement;import Java.util.hashtable;import Java.util.map;import Java.util.regex.matcher;import Java.util.regex.pattern;public class Procedurehelper {/* is the development mode */static Boolean DEV = False;static map<string, integer> CACHE = new hashtable<string,integer> (100); static Pattern Pattern_args = Pattern.compile ("' \\s*\\ ((. *) \ \) \\s*begin", pattern.dotall| pattern.case_insensitive); static Pattern Pattern_arg = Pattern.compile ("(in|out|inout) [^-,]* (, |$)", pattern.dotall| pattern.case_insensitive);p rivate static int doWork (Connection con,string proc) throws Exception {Statement stmt = Null;s TMT = Con.createstatement (); 3. The Statement interface needs to be instantiated via the connection interface ResultSet rs = Stmt.executequery ("Show create PROCEDURE" +proc); if (Rs.next ()) { The third column is the code for the stored procedure, string = rs.getstring (3); Matcher m = pattern_args.matcher (code), if (M.find ()) {String ARGS = M.group (1), if (Args.matches ("\\s* ")) return 0;else{matcher m2 = pattern_arg.matcher (args); int num = 0;while (M2.find ()) {//system.out.println ( M2.group ()); num++;} return num;}} Else{throw new RuntimeException ("Stored procedure:" +proc+ "Cannot get the number of arguments by regular expression: \ n" +code);}} Rs.close (); Stmt.close (); throw new RuntimeException ("No stored Procedure");} /** * Note that in the parameters of the stored procedure, the comment cannot be written, especially in XXX, the format of the comment, otherwise it will be incorrectly recognized * @param con jdbc connection, and do not release resources within the function * @param stored procedure called proc * @param args Parameters passed to the stored procedure * @throws resolution failed * */public static int getprocedureargsnumber (Connection con,string proc) throws Exception {if (CO n = = NULL | | Proc = = null| | Proc.equals ("")) {throw new RuntimeException ("proc, con argument cannot be empty");} Here you can do cache return doWork (CON,PROC);} public static void Main (String arg[]) throws exception{connection con = null;//Represents the connection object of the database Class.forName ("COM.MYSQL.JDBC. Driver "); 1. Use class to load driver con = drivermanager.getconnection ("jdbc:mysql://127.0.0.1:3306/test?useunicode=true& Characterencoding=utf-8 "," root "," root "); 2. Connect Database System.out.println (Getprocedureargsnumber (Con, "My_procedurE ")); Con.close ();}}
The code's perception of the stored procedure is that it takes advantage of regular expressions, so at the time of the match, there may be some extreme situations, such as a statement with rules in the comments, but in general, this is rarely the case.
Number of Java-aware MySQL stored procedure variables