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 = 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 to the beginning, before the first row. // cursor position is 0. rs.beforeFirst();/*from ww w. j a va 2 s . co m*/ rs.next(); // Get data at cursor String id = rs.getString("id"); System.out.println(id); 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(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 to the last row rs.absolute(-1);// w w w . j a v a 2 s .c om // Get data at cursor String id = rs.getString("id"); System.out.println(id); // Move cursor to the second-to-last row //rs.absolute(-2); 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) );"); String INSERT_RECORD = "insert into survey(id, name) values(?,?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); pstmt.setString(2, "name1"); pstmt.executeUpdate();/*from w w w. j a v a2 s . c o m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); pstmt.setString(1, "2"); pstmt.setString(2, "name2"); pstmt.executeUpdate(); rs = st.executeQuery("SELECT * FROM survey"); outputResultSet(rs); 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(ResultSet.TYPE_SCROLL_SENSITIVE, 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')"); ResultSet rs = st.executeQuery("SELECT * FROM survey"); // Move cursor to the row to update rs.first();//from w w w . j av a2 s.co m // Update the value of column column_1 on that row rs.updateString("name", "new data"); // Discard the update to the row rs.cancelRowUpdates(); 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(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();/*from w ww. jav a 2 s.co m*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); while (rs.next()) { String name = rs.getString(2); if (rs.wasNull()) { System.out.println("was NULL"); } else { System.out.println("not NULL"); } } 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(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();/* www. ja va 2 s . c om*/ ResultSet rs = st.executeQuery("SELECT * FROM survey"); while (rs.next()) { int id = rs.getInt(1); // index 1 is the "id" column String name = rs.getString(2); // index 2 is the "name" column System.out.println(id); System.out.println(name); } 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(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 to the end, after the last row rs.afterLast();/*from w w w. j a va 2 s.c o m*/ //String id = rs.getString("id");//Exception rs.previous(); // Get data at cursor String id = rs.getString("id"); System.out.println(id); rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { Connection conn = null;// w ww. j a v a2s . c o m Statement stmt = null; ResultSet rs = null; conn = getConnection(); stmt = conn.createStatement(); String excelQuery = "select * from [Sheet1$]"; rs = stmt.executeQuery(excelQuery); while (rs.next()) { System.out.println(rs.getString("FirstName") + " " + rs.getString("LastName")); } rs.close(); stmt.close(); conn.close(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { if (args.length != 2) { System.out.println("Usage: java JavaDBDemo <Name> <Address>"); System.exit(1);//from w w w . j a v a2 s. c om } String driver = "org.apache.derby.jdbc.EmbeddedDriver"; String dbName = "AddressBookDB"; String connectionURL = "jdbc:derby:" + dbName + ";create=true"; String createString = "CREATE TABLE ADDRESSBOOKTbl (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)"; Class.forName(driver); conn = DriverManager.getConnection(connectionURL); Statement stmt = conn.createStatement(); stmt.executeUpdate(createString); PreparedStatement psInsert = conn.prepareStatement("insert into ADDRESSBOOKTbl values (?,?)"); psInsert.setString(1, args[0]); psInsert.setString(2, args[1]); psInsert.executeUpdate(); Statement stmt2 = conn.createStatement(); ResultSet rs = stmt2.executeQuery("select * from ADDRESSBOOKTbl"); System.out.println("Addressed present in your Address Book\n\n"); int num = 0; while (rs.next()) { System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address" + rs.getString(2)); } rs.close(); }
From source file:ImageStringToBlob.java
public static void main(String[] args) { Connection conn = null;/*from www. j ava 2 s. c o m*/ if (args.length != 1) { System.out.println("Missing argument: full path to <oscar.properties>"); return; } try { FileInputStream fin = new FileInputStream(args[0]); Properties prop = new Properties(); prop.load(fin); String driver = prop.getProperty("db_driver"); String uri = prop.getProperty("db_uri"); String db = prop.getProperty("db_name"); String username = prop.getProperty("db_username"); String password = prop.getProperty("db_password"); Class.forName(driver); conn = DriverManager.getConnection(uri + db, username, password); conn.setAutoCommit(true); // no transactions /* * select all records ids with image_data not null and contents is null * for each id fetch record * migrate data from image_data to contents */ String sql = "select image_id from client_image where image_data is not null and contents is null"; PreparedStatement pst = conn.prepareStatement(sql); ResultSet rs = pst.executeQuery(); List<Long> ids = new ArrayList<Long>(); while (rs.next()) { ids.add(rs.getLong("image_id")); } rs.close(); sql = "select image_data from client_image where image_id = ?"; pst = conn.prepareStatement(sql); System.out.println("Migrating image data for " + ids.size() + " images..."); for (Long id : ids) { pst.setLong(1, id); ResultSet imagesRS = pst.executeQuery(); while (imagesRS.next()) { String dataString = imagesRS.getString("image_data"); Blob dataBlob = fromStringToBlob(dataString); if (writeBlobToDb(conn, id, dataBlob) == 1) { System.out.println("Image data migrated for image_id: " + id); } } imagesRS.close(); } System.out.println("Migration completed."); } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }