Trying to execute a stored procedure using JDBC, I can't seem to get past this error,
java.sql.SQLException: Parameter number 1 is not an OUT parameter
public static void SP1() throws SQLException, IOException {
try {
stmt = conn.createStatement();
stmt.executeUpdate("DROP PROCEDURE IF EXISTS sp1;");
stmt.executeUpdate("CREATE PROCEDURE sp1(OUT cName VARCHAR(20))"
+ "SELECT CuratorName FROM curator "
+ "WHERE CuratorPhone = \"90394857\" INTO cName;");
conn.commit();
System.out.println("Sp1 created");
CallableStatement cs3 = conn.prepareCall("{call sp1(?)}");
cs3.registerOutParameter(1, java.sql.Types.VARCHAR);
cs3.execute();
String result = cs3.getString("cName");
System.out.println("Result from sp1:" + result);
} catch (Exception e) {
System.out.println(e);
}
}
If your Stored Procedure definition contains an IN parameter looking at the code you have shared, then the Call statement should look like this
CallableStatement cs3 = conn.prepareCall("{call sp1(?)}");
cs3.setString(1, java.sql.Types.VARCHAR);
The parameter needs to be set as an IN Parameter.
If your Stored Procedure definition contains an OUT Parameter then your call statement needs to be modified.
CallableStatement cs3 = conn.prepareCall("{? = call sp1()}");
cs3.registerOutParameter(1, java.sql.Types.VARCHAR);
In any case, share your procedure definition, that would make it clearer.
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.