Example usage for java.sql ResultSet absolute

List of usage examples for java.sql ResultSet absolute

Introduction

In this page you can find the example usage for java.sql ResultSet absolute.

Prototype

boolean absolute(int row) throws SQLException;

Source Link

Document

Moves the cursor to the given row number in this ResultSet object.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,null)");
    st.executeUpdate("insert into survey (id,name ) values (3,'Tom')");
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    // Move cursor to the last row
    rs.absolute(-1);
    // Get data at cursor
    String id = rs.getString("id");
    System.out.println(id);/*w ww  .  j  av  a2  s. co  m*/

    // Move cursor to the second-to-last row
    //rs.absolute(-2);

    rs.close();
    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", "");

    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = statement.executeQuery("SELECT * FROM products");

    // Move to the second row
    resultSet.absolute(2);
    System.out.println("You are now in: " + resultSet.getRow());

    // Move 2 records forward from the current position (fourth row)
    resultSet.relative(2);/*  w  ww .ja v a2 s  . co m*/
    System.out.println("You are now in: " + resultSet.getRow());

    // Move to the last row in the result set
    resultSet.absolute(-1);
    System.out.println("You are now in: " + resultSet.getRow());

    // Move 3 records backward from the current position (second row)
    resultSet.relative(-3);
    System.out.println("You are now in: " + resultSet.getRow());
    connection.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//from  w  w  w  .  j  ava 2 s .c  o m

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    // Create a scrollable result set
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    // Move cursor forward
    while (resultSet.next()) {
        // Get data at cursor
        String s = resultSet.getString(1);
    }

    // Move cursor backward
    while (resultSet.previous()) {
        // Get data at cursor
        String s = resultSet.getString(1);
    }

    // Move cursor to the second row
    resultSet.absolute(2);

    // Move cursor to the last row
    resultSet.absolute(-1);

    // Move cursor to the second last row
    resultSet.absolute(-2);
}

From source file:ScrollableRs.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES");
    while (rs.next()) {
        printRow(rs);//from  ww  w . j  a  v  a 2s  .  c  o  m
    }
    rs.afterLast();
    System.out.println("\"After-last-row\" = " + rs.isAfterLast());
    rs.beforeFirst();
    System.out.println("\"Before-first-row\" = " + rs.isBeforeFirst());
    rs.first();
    printRow(rs);
    rs.last();
    printRow(rs);
    rs.previous();
    printRow(rs);

    rs.next();
    printRow(rs);

    rs.absolute(3);
    printRow(rs);

    rs.relative(-2);
    printRow(rs);
    if (conn != null)
        conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {//from   ww  w . j  a v  a2s.c o m
        String url = "jdbc:odbc:yourdatabasename";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);

        Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);

        String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP";

        ResultSet rs = stmt.executeQuery(sqlQuery);

        int rowSize = 0;
        while (rs.next()) {
            rowSize++;
        }

        System.out.println("Number of Rows in ResultSet is: " + rowSize);
        if (rowSize == 0) {
            System.out.println("Since there are no rows, exiting...");
            System.exit(0);
        }

        int cursorPosition = Math.round(rowSize / 2);

        System.out.println("Moving to position: " + cursorPosition);
        rs.absolute(cursorPosition);
        System.out.println("Name: " + rs.getString(2));

        rs.relative(-1);

        cursorPosition = rs.getRow();
        System.out.println("Moving to position: " + cursorPosition);
        System.out.println("Name: " + rs.getString(2));

        System.out.println("Moving to the first row");
        while (!rs.isFirst()) {
            rs.previous();
        }
        System.out.println("Name: " + rs.getString(2));
        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:DemoUpdatableResultSet.java

public static void main(String[] args) {
    ResultSet rs = null;
    Connection conn = null;/*w  ww  .j a va2s  .  c o m*/
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String query = "select id, name, age from employees where age > ?";
        pstmt = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        pstmt.setInt(1, 20); // set input values
        rs = pstmt.executeQuery(); // create an updatable ResultSet
                                   // update a column value in the current row.
        rs.absolute(2); // moves the cursor to the 2nd row of rs
        rs.updateString("name", "newName"); // updates the 'name' column of row 2 to newName
        rs.updateRow(); // updates the row in the data source
                        // insert column values into the insert row.
        rs.moveToInsertRow(); // moves cursor to the insert row
        rs.updateInt(1, 1234); // 1st column id=1234
        rs.updateString(2, "newName"); // updates the 2nd column
        rs.updateInt(3, 99); // updates the 3rd column to 99
        rs.insertRow();
        rs.moveToCurrentRow();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            rs.close();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

    Connection con;/*from   www  . ja  v  a2 s  .  c  o  m*/
    Statement stmt;
    ResultSet uprs;

    try {
        Class.forName(driver);
        con = DriverManager.getConnection("jdbc:odbc:RainForestDSN", "student", "student");
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        uprs = stmt.executeQuery("SELECT * FROM Records");

        // Check the column count
        ResultSetMetaData md = uprs.getMetaData();
        System.out.println("Resultset has " + md.getColumnCount() + " cols.");

        int rowNum = uprs.getRow();
        System.out.println("row1 " + rowNum);
        uprs.absolute(1);
        rowNum = uprs.getRow();
        System.out.println("row2 " + rowNum);
        uprs.next();
        uprs.moveToInsertRow();
        uprs.updateInt(1, 150);
        uprs.updateString(2, "Madonna");
        uprs.updateString(3, "Dummy");
        uprs.updateString(4, "Jazz");
        uprs.updateString(5, "Image");
        uprs.updateInt(6, 5);
        uprs.updateDouble(7, 5);
        uprs.updateInt(8, 15);
        uprs.insertRow();
        uprs.close();
        stmt.close();
        con.close();
    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}

From source file:InsertRowBug.java

public static void main(String args[]) {

    String url;/*  w  w  w.  ja  v a 2  s  .com*/
    // url = "jdbc:odbc:SQL Anywhere 5.0 Sample";
    // url = "jdbc:oracle:thin:@server:1521:db570";
    url = "jdbc:odbc:RainForestDSN";

    String driver;
    //driver = "oracle.jdbc.driver.OracleDriver";
    driver = "sun.jdbc.odbc.JdbcOdbcDriver";

    String user, pass;
    user = "student";
    pass = "student";

    Connection con;
    Statement stmt;
    ResultSet uprs;

    try {
        Class.forName(driver);

    } catch (java.lang.ClassNotFoundException e) {
        System.err.println(e);
        return;
    }

    try {
        con = DriverManager.getConnection(url, user, pass);
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        uprs = stmt.executeQuery("SELECT * FROM Music_Recordings");

        // Check the column count
        ResultSetMetaData md = uprs.getMetaData();
        System.out.println("Resultset has " + md.getColumnCount() + " cols.");

        int rowNum = uprs.getRow();
        System.out.println("row1 " + rowNum);
        uprs.absolute(1);
        rowNum = uprs.getRow();
        System.out.println("row2 " + rowNum);
        uprs.next();
        uprs.moveToInsertRow();
        uprs.updateInt(1, 150);
        uprs.updateString(2, "Madonna");
        uprs.updateString(3, "Dummy");
        uprs.updateString(4, "Jazz");
        uprs.updateString(5, "Image");
        uprs.updateInt(6, 5);
        uprs.updateDouble(7, 5);
        uprs.updateInt(8, 15);
        uprs.insertRow();
        uprs.close();
        stmt.close();
        con.close();
    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,null)");
    st.executeUpdate("insert into survey (id,name ) values (3,'Tom')");
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    // Move cursor forward
    while (rs.next()) {
        // Get data at cursor
        String id = rs.getString(1);
        String name = rs.getString(2);
    }//  w  w w  . j  a  v  a 2  s  .  co m

    // Move cursor backward
    while (rs.previous()) {
        // Get data at cursor
        String id = rs.getString(1);
        String name = rs.getString(2);
    }

    // Move cursor to the first row
    rs.first();

    // Move cursor to the last row
    rs.last();

    // Move cursor to the end, after the last row
    rs.afterLast();

    // Move cursor to the beginning, before the first row.
    // cursor position is 0.
    rs.beforeFirst();

    // Move cursor to the second row
    rs.absolute(2);

    // Move cursor to the last row
    rs.absolute(-1);

    // Move cursor to the second-to-last row
    rs.absolute(-2);

    // Move cursor down 5 rows from the current row. If this moves
    // cursor beyond the last row, cursor is put after the last row
    rs.relative(5);

    // Move cursor up 3 rows from the current row. If this moves
    // cursor beyond the first row, cursor is put before the first row
    rs.relative(-3);

    rs.close();
    st.close();
    conn.close();

}

From source file:com.example.querybuilder.server.Jdbc.java

public static void absolute(ResultSet resultSet, int row) {
    try {/*  w  w  w .  j av  a  2 s  .c o  m*/
        resultSet.absolute(row);
    } catch (SQLException e) {
        throw new SqlRuntimeException(e);
    }
}