コード例 #1
0
  private void jButton51ActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jButton51ActionPerformed
    String userName = jTable11.getValueAt(jTable11.getSelectedRow(), 0).toString();
    String sect = jTable11.getValueAt(jTable11.getSelectedRow(), 1).toString();

    try {
      // connectDB.setAutoCommit(false);
      java.sql.PreparedStatement deletePs =
          connectDB.prepareStatement(
              "DELETE FROM maintenance.section_allocation WHERE user_name = '"
                  + jTable11.getModel().getValueAt(jTable11.getSelectedRow(), 0).toString()
                  + "' "
                  + "AND section = '"
                  + jTable11.getModel().getValueAt(jTable11.getSelectedRow(), 1).toString()
                  + "' ");

      deletePs.executeUpdate();
      connectDB.commit();
      // connectDB.setAutoCommit(true);

    } catch (java.sql.SQLException sq) {
      javax.swing.JOptionPane.showMessageDialog(
          this, sq.getMessage(), "Error Message!", javax.swing.JOptionPane.ERROR_MESSAGE);
      try {
        connectDB.rollback();
      } catch (java.sql.SQLException sql) {
        javax.swing.JOptionPane.showMessageDialog(
            this, sql.getMessage(), "Error Message!", javax.swing.JOptionPane.ERROR_MESSAGE);
      }
    }

    javax.swing.JOptionPane.showMessageDialog(
        this,
        userName + " has been removed from " + sect,
        "User Allocation Removed",
        javax.swing.JOptionPane.INFORMATION_MESSAGE);

    jButton31.doClick(); // Add your handling code here:.
  } // GEN-LAST:event_jButton51ActionPerformed
コード例 #2
0
  private void jButton32ActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jButton32ActionPerformed
    // TODO add your handling code here:

    try {
      java.sql.Statement popStat = connectDB.createStatement();
      java.sql.ResultSet popSet =
          popStat.executeQuery(
              "SELECT user_name, section FROM maintenance.section_allocation ORDER BY user_name");

      int row = 0;

      while (popSet.next()) {
        jTable11.setValueAt(popSet.getString(1), row, 0);
        jTable11.setValueAt(popSet.getString(2), row, 1);

        row += 1;
      }

    } catch (java.sql.SQLException sqle) {
      sqle.printStackTrace();
    }
  } // GEN-LAST:event_jButton32ActionPerformed
コード例 #3
0
  private void jButton11ActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jButton11ActionPerformed
    boolean noSection = false;

    for (int r = 0; r < jTable11.getRowCount(); r++) {
      if (!(jTable11.getValueAt(r, 0) == null)) {
        if (jTable11.getValueAt(r, 1).equals("-")) {
          noSection = true;
        }
      }
    }

    if (noSection) {
      javax.swing.JOptionPane.showMessageDialog(
          this,
          "Please ensure all the sections have been set!",
          "Incomplete row!",
          javax.swing.JOptionPane.INFORMATION_MESSAGE);

    } else {
      try {

        connectDB.setAutoCommit(false);

        java.sql.PreparedStatement insertPs =
            connectDB.prepareStatement(
                "INSERT INTO maintenance.section_allocation (section, user_name) VALUES (?, ?)");

        for (int r = 0; r < jTable11.getRowCount(); r++) {

          if (jTable11.getValueAt(r, 0) == null || jTable11.getValueAt(r, 1) == null) {
            // do nothing
          } else {
            insertPs.setString(1, jTable11.getValueAt(r, 1).toString());
            insertPs.setString(2, jTable11.getValueAt(r, 0).toString());

            insertPs.executeUpdate();
          }
        }

        connectDB.commit();
        connectDB.setAutoCommit(true);

        javax.swing.JOptionPane.showMessageDialog(
            this,
            "Maintenance Section(s) allocated successfully!",
            "Insert Successful!",
            javax.swing.JOptionPane.INFORMATION_MESSAGE);

        // clear table
        jButton31.doClick();

      } catch (java.sql.SQLException sqle) {
        sqle.printStackTrace();

        javax.swing.JOptionPane.showMessageDialog(
            this, sqle, "Error Message!!", javax.swing.JOptionPane.ERROR_MESSAGE);

        try {

          connectDB.rollback();
          connectDB.setAutoCommit(true);

        } catch (java.sql.SQLException sqlerollback) {

          sqlerollback.printStackTrace();
          javax.swing.JOptionPane.showMessageDialog(
              this, sqlerollback, "Error Message!!", javax.swing.JOptionPane.ERROR_MESSAGE);
        }
      }
    } // ends else
  } // GEN-LAST:event_jButton11ActionPerformed