import java.sql.*;

public class CursorPosition {
    static String server     = "server";
    static String db         = "TST";
    static String user       = "DBA";
    static String password   = "DBA";

  public static void main (String[] args){

    String url = "jdbc:sapdb://" + server + "/"+ db;

    try {
      Class.forName("com.sap.dbtech.jdbc.DriverSapDB");
      Connection connect = DriverManager.getConnection(url, user, password);
      connect.setAutoCommit(true);

      // Driver information
      DatabaseMetaData meta = connect.getMetaData();
      System.out.println("Driver version: " + meta.getDriverVersion());

      // Creating table for test
      System.out.println("Creating table 'CURSORPOSITION'");
      connect.createStatement().executeUpdate(
        "CREATE TABLE CURSORPOSITION(ID INTEGER NOT NULL)");

      // Inserting data
      System.out.println("Inserting data to table 'CURSORPOSITION' (10 rows)");
      PreparedStatement pstmt = connect.prepareStatement(
        "INSERT INTO CURSORPOSITION(ID) VALUES (?)");
      for ( int i=1; i<=10; i++ ) {
        pstmt.setInt(1, i);
        pstmt.executeUpdate();
      }

      // Selecting data
      System.out.println("Selection data from table 'CURSORPOSITION'");
      Statement stmt = connect.createStatement();
      ResultSet rs = stmt.executeQuery(
        "SELECT ID FROM CURSORPOSITION ORDER BY ID");

      System.out.println("Stepping forward throw ResultSet");
      while ( rs.relative(1) )
        System.out.println("CURSOR POS: " + rs.getRow() +
          ", CONTENT: " + rs.getObject(1));

      System.out.println("Stepping backward throw ResultSet");
      while ( rs.relative(-1) )
        System.out.println("CURSOR POS: " + rs.getRow() +
          ", CONTENT: " + rs.getObject(1));

      System.out.println("Jumping to last row");
      if ( rs.last() )
        System.out.println("CURSOR POS: " + rs.getRow() +
          ", CONTENT: " + rs.getObject(1));

      // Dropping table
      System.out.println("Dropping table 'CURSORPOSITION'");
      connect.createStatement().executeUpdate(
        "DROP TABLE CURSORPOSITION");

    } catch (Throwable  t){
      t.printStackTrace();
    }

  }

}
