Connection connection = DriverManager.getConnection(databaseUrl, username, password); CallableStatement callableStatement = connection.prepareCall("{CALL getUserDetails(?)}"); callableStatement.setString(1, "John"); ResultSet resultSet = callableStatement.executeQuery();
Connection connection = DataSource.getConnection(); CallableStatement callableStatement = connection.prepareCall("{CALL getEmployeeDetails(?,?)}"); callableStatement.setInt(1, 123); callableStatement.registerOutParameter(2, Types.VARCHAR); callableStatement.execute(); String employeeName = callableStatement.getString(2);In this example, we obtain a connection to the database using a DataSource and create a CallableStatement object to call the stored procedure getEmployeeDetails. The procedure takes an integer parameter which we set using the setInt() method on the CallableStatement. We also register an output parameter using the registerOutParameter() method. Finally, we execute the procedure using execute() method and retrieve the output parameter value using the getString() method. The package library for the Connection class is java.sql.