Example usage for java.sql ResultSet getRow

List of usage examples for java.sql ResultSet getRow

Introduction

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

Prototype

int getRow() throws SQLException;

Source Link

Document

Retrieves the current row number.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection conn = null;//from w ww .j  a v a  2s  . c o  m
    Statement s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet r = s.executeQuery("SELECT * FROM employee");
    r.last();
    int count = r.getRow();
    r.beforeFirst();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("org.apache.derby.jdbc.ClientDriver");
    Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/testDb", "name", "pass");
    Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    String insertEmp1 = "insert into emp values(10,'A','trainee')";
    String insertEmp2 = "insert into emp values(11,'B','trainee')";
    String insertEmp3 = "insert into emp values(12,'C','trainee')";
    con.setAutoCommit(false);//from   ww w .  ja v a  2 s  .  co m
    stmt.addBatch(insertEmp1);
    stmt.addBatch(insertEmp2);
    stmt.addBatch(insertEmp3);
    ResultSet rs = stmt.executeQuery("select * from emp");
    rs.last();
    System.out.println("rows before batch execution= " + rs.getRow());
    stmt.executeBatch();
    con.commit();
    System.out.println("Batch executed");
    rs = stmt.executeQuery("select * from emp");
    rs.last();
    System.out.println("rows after batch execution= " + rs.getRow());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//from   w  ww  . j a v a 2s . 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);
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    // Get cursor position
    int pos = resultSet.getRow();
    boolean b = resultSet.isBeforeFirst();

    // Move cursor to the first row
    resultSet.next();

    // Get cursor position
    pos = resultSet.getRow();
    b = resultSet.isFirst();

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

    // Get cursor position
    pos = resultSet.getRow();
    b = resultSet.isLast();

    // Move cursor past last row
    resultSet.afterLast();

    // Get cursor position
    pos = resultSet.getRow();
    b = resultSet.isAfterLast();

}

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");

    // Get cursor position
    int pos = rs.getRow(); // 0
    System.out.println(pos);// w ww.  j a v a  2s  . co m
    boolean b = rs.isBeforeFirst(); // true
    System.out.println(b);

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

    // Get cursor position
    pos = rs.getRow(); // 1
    b = rs.isFirst(); // true
    System.out.println(pos);
    System.out.println(b);

    // Move cursor to the last row
    rs.last();
    // Get cursor position
    pos = rs.getRow();
    System.out.println(pos);
    b = rs.isLast(); // true

    // Move cursor past last row
    rs.afterLast();

    // Get cursor position
    pos = rs.getRow();
    b = rs.isAfterLast(); // true

    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");
    while (resultSet.next()) {
        String productCode = resultSet.getString("product_code");

        int row = resultSet.getRow();

        System.out.println(row + ". " + productCode);
    }//from   ww w  .  j av  a2  s .  co m
    connection.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);/*from  w  w  w  . j a  va 2 s  .com*/
    System.out.println("You are now in: " + resultSet.getRow());

    // Move 2 records forward from the current position (fourth row)
    resultSet.relative(2);
    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[] 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 to the end of the result set
    rs.last();//from   w  ww  .  j  a va 2s  . c om

    // get the row number of the last row, which is also the row count
    int rowCount = rs.getRow();
    System.out.println(rowCount);

    // now you may move the cursor to the front of this ResultSet object,
    // just before the first row
    rs.beforeFirst();

    rs.close();
    st.close();
    conn.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  ww.j a  v  a2  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 to the end of the result set
    resultSet.last();

    // Get the row number of the last row which is also the row count
    int rowCount = resultSet.getRow();

}

From source file:Main.java

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

    Connection con;/*ww w . ja  va 2s .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:GetNumberOfRowsScrollableResultSet_MySQL.java

public static void main(String[] args) {
    Connection conn = null;//w  w w  .  j av a2s  .c  o  m
    Statement stmt = null;
    ResultSet rs = null;
    try {
        conn = getConnection();
        String query = "select id from employees";
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        rs = stmt.executeQuery(query);
        // extract data from the ResultSet scroll from top
        while (rs.next()) {
            String id = rs.getString(1);
            System.out.println("id=" + id);
        }
        // move to the end of the result set
        rs.last();
        // get the row number of the last row which is also the row count
        int rowCount = rs.getRow();
        System.out.println("rowCount=" + rowCount);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}