Call a procedure with one IN/OUT parameter in Java
Description
The following code shows how to call a procedure with one IN/OUT parameter.
Example
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Types;
/*from w w w . ja v a 2 s . c o m*/
public class Main {
public static void main(String[] args) throws Exception {
Connection connection = getConnection();
CallableStatement cs = connection.prepareCall("{call myprocinout(?)}");
// Register the type of the IN/OUT parameter
cs.registerOutParameter(1, Types.VARCHAR);
// Set the value for the IN/OUT parameter
cs.setString(1, "a string");
// Execute the stored procedure and retrieve the IN/OUT value
cs.execute();
String outParam = cs.getString(1); // OUT parameter
System.out.println(outParam);
}
private static Connection getConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:mem:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
}