简体   繁体   English

JDBC存储过程调用

[英]JDBC stored procedure call

I created a stored procedure as follows: 我创建了一个存储过程,如下所示:

//Stored procedures in mysql // mysql中的存储过程

DELIMITER $$
CREATE PROCEDURE `bank`.`hello` (in id varchar(20),in pass varchar(20))
BEGIN
 insert into admin(username,password) values (id,pass);
END $$
DELIMITER ;

and I am calling the stored procedure in java as follows: 并且我在java中调用存储过程,如下所示:

//calling stored procedures using JDBC //使用JDBC调用存储过程

package StoredProcEx;
import java.sql.*;
public class StoredProcEx {
    public static void main(String arg[]){
    try{

    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bank","root","root");
    //PreparedStatement stmt=con.prepareStatement("insert into admin values(?,?)");
    CallableStatement stmt=con.prepareCall("{?=call hello(?,?)}");
    stmt.setString(1,"birender");
    stmt.setString(2,"admin");
    stmt.execute();
    //stmt.execute();
    }
    catch(ClassNotFoundException ce){
    ce.printStackTrace();
    }
    catch(SQLException se){
     se.printStackTrace();
    }
    catch(Exception e){}
    }
}

But it is showing a compile time exception as follows: 但是它显示了一个编译时异常,如下所示:

Can's set IN parameter for return value of stored function call. 可以为存储的函数调用的返回值设置IN参数。

Please use 请用

CallableStatement stmt=con.prepareCall("{call hello(?,?)}");

instead of 代替

CallableStatement stmt=con.prepareCall("{?=call hello(?,?)}");

First ? 首先? in your code is used for return type 在您的代码中用于返回类型

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

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