Example usage for java.sql ResultSet close

List of usage examples for java.sql ResultSet close

Introduction

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

Prototype

void close() throws SQLException;

Source Link

Document

Releases this ResultSet object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

Usage

From source file:DemoDisplayBlobFromDatabase.java

public static void main(String args[]) throws Exception {
    Connection conn = null;/*from   www .ja v  a  2  s  . 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) throws Exception {
    try {//  ww w.j  ava  2  s.  c  om
        String url = "jdbc:odbc:yourdatabasename";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);

        String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP";
        ResultSet rs = stmt.executeQuery(sqlQuery);
        rs.last();

        // Move the cursor backwards through the ResultSet
        while (rs.previous()) {
            String nbr = rs.getString(1);
            String name = rs.getString(2);
            String job = rs.getString(3);
            String mgr = rs.getString(4);
            Timestamp hireDate = rs.getTimestamp(5);
            System.out.println(name);
        }

        rs.close();
        stmt.close();
        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:ResultSetUpdate.java

public static void main(String args[]) {

    String url;//from w  w w  .  j  a  v  a 2s  .  c om
    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 {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    ResultSet rs = DriverManager.getConnection("jdbc:odbc:employee_xls").createStatement()
            .executeQuery("Select * from [Sheet1$]");

    ResultSetMetaData rsmd = rs.getMetaData();
    int numberOfColumns = rsmd.getColumnCount();

    System.out.println("No of cols " + numberOfColumns);
    while (rs.next()) {
        for (int i = 1; i <= numberOfColumns; i++) {
            String columnValue = rs.getString(i);
            System.out.println(columnValue);
        }// w w  w.j a v  a 2 s.  c  om
    }
    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_INSENSITIVE, 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')");
    st = conn.createStatement();//  ww  w. j a v a 2s .com
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();

    // get the column names; column indexes start from 1
    for (int i = 1; i < numberOfColumns + 1; i++) {
        String columnName = rsMetaData.getColumnName(i);
        String tableName = rsMetaData.getTableName(i);
        System.out.println(columnName);
        System.out.println(tableName);
    }

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

}

From source file:Main.java

public static void main(String[] args) {
    Connection conn = null;// w  w w .j a va 2 s .  c  o  m
    Statement stmt = null;
    try {
        Class.forName(JDBC_DRIVER);
        conn = DriverManager.getConnection(DB_URL, USER, PASS);
        stmt = conn.createStatement();
        String sql = "SELECT id, first, last, age FROM Employees";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            int id = rs.getInt("id");
            int age = rs.getInt("age");
            String first = rs.getString("first");
            String last = rs.getString("last");

            System.out.print("ID: " + id);
            System.out.print(", Age: " + age);
            System.out.print(", First: " + first);
            System.out.println(", Last: " + last);
        }
        rs.close();
        stmt.close();
        conn.close();
    } catch (SQLException se) {
        se.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (stmt != null)
                stmt.close();
            if (conn != null)
                conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }
}

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 .j  ava  2s .com*/
    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 = null;/*  w  w  w  . j av a 2 s .  co m*/
    Statement stmt = null;

    Class.forName(JDBC_DRIVER);
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    System.out.println("Deleting database...");
    stmt = conn.createStatement();

    conn = DriverManager.getConnection(DB_URL, USER, PASS);

    stmt = conn.createStatement();
    String sql = "SELECT id, first, last, age FROM Person";
    ResultSet rs = stmt.executeQuery(sql);
    while (rs.next()) {
        // Retrieve by column name
        int id = rs.getInt("id");
        int age = rs.getInt("age");
        String first = rs.getString("firstName");
        String last = rs.getString("lastName");

        // Display values
        System.out.print("ID: " + id);
        System.out.print(", Age: " + age);
        System.out.print(", First: " + first);
        System.out.println(", Last: " + last);
    }
    rs.close();
    stmt.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_INSENSITIVE, 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 = conn.createStatement();//from   www . j  a v a  2  s.com
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    // extract data from the ResultSet
    while (rs.next()) {
        int id = rs.getInt(1);
        System.out.println("id=" + id);
        String name = rs.getString(2);
        System.out.println("name=" + name);
        if (rs.wasNull()) {
            System.out.println("name is null");
        } else {
            System.out.println("name is not null");
        }
        System.out.println("---------------");
    }
    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();

    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 = conn.createStatement();//from   w  w w .j a va  2  s.c  o  m
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    // extract data from the ResultSet
    while (rs.next()) {
        int id = rs.getInt(1);
        System.out.println("id=" + id);
        String name = rs.getString(2);
        System.out.println("name=" + name);
        if (rs.wasNull()) {
            System.out.println("name is null");
        } else {
            System.out.println("name is not null");
        }
        System.out.println("---------------");
    }
    rs.close();
    st.close();
    conn.close();

}