Jul 30

JDBC: Stored Procedure

Sample stored procedure call using JDBC:Call stored procedure to change/set a value in the database

//set birthday - supply professor nametry{ String professor= "dylan thomas"; CallableStatement proc = connection.prepareCall("{ call set_birth_date(?, ?) }"); proc.setString(1, professor); proc.setString(2, '1950-01-01'); cs.execute();}catch (SQLException e){ // ....}Stored procedures can also return result/data to the caller: like//return birth dayconnection.setAutoCommit(false);CallableStatement proc = connection.prepareCall("{ ? = call birthday_when(?) }");proc.registerOutParameter(1, Types.Timestamp);proc.setString(2, professorName);cs.execute();Timestamp age = proc.getString(2);Stored procedures can also return complex data like resultsets. JDBC drivers from oracle, Sql Server, PostgreSQL support this.

Skip to toolbar