Example usage for java.sql ResultSet beforeFirst

List of usage examples for java.sql ResultSet beforeFirst

Introduction

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

Prototype

void beforeFirst() throws SQLException;

Source Link

Document

Moves the cursor to the front of this ResultSet object, just before the first row.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection conn = null;//from  w w  w . ja va 2  s  .  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:UpdateableRs.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES");
    printRs(rs);/*from w  w w.  j  a v a 2  s .c  o m*/

    rs.beforeFirst();

    while (rs.next()) {
        double newSalary = rs.getDouble("salary") * 1.053;
        rs.updateDouble("salary", newSalary);
        rs.updateRow();
    }
    printRs(rs);
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES");
    printRs(rs);//  w  w w.ja va  2s.c o  m

    rs.beforeFirst();

    while (rs.next()) {
        double newSalary = rs.getDouble(3) * 1.053;
        rs.updateDouble("salary", newSalary);
        rs.updateRow();
    }
    printRs(rs);
    conn.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 cursor to the beginning, before the first row.
    // cursor position is 0.
    rs.beforeFirst();
    rs.next();/*from w ww .  j a va 2 s  . c o m*/
    // Get data at cursor
    String id = rs.getString("id");
    System.out.println(id);

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

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   w w  w  .j  av a  2  s . c  om*/
    }
    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:org.mzd.shap.sql.UpdateFeatureTable.java

/**
 * @param args/*from   w  ww .j  av a 2 s  .c om*/
 */
public static void main(String[] args) throws SQLException {

    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl("jdbc:mysql://localhost/BBay01a");
    ds.setUsername("test");
    ds.setPassword("xeno12");

    Connection conn = ds.getConnection();

    // Get the Sequence ID list.
    PreparedStatement select = null;
    PreparedStatement update = null;
    ResultSet result = null;

    select = conn.prepareStatement("SELECT DISTINCT SEQUENCE_ID FROM Features");
    result = select.executeQuery();
    List<Integer> sequenceIds = new ArrayList<Integer>();
    result.beforeFirst();
    while (result.next()) {
        sequenceIds.add(result.getInt(1));
    }
    result.close();
    select.close();

    // Get the list of Feature IDs for this Sequence ID
    select = conn.prepareStatement("SELECT FEATURE_ID FROM Features WHERE SEQUENCE_ID=? ORDER BY start");

    // Update the idx column for this feature id
    update = conn.prepareStatement("UPDATE Features SET featureOrder=? where FEATURE_ID=?");

    for (Integer seqId : sequenceIds) {
        select.setInt(1, seqId);
        result = select.executeQuery();

        int n = 0;
        result.beforeFirst();
        while (result.next()) {
            int featId = result.getInt(1);
            update.setInt(1, n++);
            update.setInt(2, featId);
            if (update.executeUpdate() != 1) {
                System.out.println("Failed update [" + update.toString() + "]");
                throw new SQLException();
            }
        }
        result.close();
    }

    update.close();
    select.close();

    conn.close();
    ds.close();
}

From source file:Main.java

public static void main(String[] args) throws SQLException {
    // DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@yourDB", "scott", "tiger");

    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    stmt.setFetchSize(1);/*from   w ww  .java  2 s. c o m*/
    ResultSet rset = stmt.executeQuery("select EMPNO, ENAME, SAL from EMP");
    showProperty(rset);

    while (rset.next()) {
        System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getInt(3));
    }
    doSomeChanges(conn);
    // Place the cursor before the first row
    rset.beforeFirst();

    while (rset.next()) {
        System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getInt(3));
    }
    rset.close();
    stmt.close();
    cleanup(conn);
    conn.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   www .  j  a v  a  2s.  c  o  m

    // 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:InsertRow.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;/* w  w w.  jav  a  2 s.co  m*/
    Statement stmt;
    String query = "select COF_NAME, PRICE from COFFEES";

    try {

        Class.forName("myDriver.ClassName");

    } catch (java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }

    try {

        con = DriverManager.getConnection(url, "myLogin", "myPassword");

        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES");

        uprs.moveToInsertRow();

        uprs.updateString("COF_NAME", "Kona");
        uprs.updateInt("SUP_ID", 150);
        uprs.updateFloat("PRICE", 10.99f);
        uprs.updateInt("SALES", 0);
        uprs.updateInt("TOTAL", 0);

        uprs.insertRow();
        uprs.beforeFirst();

        System.out.println("Table COFFEES after insertion:");
        while (uprs.next()) {
            String s = uprs.getString("COF_NAME");
            int sup = uprs.getInt("SUP_ID");
            float f = uprs.getFloat("PRICE");
            int sales = uprs.getInt("SALES");
            int t = uprs.getInt("TOTAL");
            System.out.print(s + "   " + sup + "   " + f + "   ");
            System.out.println(sales + "   " + t);
        }

        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[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*www . j a va  2 s.co  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 beginning, before the first row.
    // cursor position is 0.
    resultSet.beforeFirst();
}