Advertisement
Guest User

Untitled

a guest
Oct 28th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1.     public String getAndUpdateEmail() {
  2.         String email = null;
  3.  
  4.         String[] colNames = { "email", "already_linked", "account_link_timestamp" };
  5.         String query = "select " + Stream.of(colNames).collect(Collectors.joining(", "))
  6.                 + " from email_accounts where already_linked = false for update";
  7.         System.out.println(query);
  8.  
  9.         try (Connection conn = DriverManager.getConnection(url, props); // Make sure conn.setAutoCommit(false);
  10.                 Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
  11.                 ResultSet rs = stmt.executeQuery(query)) {
  12.             conn.setAutoCommit(false);
  13.             while (rs.next()) {
  14.                 // Get the current values, if you need them.
  15.                 email = rs.getString(colNames[0]);
  16.                 boolean linked = rs.getBoolean(colNames[1]);
  17.                 Timestamp time = rs.getTimestamp(colNames[2]);
  18.                 // ...
  19.                 rs.updateBoolean(colNames[1], true);
  20.                 rs.updateTimestamp(colNames[2], //
  21.                         new Timestamp(System.currentTimeMillis()));
  22.                 rs.updateRow();
  23.             }
  24.             conn.commit();
  25.         } catch (SQLException e) {
  26.             e.printStackTrace();
  27.         }
  28.  
  29.         return email;
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement