title: How to fetch mysql cursore value from stored procedure
body: <p>How to fetch mysql cursor values in java program.</p>
  
  <p>This is my mysql stored procedure</p>
  
  <pre><code>delimiter //
  CREATE PROCEDURE cursor_student()
  BEGIN
  DECLARE row_count INT DEFAULT 0;
  DECLARE exit_flag INT DEFAULT 0;
  DECLARE sid varchar(30);
  DECLARE sname varchar(50);
  DECLARE rst CURSOR FOR 
      SELECT sid, sname FROM student WHERE class = '11th';
  
  DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET exit_flag=1;
  
  OPEN rst;
  fetch_loop: LOOP
  
      FETCH rst INTO sid, sname;  
      IF exit_flag THEN 
          LEAVE fetch_loop;
      END IF;
      SET row_count = row_count +1;
  END LOOP;
  CLOSE rst;  
  SELECT 'number of rows fetched =', row_count;
  END;
  </code></pre>
  
  <p>this is my simlpe java program to read above stored procedure</p>
  
  <pre><code>import java.sql.CallableStatement;
  import java.sql.Date;
  import java.sql.DriverManager;
  import java.sql.Connection;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  public class StoredProcedure {
  
  public static void main(String[] args) {
      Connection dbConnection = null;
      String url = "jdbc:mysql://localhost:3306/";
      String db = "test";
      String driver = "com.mysql.jdbc.Driver";
      ResultSet rs = null;
      CallableStatement callableStatement = null;
      String getDBUSERCursorSql = "{call cursor_student}";
      try {
          Class.forName(driver);
          dbConnection = DriverManager.getConnection(url + db, "root", "");
          try {
  
              callableStatement = dbConnection.prepareCall(getDBUSERCursorSql);
  
  
              callableStatement.executeUpdate();
  
              rs = callableStatement.getResultSet();
  
              while (rs.next()) {
  
  
                  System.out.println("sid "+rs.getString(1)  +"  name  "+rs.getString(2));
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Output of above java program is</p>
  
  <pre><code>  sid number of rows fetched =  name  6
  </code></pre>
  
  <p>but i want to display values of sid and sname</p>
  
  <pre><code>+------+-------+
  | sid  | sname |
  +------+-------+
  | 1    | asdf  |
  | 2    | dff   |
  | 3    | gggg  |
  | 4    | tttt  |
  | 5    | mmmm  |
  | 6    | .uyy  |
  +------+-------+
  </code></pre>