List of usage examples for java.sql ResultSet CONCUR_UPDATABLE
int CONCUR_UPDATABLE
To view the source code for java.sql ResultSet CONCUR_UPDATABLE.
Click Source Link
ResultSet
object that may be updated. From source file:Main.java
public static void main(String[] args) { Connection conn = null;/* w w w . java 2s .c om*/ 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 con = DriverManager.getConnection("jdbc:mysql://192.168.1.3/zzzTest?" + // "useUnicode=yes&characterEncoding=UTF-8" + // "&user=root&password=whatever"); String newName = "G"; String newEmail = "g@example.com"; String newMobile = "444-555-2222"; String sql = "SELECT " + // "id, " + // "name, " + // "email, " + // "mobile " + // "FROM registerSmsUsers " + // "WHERE mobile = ? " + // "FOR UPDATE"; PreparedStatement pst = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); pst.setString(1, newMobile);/* ww w. j a v a2s .c o m*/ ResultSet rs = pst.executeQuery(); if (rs.next()) { rs.moveToCurrentRow(); rs.updateString("name", newName); rs.updateString("email", newEmail); rs.updateRow(); System.out.println("Existing row updated."); } else { rs.moveToInsertRow(); rs.updateString("name", newName); rs.updateString("email", newEmail); rs.updateString("mobile", newMobile); rs.insertRow(); System.out.println("New row inserted."); } }
From source file:TestBatchUpdate.java
public static void main(String args[]) { Connection conn = null;/*from w ww .j ava 2s .com*/ Statement stmt = null; ResultSet rs = null; try { conn = getConnection(); stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); conn.setAutoCommit(false); stmt.addBatch("INSERT INTO batch_table(id, name) VALUES('11', 'A')"); stmt.addBatch("INSERT INTO batch_table(id, name) VALUES('22', 'B')"); stmt.addBatch("INSERT INTO batch_table(id, name) VALUES('33', 'C')"); int[] updateCounts = stmt.executeBatch(); conn.commit(); rs = stmt.executeQuery("SELECT * FROM batch_table"); while (rs.next()) { String id = rs.getString("id"); String name = rs.getString("name"); System.out.println("id=" + id + " name=" + name); } } catch (BatchUpdateException b) { System.err.println("SQLException: " + b.getMessage()); System.err.println("SQLState: " + b.getSQLState()); System.err.println("Message: " + b.getMessage()); System.err.println("Vendor error code: " + b.getErrorCode()); System.err.print("Update counts: "); int[] updateCounts = b.getUpdateCounts(); for (int i = 0; i < updateCounts.length; i++) { System.err.print(updateCounts[i] + " "); } } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); System.err.println("SQLState: " + ex.getSQLState()); System.err.println("Message: " + ex.getMessage()); System.err.println("Vendor error code: " + ex.getErrorCode()); } catch (Exception e) { System.err.println("Exception: " + e.getMessage()); } finally { try { rs.close(); stmt.close(); conn.close(); } catch (Exception ignore) { } } }
From source file:InsertRowUpdatableResultSet_MySQL.java
public static void main(String[] args) { Connection conn = null;//from w w w. ja va 2 s .c o 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); } // 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:DemoUpdatableResultSet.java
public static void main(String[] args) { ResultSet rs = null;/* w w w . j a v a 2s. c o m*/ Connection conn = null; PreparedStatement pstmt = null; try { conn = getConnection(); String query = "select id, name, age from employees where age > ?"; pstmt = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); pstmt.setInt(1, 20); // set input values rs = pstmt.executeQuery(); // create an updatable ResultSet // update a column value in the current row. rs.absolute(2); // moves the cursor to the 2nd row of rs rs.updateString("name", "newName"); // updates the 'name' column of row 2 to newName rs.updateRow(); // updates the row in the data source // insert column values into the insert row. rs.moveToInsertRow(); // moves cursor to the insert row rs.updateInt(1, 1234); // 1st column id=1234 rs.updateString(2, "newName"); // updates the 2nd column rs.updateInt(3, 99); // updates the 3rd column to 99 rs.insertRow(); rs.moveToCurrentRow(); } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:test.NewClass.java
public static void main(String[] args) throws JSONException { String db_url = "jdbc:jtds:sqlserver://w2ksa.cs.cityu.edu.hk:1433/aiad001_db;"; String user = "aiad001"; String db_user = user;/*from w w w . ja v a 2s.c o m*/ String pwd = "aiad001"; String db_password = pwd; String str_q = "SELECT * FROM \"User\""; try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); Connection conn = DriverManager.getConnection(db_url, user, pwd); Statement statmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = statmt.executeQuery(str_q); ResultSetMetaData rsmd = rs.getMetaData(); int numOfColumns = rsmd.getColumnCount(); for (int i = 1; i <= numOfColumns; i++) { System.out.printf("%s ", rsmd.getColumnName(i)); } System.out.println(""); while (rs.next()) { System.out.printf("{%s %20s %s %s}\n", rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)); if (rs.getString(1).equals("1")) rs.updateString(4, "abcd1111"); System.out.println(rsmd); } } catch (ClassNotFoundException ex) { Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:ResultSetUpdate.java
public static void main(String args[]) { String url;//from w ww.j a v a 2 s. c o 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:BatchUpdate.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;/* w ww . j ava 2s . c o m*/ Statement stmt; try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); con.setAutoCommit(false); stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Amaretto', 49, 9.99, 0, 0)"); stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Hazelnut', 49, 9.99, 0, 0)"); stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Amaretto_decaf', 49, 10.99, 0, 0)"); stmt.addBatch("INSERT INTO COFFEES " + "VALUES('Hazelnut_decaf', 49, 10.99, 0, 0)"); int[] updateCounts = stmt.executeBatch(); ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES"); System.out.println("Table COFFEES after insertion:"); while (uprs.next()) { String name = uprs.getString("COF_NAME"); int id = uprs.getInt("SUP_ID"); float price = uprs.getFloat("PRICE"); int sales = uprs.getInt("SALES"); int total = uprs.getInt("TOTAL"); System.out.print(name + " " + id + " " + price); System.out.println(" " + sales + " " + total); } uprs.close(); stmt.close(); con.close(); } catch (BatchUpdateException b) { System.err.println("SQLException: " + b.getMessage()); System.err.println("SQLState: " + b.getSQLState()); System.err.println("Message: " + b.getMessage()); System.err.println("Vendor: " + b.getErrorCode()); System.err.print("Update counts: "); int[] updateCounts = b.getUpdateCounts(); for (int i = 0; i < updateCounts.length; i++) { System.err.print(updateCounts[i] + " "); } } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); System.err.println("SQLState: " + ex.getSQLState()); System.err.println("Message: " + ex.getMessage()); System.err.println("Vendor: " + ex.getErrorCode()); } }
From source file:InsertRows.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;//from w w w.j a v a2 s .co m Statement stmt; try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES"); uprs.moveToInsertRow(); uprs.updateString("COF_NAME", "Kona"); uprs.updateInt("SUP_ID", 150); uprs.updateFloat("PRICE", 10.99f); uprs.updateInt("SALES", 0); uprs.updateInt("TOTAL", 0); uprs.insertRow(); uprs.updateString("COF_NAME", "Kona_Decaf"); uprs.updateInt("SUP_ID", 150); uprs.updateFloat("PRICE", 11.99f); uprs.updateInt("SALES", 0); uprs.updateInt("TOTAL", 0); uprs.insertRow(); uprs.beforeFirst(); System.out.println("Table COFFEES after insertion:"); while (uprs.next()) { String name = uprs.getString("COF_NAME"); int id = uprs.getInt("SUP_ID"); float price = uprs.getFloat("PRICE"); int sales = uprs.getInt("SALES"); int total = uprs.getInt("TOTAL"); System.out.print(name + " " + id + " " + price); System.out.println(" " + sales + " " + total); } uprs.close(); stmt.close(); con.close(); } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } }
From source file:InsertRow.java
public static void main(String args[]) { String url = "jdbc:mySubprotocol:myDataSource"; Connection con;//from w ww . java 2s. co m Statement stmt; String query = "select COF_NAME, PRICE from COFFEES"; try { Class.forName("myDriver.ClassName"); } catch (java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "myLogin", "myPassword"); stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES"); uprs.moveToInsertRow(); uprs.updateString("COF_NAME", "Kona"); uprs.updateInt("SUP_ID", 150); uprs.updateFloat("PRICE", 10.99f); uprs.updateInt("SALES", 0); uprs.updateInt("TOTAL", 0); uprs.insertRow(); uprs.beforeFirst(); System.out.println("Table COFFEES after insertion:"); while (uprs.next()) { String s = uprs.getString("COF_NAME"); int sup = uprs.getInt("SUP_ID"); float f = uprs.getFloat("PRICE"); int sales = uprs.getInt("SALES"); int t = uprs.getInt("TOTAL"); System.out.print(s + " " + sup + " " + f + " "); System.out.println(sales + " " + t); } uprs.close(); stmt.close(); con.close(); } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } }