Example usage for java.sql ResultSet first

List of usage examples for java.sql ResultSet first

Introduction

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

Prototype

boolean first() throws SQLException;

Source Link

Document

Moves the cursor to the first row in this ResultSet object.

Usage

From source file:Main.java

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

    resultSet.first();

    // Update the value of column col_string on that row
    resultSet.updateString("col_string", "new data");

    // Update the row; if auto-commit is enabled, update is committed
    resultSet.updateRow();

}

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

    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 id,name FROM survey");

    // Delete the first row
    rs.first();
    rs.deleteRow();/*from  w w  w  .j  a va  2  s  .  c o  m*/

    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 w w .j  av a  2s .  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 an updatable result set
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    // Delete the first row
    resultSet.first();
    resultSet.deleteRow();
}

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

    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 row to update
    rs.first();

    // Update the value of column column_1 on that row
    rs.updateString("name", "new data");

    // Discard the update to the row
    rs.cancelRowUpdates();/*  w w w .j a  va  2  s . com*/

    rs.close();
    st.close();
    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_UPDATABLE);

    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 row to update
    rs.first();

    // Update the value of column column_1 on that row
    rs.updateString(2, "new data");

    // Update the row; if autocommit is enabled,
    // update is committed
    rs.updateRow();//from  w  ww.ja  v  a  2s. com

    rs.close();
    st.close();
    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_UPDATABLE);

    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 row to update
    rs.first();

    // Update the value of column column_1 on that row
    rs.updateString("name", "new data");

    // Update the row; if autocommit is enabled,
    // update is committed
    rs.updateRow();/*from  w  ww  . j  a  va2  s. co m*/

    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);//ww  w . j a  v  a2s.  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_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

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

    // Update the value of column col_string on that row
    resultSet.updateString("col_string", "new data");

    // Discard the update to the row
    resultSet.cancelRowUpdates();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    String sqlQuery = "SELECT uid, name, duration from EVENTS";

    ResultSet rs = stmt.executeQuery(sqlQuery);

    while (rs.next()) {
        rs.updateString("Name", "new Name");

        rs.updateRow();/*from  ww w  . j  a v a  2s .c o m*/
    }

    rs.first();
    while (rs.next()) {
        String name = rs.getString(2);
        Timestamp hireDate = rs.getTimestamp(5);
        System.out.println("Name: " + name + " Hire Date: " + hireDate);
    }

    rs.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 forward
    while (rs.next()) {
        String id = rs.getString("id");
        System.out.println(id);/*from   w ww  .  j a  v  a  2s  .  c  o m*/
    }
    // Move cursor to the first row
    rs.first();
    // Get data at cursor
    String id = rs.getString("id");
    System.out.println(id);

    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_SENSITIVE,
            ResultSet.CONCUR_UPDATABLE);

    String query = "SELECT id, code, name, quantity, price FROM products";
    ResultSet uprs = statement.executeQuery(query);

    while (uprs.next()) {
        System.out.println(uprs.getString("id") + ":" + uprs.getString("code") + ":" + uprs.getString("name")
                + ":" + uprs.getInt("quantity") + ":" + uprs.getDouble("price"));
    }/*  w  w w.  j  av  a 2  s  .c  o  m*/
    uprs.first();
    uprs.updateString("name", "Java");
    uprs.updateRow();
    uprs.next();
    uprs.deleteRow();

    uprs.moveToInsertRow();
    uprs.updateString("code", "1");
    uprs.updateString("name", "Data Structures");
    uprs.updateInt("quantity", 1);
    uprs.updateDouble("price", 5.99);
    uprs.insertRow();

    uprs.beforeFirst();
    while (uprs.next()) {
        System.out.println(uprs.getString("id") + "\t" + uprs.getString("code") + "\t" + uprs.getString("name")
                + "\t" + uprs.getInt("quantity") + "\t" + uprs.getDouble("price"));
    }
    connection.close();
}