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: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);/*ww  w. j  av a  2s. 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: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  a2  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");

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

public static void main(String args[]) {

    String url;//  ww w  .j ava  2s  .  co  m
    url = "jdbc:odbc:UserDB";

    String user, pass;
    user = "ian";
    pass = "stjklsq";

    Connection con;
    Statement stmt;
    ResultSet rs;

    try {
        con = DriverManager.getConnection(url, user, pass);
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        rs = stmt.executeQuery("SELECT * FROM Users where nick=\"ian\"");

        // Get the resultset ready, update the passwd field, commit
        rs.first();
        rs.updateString("password", "unguessable");
        rs.updateRow();

        rs.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);//from  w ww  . j ava2s .  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 first row
    resultSet.first();

}

From source file:Main.java

public static void main(String[] args) {
    Connection conn = null;/*  w  w  w. jav a  2s  . co m*/
    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: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()) {
        // Get data at cursor
        String id = rs.getString(1);
        String name = rs.getString(2);
    }//  w ww .ja  v  a 2 s  .  co  m

    // Move cursor backward
    while (rs.previous()) {
        // Get data at cursor
        String id = rs.getString(1);
        String name = rs.getString(2);
    }

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

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

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

    // Move cursor to the beginning, before the first row.
    // cursor position is 0.
    rs.beforeFirst();

    // Move cursor to the second row
    rs.absolute(2);

    // Move cursor to the last row
    rs.absolute(-1);

    // Move cursor to the second-to-last row
    rs.absolute(-2);

    // Move cursor down 5 rows from the current row. If this moves
    // cursor beyond the last row, cursor is put after the last row
    rs.relative(5);

    // Move cursor up 3 rows from the current row. If this moves
    // cursor beyond the first row, cursor is put before the first row
    rs.relative(-3);

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

}

From source file:oscar.util.JDBCUtil.java

public static void toDataBase(InputStream inputStream, String fileName) {
    boolean validation = true;
    DOMParser parser = new DOMParser();
    Document doc;/*w  ww.  ja  va 2s .c o  m*/

    try {
        //InputStream inputStream = file.getInputStream();
        InputSource source = new InputSource(inputStream);
        //String fileName = file.getFileName();
        int indexForm = fileName.indexOf("_");
        int indexDemo = fileName.indexOf("_", indexForm + 1);
        int indexTimeStamp = fileName.indexOf(".", indexDemo);
        String formName = fileName.substring(0, indexForm);
        String demographicNo = fileName.substring(indexForm + 1, indexDemo);
        String timeStamp = fileName.substring(indexDemo + 1, indexTimeStamp);

        //check if the data existed in the database already...
        String sql = "SELECT * FROM " + formName + " WHERE demographic_no='" + demographicNo
                + "' AND formEdited='" + timeStamp + "'";
        MiscUtils.getLogger().debug(sql);
        ResultSet rs = DBHandler.GetSQL(sql);
        if (!rs.first()) {
            rs.close();
            sql = "SELECT * FROM " + formName + " WHERE demographic_no='" + demographicNo + "' AND ID='0'";
            MiscUtils.getLogger().debug("sql: " + sql);
            rs = DBHandler.GetSQL(sql, true);
            rs.moveToInsertRow();
            //To validate or not
            parser.setFeature("http://xml.org/sax/features/validation", validation);
            parser.parse(source);
            doc = parser.getDocument();
            rs = toResultSet(doc, rs);
            rs.insertRow();
        }
        rs.close();
    } catch (Exception e) {
        MiscUtils.getLogger().debug("Errors " + e);

    }

}

From source file:massbank.svn.SVNUtils.java

/**
 *
 *///from w  w  w. ja  va 2s .  c  o m
public static boolean checkDBExists(String dbName) {
    if (dbName.equals("")) {
        return false;
    }
    Connection con = connectDB(dbName);
    if (con == null) {
        return false;
    }
    try {
        Statement stmt = con.createStatement();
        String sql = "SHOW TABLES LIKE 'SPECTRUM'";
        ResultSet rs = stmt.executeQuery(sql);
        String val = "";
        if (rs.first()) {
            val = rs.getString(1);
        }
        con.close();
        if (!val.equals("")) {
            return true;
        } else {
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:zebrinho.domain.User.java

public static User fromUsername(String username) {
    try {/* w  w w. j  a va 2 s. co  m*/
        PreparedStatement ps = DB.getConnection()
                .prepareStatement("select id, username, password from USER where username = ?");
        ps.setString(1, username);
        ResultSet rs = ps.executeQuery();
        if (!rs.first()) {
            return null;
        }
        User user = new User();
        user.id = rs.getInt(1);
        user.username = rs.getString(2);
        user.password = rs.getString(3);
        return user;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.nuxeo.storage.file.utils.CheckDocumentFileIsUnique.java

public static boolean isUniqueWithSQL(DocumentModel inDoc) throws SQLException, ClientException {

    // What do we do with null?
    if (inDoc == null) {
        throw new IllegalArgumentException("inDoc should not be null");
    }/* w  w  w .j a va2 s  .c  om*/

    // Nothing if it's a proxy. It's ok, and not a duplicate
    if (inDoc.isImmutable() || inDoc.isProxy()) {
        return true;
    }

    // If we have no file, let's consider it's ok
    if (!inDoc.hasSchema("file")) {
        return true;
    }
    Blob theFile = (Blob) inDoc.getPropertyValue("file:content");
    if (theFile == null) {
        return true;
    }

    //_myLog.warn("CHECKCKING UNIQUENESS...");

    //OK, now we query
    Connection co = ConnectionHelper.getConnection(null);
    try {
        String statementStr = String.format(kSTATEMENT_SQL, theFile.getDigest(), inDoc.getId());
        //_myLog.warn("... with statement:\n" + statementStr);

        //Statement st = co.createStatement();
        Statement st = co.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet rs = st.executeQuery(statementStr);

        int count = -1;
        if (rs.first()) {
            count = rs.getInt(1);
        } else {
            throw new SQLException("Could not retrieve the COUNT(*) column");
        }

        //_myLog.warn("CHECKCKING UNIQUENESS, count = " + count + " (0 => unique)");

        return count == 0;

    } catch (SQLException e) {
        _myLog.warn("CHECKCKING UNIQUENESS, SQLException " + e);
        throw e;
    } finally {
        if (co != null) {
            co.close();
        }
    }
}