Beispiel #1
0
  public static void main(String args[]) {

    Connection con;
    Statement stmt;
    ResultSet rs;

    try {
      con = ConnectionUtil.getConnection(args[0]);
      stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      rs = stmt.executeQuery("SELECT * FROM Users where name='ian';");

      // Get the resultset ready, update the passwd field, commit
      if (rs.next()) {
        rs.updateString("password", "unguessable");
        rs.updateRow();
      } else {
        System.out.println("Error: user not found");
      }

      rs.close();
      stmt.close();
      con.close();
    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
    }
  }
  public static void main(String[] av) throws Throwable {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {

      System.out.println("Getting Connection");
      conn = ConnectionUtil.getConnection("ecom");

      // Turn off auto-commit
      conn.setAutoCommit(false);

      // Any warnings generated by the connect?
      checkForWarning(conn.getWarnings());

      System.out.println("Creating Statement");
      stmt = conn.createStatement();

      System.out.println("Creating table");
      stmt.executeUpdate("create table test (id int, name varchar)");

      System.out.println("Executing Insert");
      stmt.executeUpdate("insert into test values(42, 'ian');");

      System.out.println("Sleeping...");
      Thread.sleep(60 * 1000);

      System.out.println("** Committing **");
      conn.commit();

      System.out.println("Re-reading table");
      rs = stmt.executeQuery("select * from test");
      int i = 0;
      while (rs.next()) {

        System.out.println("Retrieving ID");
        int x = rs.getInt(1);
        System.out.println("Retrieving Name");
        String s = rs.getString(2);

        System.out.println("ROW " + ++i + ": " + x + "; " + s + "; " + ".");
      }

      System.out.println("Removing table");
      stmt.executeUpdate("drop table test");
    } finally {
      SQLUtils.cleanup(rs, stmt, conn);
    }
  }