Example usage for java.sql ResultSet TYPE_SCROLL_INSENSITIVE

List of usage examples for java.sql ResultSet TYPE_SCROLL_INSENSITIVE

Introduction

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

Prototype

int TYPE_SCROLL_INSENSITIVE

To view the source code for java.sql ResultSet TYPE_SCROLL_INSENSITIVE.

Click Source Link

Document

The constant indicating the type for a ResultSet object that is scrollable but generally not sensitive to changes to the data that underlies the ResultSet.

Usage

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  . ja  va2s .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 down 5 rows from the current row. If this moves cursor beyond the last row, cursor is put after the last row
    resultSet.relative(5);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*w  w  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);
    // 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");

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

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

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

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

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

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//from   ww  w . j  a  va2 s  .c om

    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:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar);");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')");

    JdbcRowSet jdbcRS = new JdbcRowSetImpl(conn);
    jdbcRS.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
    String sql = "SELECT * FROM survey";
    jdbcRS.setCommand(sql);//  www .j  a v  a2 s .  com
    jdbcRS.execute();
    jdbcRS.addRowSetListener(new ExampleListener());

    while (jdbcRS.next()) {
        System.out.println("id=" + jdbcRS.getString(1));
        System.out.println("name=" + jdbcRS.getString(2));
    }
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    DatabaseMetaData meta = conn.getMetaData();

    if (meta.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY)) {
        System.out.println("type name=TYPE_FORWARD_ONLY");
    }/*w ww  . j  a v  a 2  s.  c  om*/
    if (meta.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)) {
        System.out.println("type name=TYPE_SCROLL_INSENSITIVE");
    }
    if (meta.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE)) {
        System.out.println("type name=TYPE_SCROLL_SENSITIVE");
    }

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar);");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')");

    JdbcRowSet jdbcRS;/*  w  w w. j  ava2  s .  c o m*/
    jdbcRS = new JdbcRowSetImpl(conn);
    jdbcRS.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
    String sql = "SELECT * FROM survey";
    jdbcRS.setCommand(sql);
    jdbcRS.execute();
    jdbcRS.addRowSetListener(new ExampleListener());

    while (jdbcRS.next()) {
        // each call to next, generates a cursorMoved event
        System.out.println("id=" + jdbcRS.getString(1));
        System.out.println("name=" + jdbcRS.getString(2));
    }
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);// w w  w  . j a v  a 2  s  .  c o  m
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    DatabaseMetaData metadata = connection.getMetaData();

    boolean supportForwardOnly = metadata.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY);
    System.out.println("supportForwardOnly = " + supportForwardOnly);

    boolean supportScrollInsensitive = metadata.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
    System.out.println("supportScrollInsensitive = " + supportScrollInsensitive);

    boolean supportScrollSensitive = metadata.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
    System.out.println("supportScrollSensitive = " + supportScrollSensitive);
    connection.close();
}

From source file:GetNumberOfRowsScrollableResultSet_MySQL.java

public static void main(String[] args) {
    Connection conn = null;/*from  w ww .  j a  v  a2  s . 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();
        }
    }
}

From source file:ReverseSelect.java

public static void main(String argv[]) {
    Connection con = null;//from  ww  w  .  j av  a 2  s .c  o  m

    try {
        String url = "jdbc:msql://carthage.imaginary.com/ora";
        String driver = "com.imaginary.sql.msql.MsqlDriver";
        Properties p = new Properties();
        Statement stmt;
        ResultSet rs;

        p.put("user", "borg");
        Class.forName(driver).newInstance();
        con = DriverManager.getConnection(url, "borg", "");
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs = stmt.executeQuery("SELECT * from test ORDER BY test_id");
        // as a new ResultSet, rs is currently positioned
        // before the first row
        System.out.println("Got results:");
        // position rs after the last row
        rs.afterLast();
        while (rs.previous()) {
            int a = rs.getInt("test_id");
            String str = rs.getString("test_val");

            System.out.print("\ttest_id= " + a);
            System.out.println("/str= '" + str + "'");
        }
        System.out.println("Done.");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.nuxeo.App.java

public static void main(String[] args) throws SQLException, IOException {

    Properties prop = readProperties();
    String user = prop.getProperty("user");
    String password = prop.getProperty("password");
    String connectionURL = prop.getProperty("url");
    String driver = prop.getProperty("driver");
    String query = prop.getProperty("query");

    log.info("Connect to:" + connectionURL + " from " + getHostName());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(baos);
    final ConsoleReporter reporter = new ConsoleReporter(printStream);

    Connection conn = null;/*from ww  w  . j a  va  2  s  .c o m*/
    PreparedStatement ps = null;
    ResultSet rs = null;
    TimerContext tc = null;
    int repeat = Integer.valueOf(System.getProperty(REPEAT_KEY, DEFAULT_REPEAT)).intValue();

    log.info("Submiting " + repeat + " queries: " + query);
    try {
        Class.forName(driver);
        tc = connTimer.time();
        conn = DriverManager.getConnection(connectionURL, user, password);
        tc.stop();
        ps = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        int paramCount = countOccurrences(query, '?');
        for (int i = 1; i <= paramCount; i++) {
            String key = "p" + i;
            String param = prop.getProperty(key);
            if (param == null) {
                break;
            }
            log.info(key + " = " + param);
            String type = "object";
            if (param.contains(":")) {
                type = param.split(":", 2)[0];
                param = param.split(":", 2)[1];
            }
            if (type.equalsIgnoreCase("object")) {
                ps.setObject(i, (Object) param);
            } else if (type.equalsIgnoreCase("string")) {
                ps.setString(i, param);
            } else if (type.equalsIgnoreCase("nstring")) {
                ps.setNString(i, param);
            } else {
                log.warn("Unknown type " + type + " use setObject");
                ps.setObject(i, (Object) param);
            }
        }

        int rows = 0;
        int bytes = 0;

        for (int i = 0; i < repeat; i++) {
            tc = execTimer.time();
            rs = ps.executeQuery();
            tc.stop();
            tc = fetchingTimer.time();
            ResultSetMetaData rsmd = rs.getMetaData();
            int cols = rsmd.getColumnCount();
            while (rs.next()) {
                rows++;
                for (int c = 1; c <= cols; c++) {
                    bytes += rs.getBytes(1).length;
                }
            }
            rs.close();
            tc.stop();
            // don't stress too much
            Thread.sleep((int) (Math.random() * 100));
        }
        log.info("Fetched rows: " + rows + ", total bytes: " + bytes + ", bytes/rows: "
                + ((float) bytes) / rows);

    } catch (SQLException e) {
        log.error(e.getMessage(), e);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } finally {
        if (rs != null) {
            rs.close();
        }
        if (ps != null) {
            ps.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
    reporter.run();
    try {
        String content = baos.toString("ISO-8859-1");
        log.info(content);
    } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage(), e);
    }

}