title: Oracle Java Stored Procedure Command-line Interaction
body: <p>I have a funny requirement.</p>
  
  <p>We need to have a command-line interaction in a Java Stored Procedure. In spite of granting appropriate permissions using the <code>dbms_java.grant_permission</code> commands, I am encountering <code>java.io.IOException</code>, where I read from <code>System.in</code> using <code>java.io.InputStreamReader</code>.</p>
  
  <p>Where is the problem?</p>
  
  <p>The Java Source is here:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  
  import java.sql.ResultSet;
  import java.sql.Statement;
  import java.sql.Connection;
  import java.sql.SQLException;
  
  
  import oracle.jdbc.driver.OracleDriver;
  
  public class ExecuteInteractiveBatch {
  
      public static void aFunction() {
  
          Connection connection = null;
          try {
              int rowFetched = 0;
  
              connection = new OracleDriver().defaultConnection();
  
              Statement stmt = connection.createStatement();
  
              ResultSet rs = stmt.executeQuery("SELECT count(1) cnt from sometable where c = 2");
  
              int count = 0;
              if (rs.next()) {
                  count = rs.getInt(1);
              }
              rs.close();
              stmt.close();
              rs = null;
              stmt = null;
              if (count == 1) {
                  System.out.println("Do you want to continue?");
                  BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                  String response = reader.readLine();
                  if ("Y".equalsIgnoreCase(response)) {
                      stmt = connection.createStatement();
                      int rowsAffected = stmt.executeUpdate("DELETE from sometable where c=2");
                      System.out.println("" + rowsAffected + " row(s) deleted");
                      stmt.close();
                  }
              }
          } catch (IOException ex) {
              ex.printStackTrace();
          } catch (SQLException ex) {
              ex.printStackTrace();
          } finally {
  
              try {
                  if (connection != null || !connection.isClosed()) {
                      connection.close();
                  }
              } catch (SQLException ex) {
                  ex.printStackTrace();
              }
          }
      }
  }
  </code></pre>