Snippet Java for MySQL

With snippet Java for MySQL, you can free your mind

  1. Get Last Inserted Auto Increment ID in MySQL

    PreparedStatement pstmt = conn.prepareStatement(Query, Statement.RETURN_GENERATED_KEYS);
    pstmt.executeUpdate();
    ResultSet keys = pstmt.getGeneratedKeys();
    keys.next();
    key = keys.getInt(1);
    
  2. Rollback When SQL Batch Fail

    Connection conn = null;
    Statement stmt = null;
    try {
      conn = openRedShiftConnection();
      conn.setAutoCommit(false);
      stmt = conn.createStatement();
      stmt.addBatch(MAY_CORRUPT_SQL_QUERY);
      stmt.addBatch(MAY_CORRUPT_SQL_QUERY);
      stmt.executeBatch();
    } catch (Exception e) {
      conn.rollback();
      throw e;
    } finally {
      stmt.close();
      conn.close();
    }
    

Comments

Popular posts from this blog

Reduce TIME_WAIT Socket Connections