Stored Procedure accepts two IN parameters and two OUT parameters. - Java JDBC

Java examples for JDBC:Stored Procedure

Description

Stored Procedure accepts two IN parameters and two OUT parameters.

Demo Code

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;

public class Main {
  public static void main(String[] args) {
    try {/*from  w w w .  java  2 s  .  c om*/
      Connection conn = null;// JDBCUtil.getConnection();
      String sql = "{call give_raise(?, ?, ?, ?)}";

      CallableStatement cstmt = conn.prepareCall(sql);

      // Register the OUT parameters: old_income(index 3), new_income(index 4)
      cstmt.registerOutParameter(3, Types.DOUBLE);
      cstmt.registerOutParameter(4, Types.DOUBLE);

      // Set values for person_id at index 1 and for raise at index 2
      cstmt.setInt(1, 1001);
      cstmt.setDouble(2, 4.5);

      // Execute the stored procedure
      cstmt.execute();

      // Read the values of the OUT parameters old_income(index 3)
      // and new_income (index 4)
      double oldIncome = cstmt.getDouble(3);
      double newIncome = cstmt.getDouble(4);
      System.out.println("Old Income:" + oldIncome);
      System.out.println("New Income:" + newIncome);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Result


Related Tutorials