在java中调用存储过程[重复]

时间:2023-01-19 10:19:54

This question already has an answer here:

这个问题在这里已有答案:

delimiter//
create procedure SearchByTitle(IN var1(50),OUT output varchar(500))
Begin
Select * From Poem;
End//

I want to create a stored procedure so that it will display all details on poem based on my input. how do i go about calling this in java?

我想创建一个存储过程,以便它根据我的输入显示诗的所有细节。我如何在java中调用它?

1 个解决方案

#1


1  

you can use a Callable JDBC Statement like this.

你可以像这样使用Callable JDBC Statement。

 public static void main(String[] args) throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driver).newInstance();

    System.out.println("Connecting to database...");
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");

    //callable statement
    CallableStatement cstmt = conn.prepareCall("{call SearchByTitle(?,?)}");
    cstmt.setInt(1, "first param value");
    cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
    cstmt.execute();

    //getting output param value
    String empName = cstmt.getString(2);
    System.out.println(empName);

    conn.close();

}

#1


1  

you can use a Callable JDBC Statement like this.

你可以像这样使用Callable JDBC Statement。

 public static void main(String[] args) throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driver).newInstance();

    System.out.println("Connecting to database...");
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");

    //callable statement
    CallableStatement cstmt = conn.prepareCall("{call SearchByTitle(?,?)}");
    cstmt.setInt(1, "first param value");
    cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
    cstmt.execute();

    //getting output param value
    String empName = cstmt.getString(2);
    System.out.println(empName);

    conn.close();

}