简体   繁体   中英

how to use mysql stored procedure in java with dynamic arugments?

在此处输入图片说明 there are three fixed arguments in this little Java project, and the first two arguments are all fixed arguments. And I want to know, if there is a way to use stored procedure with the arguments that is not fixed in the project. For example I want to execute the procedure with arguement v1, and I think I might use "CallableStatement cstmt = conn.prepareCall("{call proc_2(v1,'b',?)}"); But obviously it's wrong. So my problem is how the sql sentence can get the varibale from java?

You can use question marks as placeholders for dynamic arguments too. For example:

CallableStatement cstmt = conn.prepareCall("{call proc_2('a', ?, ?)}");
cstmt.setString(1, aString);
cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
boolean i = cstmt.execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM