Esempio n. 1
0
  @Override
  public int update(VipadVO vipadVO) {

    int updateCount = 0;
    Connection con = null;
    PreparedStatement pstmt = null;

    try {

      Class.forName(driver);
      con = DriverManager.getConnection(url, userid, passwd);
      pstmt = con.prepareStatement(UPDATE);

      con.setAutoCommit(false); // 在JDBC裡面預設AutoCommit為true,假如要rollback,要把預設改為false

      pstmt.setInt(1, vipadVO.getGid());
      pstmt.setString(2, vipadVO.getMember_id());
      pstmt.setTimestamp(3, vipadVO.getJoindate());
      pstmt.setTimestamp(4, vipadVO.getQuitdate());
      pstmt.setInt(5, vipadVO.getStatus());
      pstmt.setInt(6, vipadVO.getVid());

      updateCount = pstmt.executeUpdate();

      con.commit();
      con.setAutoCommit(true); // 把AutoCommit為true

      // Handle any driver errors
    } catch (ClassNotFoundException e) {
      throw new RuntimeException("Couldn't load database driver. " + e.getMessage());
      // Handle any SQL errors
    } catch (SQLException se) {
      if (con != null) {
        try {
          System.out.println("Transaction begin, start rollback");
          con.rollback();
        } catch (Exception e) {
          System.out.println(e.getMessage());
        }
      }
      throw new RuntimeException("A database error occured. " + se.getMessage());
      // Clean up JDBC resources
    } finally {
      if (pstmt != null) {
        try {
          pstmt.close();
        } catch (SQLException se) {
          se.printStackTrace(System.err);
        }
      }
      if (con != null) {
        try {
          con.close();
        } catch (Exception e) {
          e.printStackTrace(System.err);
        }
      }
    }
    return updateCount;
  }