Processing a ResultSet Produced by a Stored Procedure - Java JDBC

Java examples for JDBC:ResultSet

Description

Processing a ResultSet Produced by a Stored Procedure

Demo Code

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main{
  public static void main(String[] args) {
    Connection conn = null;/*ww  w  .j ava 2 s  .c om*/
    try {
      conn = JDBCUtil.getConnection();

      // Print details for person_id 101
      printPersonDetails(conn, 101);

      JDBCUtil.commit(conn);
    }

    catch (SQLException e) {
      System.out.println(e.getMessage());
      JDBCUtil.rollback(conn);
    } finally {
      JDBCUtil.closeConnection(conn);
    }
  }

  public static void printPersonDetails(Connection conn, int personId)
      throws SQLException {
    String SQL = "{ call get_person_details(?) }";
    CallableStatement cstmt = null;
    try {
      cstmt = conn.prepareCall(SQL);

      // Set the IN parameters
      cstmt.setInt(1, personId);
      ResultSet rs = cstmt.executeQuery();

      // Process the result set
      //QueryPersonTest.printResultSet(rs);
    } finally {
      JDBCUtil.closeStatement(cstmt);
    }
  }
}

class JDBCUtil {
  public static Connection getConnection() throws SQLException {
    // Register the Java DB embedded JDBC driver
    Driver derbyEmbeddedDriver = null;// new
                                      // org.apache.derby.jdbc.EmbeddedDriver();
    DriverManager.registerDriver(derbyEmbeddedDriver);

    String dbURL = "jdbc:derby:beginningJavaDB;create=true;";
    String userId = "root";
    String password = "password";

    Connection conn = DriverManager.getConnection(dbURL, userId, password);
    conn.setAutoCommit(false);
    return conn;
  }

  public static void closeConnection(Connection conn) {
    try {
      if (conn != null) {
        conn.close();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void closeStatement(Statement stmt) {
    try {
      if (stmt != null) {
        stmt.close();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void closeResultSet(ResultSet rs) {
    try {
      if (rs != null) {
        rs.close();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void commit(Connection conn) {
    try {
      if (conn != null) {
        conn.commit();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void rollback(Connection conn) {
    try {
      if (conn != null) {
        conn.rollback();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    Connection conn = null;
    try {
      conn = getConnection();
      System.out.println("Connetced to the database.");
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      closeConnection(conn);
    }
  }
}

Related Tutorials