content: 
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.SQLException;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.swing.JOptionPane;
  
  /**
   *
   * @author ahmed
   */
  public class AddStudent extends javax.swing.JFrame {
  
      /**
       * Creates new form AddStudent
       */
      public AddStudent() {
          initComponents();
          this.setLocationRelativeTo(null);
      }
  
      /**
       * This method is called from within the constructor to initialize the form.
       * WARNING: Do NOT modify this code. The content of this method is always
       * regenerated by the Form Editor.
       */
      @SuppressWarnings("unchecked")
      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
      private void initComponents() {
  
          jPanel3 = new javax.swing.JPanel();
          name2 = new javax.swing.JTextField();
          jButton1 = new javax.swing.JButton();
          department = new javax.swing.JTextField();
          jLabel1 = new javax.swing.JLabel();
          jLabel2 = new javax.swing.JLabel();
          jButton10 = new javax.swing.JButton();
          confirmpassword = new javax.swing.JTextField();
          password = new javax.swing.JTextField();
          national_id = new javax.swing.JTextField();
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
          jPanel3.setBackground(new java.awt.Color(0, 64, 128));
  
          name2.setText("Enter Student Name");
          name2.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  name2ActionPerformed(evt);
              }
          });
  
          jButton1.setBackground(new java.awt.Color(0, 64, 128));
          jButton1.setForeground(new java.awt.Color(255, 255, 255));
          jButton1.setText("Enter");
          jButton1.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  jButton1ActionPerformed(evt);
              }
          });
  
          department.setText("Department ID");
  
          jLabel2.setIcon(new javax.swing.ImageIcon("C:\\Users\\ahmed\\OneDrive\\Desktop\\download-removebg-preview (1) - Copy.png")); // NOI18N
  
          jButton10.setBackground(new java.awt.Color(0, 64, 128));
          jButton10.setForeground(new java.awt.Color(255, 255, 255));
          jButton10.setText("back");
          jButton10.addMouseListener(new java.awt.event.MouseAdapter() {
              public void mousePressed(java.awt.event.MouseEvent evt) {
                  jButton10MousePressed(evt);
              }
          });
  
          confirmpassword.setText("Confirm Password");
          confirmpassword.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  confirmpasswordActionPerformed(evt);
              }
          });
  
          password.setText("Password");
  
          national_id.setText("National ID");
          national_id.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  national_idActionPerformed(evt);
              }
          });
  
          javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
          jPanel3.setLayout(jPanel3Layout);
          jPanel3Layout.setHorizontalGroup(
              jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(jPanel3Layout.createSequentialGroup()
                  .addGap(35, 35, 35)
                  .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addGroup(jPanel3Layout.createSequentialGroup()
                          .addComponent(jButton1)
                          .addGap(0, 0, Short.MAX_VALUE))
                      .addGroup(jPanel3Layout.createSequentialGroup()
                          .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                              .addComponent(confirmpassword)
                              .addComponent(national_id)
                              .addComponent(name2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                              .addComponent(department, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                              .addComponent(jButton10, javax.swing.GroupLayout.Alignment.LEADING)
                              .addComponent(password, javax.swing.GroupLayout.Alignment.LEADING))
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                          .addComponent(jLabel2)
                          .addGap(12, 12, 12)
                          .addComponent(jLabel1)))
                  .addContainerGap())
          );
          jPanel3Layout.setVerticalGroup(
              jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(jPanel3Layout.createSequentialGroup()
                  .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addGroup(jPanel3Layout.createSequentialGroup()
                          .addGap(15, 15, 15)
                          .addComponent(jLabel2))
                      .addGroup(jPanel3Layout.createSequentialGroup()
                          .addContainerGap()
                          .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addGroup(jPanel3Layout.createSequentialGroup()
                          .addGap(36, 36, 36)
                          .addComponent(jButton10)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                          .addComponent(name2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(department, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(national_id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addComponent(confirmpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                  .addComponent(jButton1)
                  .addContainerGap(60, Short.MAX_VALUE))
          );
  
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          );
  
          pack();
      }// </editor-fold>//GEN-END:initComponents
  
      private void name2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_name2ActionPerformed
          // TODO add your handling code here:
      }//GEN-LAST:event_name2ActionPerformed
  
      private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
          // TODO: Add your handling code here:
  
  // Get user inputs
          String studentname = name2.getText();
          String studentpassword = password.getText();
          String confirmPassword = confirmpassword.getText();
          String studentnation = national_id.getText();
          int departmentId;
  
  // Validate password match
          if (!studentpassword.equals(confirmPassword)) {
              JOptionPane.showMessageDialog(this, "Passwords do not match. Please try again.");
              return; // Exit if passwords do not match
          }
  
  // Validate and parse department ID
          try {
              departmentId = Integer.parseInt(department.getText());
          } catch (NumberFormatException e) {
              JOptionPane.showMessageDialog(this, "Invalid department ID. Please enter a valid number.");
              return; // Exit if department ID is invalid
          }
  
  // Ensure the national ID is a valid integer
          int nationalId;
          try {
              nationalId = Integer.parseInt(studentnation);
          } catch (NumberFormatException e) {
              JOptionPane.showMessageDialog(this, "Invalid National ID. Please enter a valid number.");
              return; // Exit if national ID is invalid
          }
  
  // Database operation
          try (Connection connection = ConnectToDB.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(
                  "INSERT INTO Student (Student_ID, Student_Name, Student_Password, Student_National_ID, Total_cred, Dept_ID) "
                  + "VALUES (?, ?, ?, ?, ?, ?);")) {
  
              // Set values for the prepared statement
              preparedStatement.setInt(1, nationalId);
              preparedStatement.setString(2, studentname);
              preparedStatement.setString(3, studentpassword);
              preparedStatement.setString(4, studentnation);
              preparedStatement.setInt(5, 0); // Default value for Total_cred
              preparedStatement.setInt(6, departmentId);
  
              // Execute the insert operation
              int rowsAffected = preparedStatement.executeUpdate();
  
              // Success message
              if (rowsAffected > 0) {
                  JOptionPane.showMessageDialog(this, "Student added successfully.");
              } else {
                  JOptionPane.showMessageDialog(this, "Failed to add the student. Please try again.");
              }
  
          } catch (SQLException e) {
              // Log error and show a friendly error message
              Logger.getLogger(SignUp.class.getName()).log(Level.SEVERE, "Database error: ", e);
  
              if (e.getMessage().contains("FK")) {
                  JOptionPane.showMessageDialog(this, "The Department ID does not exist. Please check and try again.");
              } else if (e.getMessage().contains("PRIMARY")) {
                  JOptionPane.showMessageDialog(this, "The National ID already exists. Please use a unique National ID.");
              } else {
                  JOptionPane.showMessageDialog(this, "An error occurred while saving your data. Please try again.");
              }
          }
  
      }//GEN-LAST:event_jButton1ActionPerformed
  
      private void jButton10MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton10MousePressed
          // TODO add your handling code here:
          systemManag s = new systemManag();
          s.setVisible(true);
          s.setLocationRelativeTo(null);//center
          this.dispose();
      }//GEN-LAST:event_jButton10MousePressed
  
      private void confirmpasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmpasswordActionPerformed
          // TODO add your handling code here:
      }//GEN-LAST:event_confirmpasswordActionPerformed
  
      private void national_idActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_national_idActionPerformed
          // TODO add your handling code here:
      }//GEN-LAST:event_national_idActionPerformed
  
      /**
       * @param args the command line arguments
       */
      public static void main(String args[]) {
          /* Set the Nimbus look and feel */
          //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
          /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
           * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
           */
          try {
              for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                  if ("Nimbus".equals(info.getName())) {
                      javax.swing.UIManager.setLookAndFeel(info.getClassName());
                      break;
                  }
              }
          } catch (ClassNotFoundException ex) {
              java.util.logging.Logger.getLogger(AddStudent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (InstantiationException ex) {
              java.util.logging.Logger.getLogger(AddStudent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (IllegalAccessException ex) {
              java.util.logging.Logger.getLogger(AddStudent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (javax.swing.UnsupportedLookAndFeelException ex) {
              java.util.logging.Logger.getLogger(AddStudent.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
          //</editor-fold>
  
          /* Create and display the form */
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  new AddStudent().setVisible(true);
              }
          });
      }
  
      // Variables declaration - do not modify//GEN-BEGIN:variables
      private javax.swing.JTextField confirmpassword;
      private javax.swing.JTextField department;
      private javax.swing.JButton jButton1;
      private javax.swing.JButton jButton10;
      private javax.swing.JLabel jLabel1;
      private javax.swing.JLabel jLabel2;
      private javax.swing.JPanel jPanel1;
      private javax.swing.JPanel jPanel2;
      private javax.swing.JPanel jPanel3;
      private javax.swing.JTextField name;
      private javax.swing.JTextField name1;
      private javax.swing.JTextField name2;
      private javax.swing.JTextField national_id;
      private javax.swing.JTextField password;
      // End of variables declaration//GEN-END:variables
  }
  