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;
  
  /*
   * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
   * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
   */
  /**
   *
   * @author ahmed
   */
  public class AddCourse extends javax.swing.JFrame {
  
      /**
       * Creates new form AddCourse
       */
      public AddCourse() {
          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() {
  
          jPanel1 = new javax.swing.JPanel();
          courseTitle = new javax.swing.JTextField();
          jButton1 = new javax.swing.JButton();
          credithour = new javax.swing.JTextField();
          department_id = new javax.swing.JTextField();
          jLabel1 = new javax.swing.JLabel();
          jLabel2 = new javax.swing.JLabel();
          jButton9 = new javax.swing.JButton();
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
          jPanel1.setBackground(new java.awt.Color(0, 64, 128));
  
          courseTitle.setText("Enter Course's Title");
          courseTitle.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  courseTitleActionPerformed(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);
              }
          });
  
          credithour.setText("Enter Course's Credit Hour");
  
          department_id.setText("Department ID");
  
          jLabel2.setIcon(new javax.swing.ImageIcon("C:\\Users\\ahmed\\OneDrive\\Desktop\\download-removebg-preview (1) - Copy.png")); // NOI18N
  
          jButton9.setBackground(new java.awt.Color(0, 64, 128));
          jButton9.setForeground(new java.awt.Color(255, 255, 255));
          jButton9.setText("back");
          jButton9.addMouseListener(new java.awt.event.MouseAdapter() {
              public void mousePressed(java.awt.event.MouseEvent evt) {
                  jButton9MousePressed(evt);
              }
          });
  
          javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
          jPanel1.setLayout(jPanel1Layout);
          jPanel1Layout.setHorizontalGroup(
              jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(jPanel1Layout.createSequentialGroup()
                  .addGap(41, 41, 41)
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addComponent(jButton1)
                      .addGroup(jPanel1Layout.createSequentialGroup()
                          .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                              .addComponent(department_id, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                              .addComponent(credithour, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                              .addComponent(courseTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                              .addComponent(jButton9))
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                          .addComponent(jLabel2)))
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                  .addComponent(jLabel1)
                  .addContainerGap())
          );
          jPanel1Layout.setVerticalGroup(
              jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(jPanel1Layout.createSequentialGroup()
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addGroup(jPanel1Layout.createSequentialGroup()
                          .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                              .addGroup(jPanel1Layout.createSequentialGroup()
                                  .addContainerGap()
                                  .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                              .addGroup(jPanel1Layout.createSequentialGroup()
                                  .addGap(16, 16, 16)
                                  .addComponent(jButton9)
                                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                  .addComponent(courseTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                  .addComponent(credithour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                  .addComponent(department_id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                          .addComponent(jButton1))
                      .addGroup(jPanel1Layout.createSequentialGroup()
                          .addContainerGap()
                          .addComponent(jLabel2)))
                  .addContainerGap(76, 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(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          );
  
          pack();
      }// </editor-fold>//GEN-END:initComponents
  
      private void jButton9MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton9MousePressed
          // TODO add your handling code here:
          if (Login.adminlogin) {
              systemManag s = new systemManag();
              s.setVisible(true);
              s.setLocationRelativeTo(null);//center
              this.dispose();
          } else {
              InstructorManag s = new InstructorManag();
              s.setVisible(true);
              s.setLocationRelativeTo(null);//center
              this.dispose();
          }
      }//GEN-LAST:event_jButton9MousePressed
  
      private void courseTitleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_courseTitleActionPerformed
          // TODO add your handling code here:
      }//GEN-LAST:event_courseTitleActionPerformed
  
      private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
          // TODO add your handling code here:
          String title = courseTitle.getText();
          String credts = credithour.getText();
          int course_id = 0;
  
          for (int i = 0; i < title.length(); i++) {
              course_id+= Character.getNumericValue(title.charAt(i));
          }
          int departmentId;
  
  // Validate and parse department ID
          try {
              departmentId = Integer.parseInt(department_id.getText());
          } catch (NumberFormatException e) {
              JOptionPane.showMessageDialog(this, "Invalid department ID. Please enter a valid number.");
              return; // Exit if department ID is invalid
          }
  
  // Database operation
          try (Connection connection = ConnectToDB.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(
                  "INSERT INTO Course (Course_ID,Title,Credits,Dept_ID)"
                  + "VALUES(?,?,?,?);")) {
  
              // Set values for the prepared statement
              preparedStatement.setInt(1, course_id);
              preparedStatement.setString(2, title);
              preparedStatement.setString(3, credts);
              preparedStatement.setInt(4,departmentId);
             
              // Execute the insert operation
              preparedStatement.executeUpdate();
  
              // Success message
              JOptionPane.showMessageDialog(this, "Course Added Successfully");
  
          } catch (SQLException e) {
              Logger.getLogger(SignUp.class.getName()).log(Level.SEVERE, "Database error: ", e);
              JOptionPane.showMessageDialog(this, "An error occurred while saving This data. Please try again.");
          }
      }//GEN-LAST:event_jButton1ActionPerformed
  
      /**
       * @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(AddCourse.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (InstantiationException ex) {
              java.util.logging.Logger.getLogger(AddCourse.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (IllegalAccessException ex) {
              java.util.logging.Logger.getLogger(AddCourse.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (javax.swing.UnsupportedLookAndFeelException ex) {
              java.util.logging.Logger.getLogger(AddCourse.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 AddCourse().setVisible(true);
              }
          });
      }
  
      // Variables declaration - do not modify//GEN-BEGIN:variables
      private javax.swing.JTextField courseTitle;
      private javax.swing.JTextField credithour;
      private javax.swing.JTextField department_id;
      private javax.swing.JButton jButton1;
      private javax.swing.JButton jButton9;
      private javax.swing.JLabel jLabel1;
      private javax.swing.JLabel jLabel2;
      private javax.swing.JPanel jPanel1;
      // End of variables declaration//GEN-END:variables
  }
  