List of usage examples for java.sql ResultSet close
void close() throws SQLException;
ResultSet
object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = null;//from w w w .jav 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 = "UPDATE Registration SET age = 30 WHERE id in (1, 2)"; stmt.executeUpdate(sql); 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(); 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 va2 s. c o m ResultSet rs = st.executeQuery("SELECT * FROM survey"); // extract data from the ResultSet while (rs.next()) { int id = rs.getInt("id"); 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 stmt = null;/* w ww .j ava 2 s . c o m*/ ResultSet rs = null; conn = getConnection(); stmt = conn.createStatement(); stmt.executeUpdate("insert into animals_table (name) values('newName')"); rs = stmt.getGeneratedKeys(); while (rs.next()) { ResultSetMetaData rsMetaData = rs.getMetaData(); int columnCount = rsMetaData.getColumnCount(); for (int i = 1; i <= columnCount; i++) { String key = rs.getString(i); System.out.println("key " + i + " is " + key); } } rs.close(); stmt.close(); conn.close(); }
From source file:TestAccessExcel.java
public static void main(String args[]) { Connection conn = null;//from ww w.ja v a 2s. co m Statement stmt = null; ResultSet rs = null; try { conn = getConnection(); stmt = conn.createStatement(); String excelQuery = "select * from [Sheet1$]"; rs = stmt.executeQuery(excelQuery); while (rs.next()) { System.out.println(rs.getString("BadgeNumber") + " " + rs.getString("FirstName") + " " + rs.getString("LastName")); } } catch (Exception e) { System.err.println(e.getMessage()); } finally { try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:DemoDisplayBinaryDataFromDatabase.java
public static void main(String args[]) throws Exception { Connection conn = null;/*from w w w .j a v a 2s . c o m*/ ResultSet rs = null; PreparedStatement pstmt = null; String query = "SELECT raw_column, long_raw_column FROM binary_table WHERE id = ?"; try { conn = getConnection(); Object[] results = new Object[2]; pstmt = conn.prepareStatement(query); pstmt.setString(1, "0001"); rs = pstmt.executeQuery(); rs.next(); // materialize binary data onto client results[0] = rs.getBytes("RAW_COLUMN"); results[1] = rs.getBytes("LONG_RAW_COLUMN"); } finally { rs.close(); pstmt.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();// w w w .j ava 2 s. co m ResultSet rs = st.executeQuery("SELECT * FROM survey"); int rsType = rs.getType(); if (rsType == java.sql.ResultSet.TYPE_FORWARD_ONLY) { System.out.println("java.sql.ResultSet.TYPE_FORWARD_ONLY"); } else if (rsType == java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE) { System.out.println("java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE"); } else if (rsType == java.sql.ResultSet.TYPE_SCROLL_SENSITIVE) { System.out.println("java.sql.ResultSet.TYPE_SCROLL_SENSITIVE"); } else { // it is an error } rs.close(); st.close(); conn.close(); }
From source file:JDBCQuery.java
public static void main(String[] av) { try {/*ww w .jav a 2 s .com*/ System.out.println("Loading Driver (with Class.forName)"); // Load the jdbc-odbc bridge driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Enable logging // DriverManager.setLogStream(System.err); System.out.println("Getting Connection"); Connection conn = DriverManager.getConnection("jdbc:odbc:Companies", "ian", ""); // user, passwd // Any warnings generated by the connect? checkForWarning(conn.getWarnings()); System.out.println("Creating Statement"); Statement stmt = conn.createStatement(); System.out.println("Executing Query"); ResultSet rs = stmt.executeQuery("SELECT * FROM Companies"); System.out.println("Retrieving Results"); int i = 0; while (rs.next()) { System.out.println("Retrieving Company ID"); int x = rs.getInt("CustNO"); System.out.println("Retrieving Name"); String s = rs.getString("Company"); System.out.println("ROW " + ++i + ": " + x + "; " + s + "; " + "."); } rs.close(); // All done with that resultset stmt.close(); // All done with that statement conn.close(); // All done with that DB connection } catch (ClassNotFoundException e) { System.out.println("Can't load driver " + e); } catch (SQLException e) { System.out.println("Database access failed " + e); } }
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"); // extract data from the ResultSet scroll from top while (rs.next()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id + " name=" + name); }//from w ww . ja va 2 s. c o m System.out.println("---------"); // scroll from the bottom rs.afterLast(); while (rs.previous()) { String id = rs.getString(1); String name = rs.getString(2); System.out.println("id=" + id + " name=" + name); } rs.close(); st.close(); conn.close(); }
From source file:MainClass.java
public static void main(String[] args) { Connection connection = null; Statement statement = null;//from ww w . j a va 2 s . c om ResultSet resultSet = null; try { connection = getConnection(); // Do work with connection statement = connection.createStatement(); String selectEmployeesSQL = "SELECT * FROM employees"; resultSet = statement.executeQuery(selectEmployeesSQL); while (resultSet.next()) { printEmployee(resultSet); } } catch (Exception e) { e.printStackTrace(); } finally { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { } // nothing we can do } if (statement != null) { try { statement.close(); } catch (SQLException e) { } // nothing we can do } if (connection != null) { try { connection.close(); } catch (SQLException e) { } // nothing we can do } } }
From source file:GetColumnNamesFromResultSet_Oracle.java
public static void main(String[] args) { Connection conn = null;/* www . j a v a 2s . c o m*/ Statement stmt = null; ResultSet rs = null; try { conn = getConnection(); // prepare query String query = "select id, name, age from employees"; // create a statement stmt = conn.createStatement(); // execute query and return result as a ResultSet rs = stmt.executeQuery(query); // get the column names from the ResultSet getColumnNames(rs); } catch (Exception e) { e.printStackTrace(); } finally { // release database resources try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }