Example usage for java.sql ResultSet next

List of usage examples for java.sql ResultSet next

Introduction

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

Prototype

boolean next() throws SQLException;

Source Link

Document

Moves the cursor forward one row from its current position.

Usage

From source file:au.org.ala.layers.stats.ObjectsStatsGenerator.java

public static void main(String[] args) {
    long start = System.currentTimeMillis();

    String fid = null;// w w  w  .j  a va2  s . c  o m

    logger.info(
            "args[0] = threadcount, args[1] = db connection string, args[2] = db username, args[3] = password");
    if (args.length >= 4) {
        CONCURRENT_THREADS = Integer.parseInt(args[0]);
        db_url = args[1];
        db_usr = args[2];
        db_pwd = args[3];
    }
    if (args.length > 4) {
        fid = args[4];
    }

    Connection c = getConnection();
    //String count_sql = "select count(*) as cnt from objects where bbox is null or area_km is null";
    String count_sql = "select count(*) as cnt from objects where area_km is null and st_geometrytype(the_geom) <> 'ST_Point' ";
    if (StringUtils.isEmpty(fid)) {
        count_sql = count_sql + " and fid = '" + fid + "'";
    }

    int count = 0;
    try {
        Statement s = c.createStatement();
        ResultSet rs = s.executeQuery(count_sql);
        while (rs.next()) {
            count = rs.getInt("cnt");
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    int iter = count / 200000;

    logger.info("Breaking into " + iter + " iterations");
    for (int i = 0; i <= iter; i++) {
        long iterStart = System.currentTimeMillis();
        //  updateBbox();
        updateArea(fid);
        logger.info("iteration " + i + " completed after " + (System.currentTimeMillis() - iterStart) + "ms");
        logger.info("total time taken is " + (System.currentTimeMillis() - start) + "ms");
    }
}

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);/*  ww w  .  j av a2 s .  com*/
    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:DemoDisplayBlobFromDatabase.java

public static void main(String args[]) throws Exception {
    Connection conn = null;/*  w w w. j a v  a2s  .  c  o m*/
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    String query = "SELECT blob_column FROM blob_table WHERE id = ?";

    try {
        conn = getConnection();
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, "0001");
        rs = pstmt.executeQuery();
        rs.next();
        // materialize binary data onto client
        java.sql.Blob blob = rs.getBlob(1);
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}

From source file:Main.java

public static void main(String[] args) {
    Connection conn = null;//from   w w  w .j ava2  s.com
    Statement stmt = null;
    ResultSet rs = null;
    try {
        conn = getConnection();
        String query = "select id, name from employees";
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        rs = stmt.executeQuery(query);
        while (rs.next()) {
            String id = rs.getString(1);
            String name = rs.getString(2);
            System.out.println("id=" + id + "  name=" + name);
        }
        rs.first();
        rs.deleteRow();
        rs.beforeFirst();
        while (rs.next()) {
            String id = rs.getString(1);
            String name = rs.getString(2);
            System.out.println("id=" + id + "  name=" + name);
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    } finally {
        try {
            rs.close();
            stmt.close();
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:TestRegisterDriverApp.java

public static void main(String args[]) {

    try {/*from  w ww. ja v a2s.  c o  m*/
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    } catch (SQLException e) {
        System.out.println("Oops! Got a SQL error: " + e.getMessage());
        System.exit(1);
    }

    Connection conn = null;
    Statement stmt = null;
    ResultSet rset = null;
    try {
        conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");

        stmt = conn.createStatement();
        rset = stmt.executeQuery("select 'Hello '||USER||'!' result from dual");
        while (rset.next())
            System.out.println(rset.getString(1));
        rset.close();
        rset = null;
        stmt.close();
        stmt = null;
        conn.close();
        conn = null;
    } catch (SQLException e) {
        System.out.println("Darn! A SQL error: " + e.getMessage());
    } finally {
        if (rset != null)
            try {
                rset.close();
            } catch (SQLException ignore) {
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException ignore) {
            }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ignore) {
            }
    }
}

From source file:TestClassForNameApp.java

public static void main(String args[]) {

    try {// w  w  w . j a va  2  s.  c  o m
        Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
        System.out.println("Oops! Can't find class oracle.jdbc.driver.OracleDriver");
        System.exit(1);
    }

    Connection conn = null;
    Statement stmt = null;
    ResultSet rset = null;
    try {
        conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");

        stmt = conn.createStatement();
        rset = stmt.executeQuery("select 'Hello '||USER||'!' result from dual");
        while (rset.next())
            System.out.println(rset.getString(1));
        rset.close();
        rset = null;
        stmt.close();
        stmt = null;
        conn.close();
        conn = null;
    } catch (SQLException e) {
        System.out.println("Darn! A SQL error: " + e.getMessage());
    } finally {
        if (rset != null)
            try {
                rset.close();
            } catch (SQLException ignore) {
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException ignore) {
            }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ignore) {
            }
    }
}

From source file:PooledConnectionExample.java

public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;//from   ww w . jav a 2s  .c  o  m
    ResultSet resultSet = null;

    try {
        connection = getConnection();
        statement = connection.createStatement();
        String selectEmployeesSQL = "SELECT * FROM employees";
        resultSet = statement.executeQuery(selectEmployeesSQL);

        while (resultSet.next()) {
            printEmployee(resultSet);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        closeAll(resultSet, statement, connection);
    }
}

From source file:Blobs.java

public static void main(String args[]) {
    if (args.length != 1) {
        System.err.println("Syntax: <java Blobs [driver] [url] " + "[uid] [pass] [file]");
        return;//from   w w  w.  ja  v a 2s  .  c om
    }
    try {
        Class.forName(args[0]).newInstance();
        Connection con = DriverManager.getConnection(args[1], args[2], args[3]);
        File f = new File(args[4]);
        PreparedStatement stmt;

        if (!f.exists()) {
            // if the file does not exist
            // retrieve it from the database and write it to the named file
            ResultSet rs;

            stmt = con.prepareStatement("SELECT blobData " + "FROM BlobTest " + "WHERE fileName = ?");

            stmt.setString(1, args[0]);
            rs = stmt.executeQuery();
            if (!rs.next()) {
                System.out.println("No such file stored.");
            } else {
                Blob b = rs.getBlob(1);
                BufferedOutputStream os;

                os = new BufferedOutputStream(new FileOutputStream(f));
                os.write(b.getBytes(0, (int) b.length()), 0, (int) b.length());
                os.flush();
                os.close();
            }
        } else {
            // otherwise read it and save it to the database
            FileInputStream fis = new FileInputStream(f);
            byte[] tmp = new byte[1024];
            byte[] data = null;
            int sz, len = 0;

            while ((sz = fis.read(tmp)) != -1) {
                if (data == null) {
                    len = sz;
                    data = tmp;
                } else {
                    byte[] narr;
                    int nlen;

                    nlen = len + sz;
                    narr = new byte[nlen];
                    System.arraycopy(data, 0, narr, 0, len);
                    System.arraycopy(tmp, 0, narr, len, sz);
                    data = narr;
                    len = nlen;
                }
            }
            if (len != data.length) {
                byte[] narr = new byte[len];

                System.arraycopy(data, 0, narr, 0, len);
                data = narr;
            }
            stmt = con.prepareStatement("INSERT INTO BlobTest(fileName, " + "blobData) VALUES(?, ?)");
            stmt.setString(1, args[0]);
            stmt.setObject(2, data);
            stmt.executeUpdate();
            f.delete();
        }
        con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:InsertRowUpdatableResultSet_MySQL.java

public static void main(String[] args) {
    Connection conn = null;//  www .ja  v  a2 s .  com
    Statement stmt = null;
    ResultSet rs = null;
    try {
        conn = getConnection();
        String query = "select id, name from employees";
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        rs = stmt.executeQuery(query);
        while (rs.next()) {
            String id = rs.getString(1);
            String name = rs.getString(2);
            System.out.println("id=" + id + "  name=" + name);
        }
        // Move cursor to the "insert row"
        rs.moveToInsertRow();
        // Set values for the new row.
        rs.updateString("id", "001");
        rs.updateString("name", "newName");
        // Insert the new row
        rs.insertRow();
        // scroll from the top again
        rs.beforeFirst();
        while (rs.next()) {
            String id = rs.getString(1);
            String name = rs.getString(2);
            System.out.println("id=" + id + "  name=" + name);
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    } finally {
        // release database resources
        try {
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:Batch.java

static public void main(String[] args) {
    Connection conn = null;/*  w ww  .j a  va  2  s .c om*/

    try {
        ArrayList breakable = new ArrayList();
        PreparedStatement stmt;
        Iterator users;
        ResultSet rs;

        Class.forName(args[0]).newInstance();
        conn = DriverManager.getConnection(args[1], args[2], args[3]);
        stmt = conn.prepareStatement("SELECT user_id, password " + "FROM user");
        rs = stmt.executeQuery();
        while (rs.next()) {
            String uid = rs.getString(1);
            String pw = rs.getString(2);

            // Assume PasswordCracker is some class that provides
            // a single static method called crack() that attempts
            // to run password cracking routines on the password
            //                if( PasswordCracker.crack(uid, pw) ) {
            //                  breakable.add(uid);
            //            }
        }
        stmt.close();
        if (breakable.size() < 1) {
            return;
        }
        stmt = conn.prepareStatement("UPDATE user " + "SET bad_password = 'Y' " + "WHERE uid = ?");
        users = breakable.iterator();
        while (users.hasNext()) {
            String uid = (String) users.next();

            stmt.setString(1, uid);
            stmt.addBatch();
        }
        stmt.executeBatch();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
}