简体   繁体   English

如何在Java中使用mysql存储过程进行动态修饰?

[英]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. 这个小Java项目中有三个固定参数,而前两个参数都是固定参数。 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? 例如,我想用争论v1执行该过程,我想我可能会使用“ CallableStatement cstmt = conn.prepareCall(“ {call proc_2(v1,'b',?)}”));但是显然这是错误的。所以我问题是sql语句如何从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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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