diff --git a/223002965(Bella) and 223003980(Bethiane) b/223002965(Bella) and 223003980(Bethiane)
new file mode 100644
index 0000000..c7afb47
--- /dev/null
+++ b/223002965(Bella) and 223003980(Bethiane)
@@ -0,0 +1,787 @@
+// Question 1: In this question, we created a class called Loan with 3 instances cariables. After we created a constructor Loan for those variables using this keyword.
+We were able to setters and getters and the in the main method, we calculated the total amount by hardcoding the input parameters.
+
+
+package com.mycompany.oop;
+
+/**
+ *
+ * @author team B
+ */
+
+//class
+public class Loan {
+
+ //instance variables
+ private double monthlyPayment;
+ private int numberOfYears;
+ private double totalAmount;
+
+//constructor loan
+ public Loan(double monthlyPayment, int numberOfYears, double totalAmount) {
+ this.monthlyPayment = monthlyPayment;
+ this.numberOfYears = numberOfYears;
+ this.totalAmount = totalAmount;
+ }
+
+//setters for variables
+ public void setMonthlyPayment(double monthlyPayment) {
+ this.monthlyPayment = monthlyPayment;
+ }
+
+ public void setNumberOfYears(int numberOfYears) {
+ this.numberOfYears = numberOfYears;
+ }
+
+ public void setTotalAmount(double totalAmount) {
+ this.totalAmount = totalAmount;
+ }
+
+ public void calculation() {
+ this.totalAmount = this.monthlyPayment * this.numberOfYears * 12;
+ }
+
+ //getters for variables
+ public double getMonthlyPayment() {
+ return monthlyPayment;
+ }
+
+ public int getNumberOfYears() {
+ return numberOfYears;
+ }
+
+ public double getTotalAmount() {
+ return totalAmount;
+ }
+
+ public static void main(String[] args) {
+
+ //testing codes
+ Loan loan = new Loan(10000, 5, 0);
+
+ loan.calculation();
+
+ System.out.println("Monthly Payment: " + loan.getMonthlyPayment());
+ System.out.println("Number of Years: " + loan.getNumberOfYears());
+ System.out.println("Total Amount for Loan: " + loan.getTotalAmount());
+ }
+
+}
+
+
+// Question 2: For this question, it wasn't easy for us to accomplish but finally we successfully executed it. The first thing was to start XAMPP and we manually created database 'school'.
+After that we started with classes. Here we created three diffrent classes to make things easier to us to understand.
+The first class is Student.java. Within that class, we tested the connection to database and worked and after we created the table 'students' to be used in the task to store our data. That the only
+thing done in this first class.
+
+
+// Class One: Student.java
+
+package com.mycompany.school.gui;
+
+import java.sql.*;
+
+/**
+ *
+ * @author team B
+ */
+
+//This class initiated the connection to database and it creates the table students in database school
+
+public class Student {
+
+ public static void main(String[] args) {
+ String jdbcUrl = "jdbc:mysql://localhost:3306/school";
+ String username = "root";
+ String password = "";
+ try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) {
+ System.out.println("Connected to the database.");
+ } catch (SQLException e) {
+ System.err.println("Connection failed.");
+ e.printStackTrace();
+ }
+
+ try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) {
+
+ String createTableSQL = "CREATE TABLE IF NOT EXISTS students("
+ + "id INT PRIMARY KEY AUTO_INCREMENT,"
+ + "firstname VARCHAR(50),"
+ + "lastname VARCHAR(50),"
+ + "university VARCHAR(50),"
+ + "college VARCHAR(50),"
+ + "school VARCHAR(50),"
+ + "department VARCHAR(50),"
+ + "year VARCHAR(30),"
+ + "regnumber VARCHAR(30),"
+ + "modules VARCHAR(30))";
+ Statement statement = connection.createStatement();
+ statement.execute(createTableSQL);
+ System.out.println("Created");
+ } catch (SQLException e) {
+ // System.err.println("Failed");
+ e.printStackTrace();
+ }
+
+ }
+}
+
+
+
+//Class 2 : StudentForm.java (This is a JFrame with a form to get the information of student from the user).
+// For this class, we used java swing to create a graphical user interface form, we styled it by changing colors and fonts to make it beautify. When done, we then added functionalities. The task requested was
+to take the inputs entered by the user in the form and then store them into students table. We created method to take the inputs and that method was assigned to the submit button so that when they form is submitted,
+the data would be sent to our table in database.
+
+package com.mycompany.school.gui;
+
+import java.sql.*;
+import javax.swing.JOptionPane;
+
+/**
+ *
+ * @author team B
+ */
+public class StudentForm extends javax.swing.JFrame {
+
+ // Database connection parameters
+ private static final String jdbcUrl = "jdbc:mysql://localhost:3306/school";
+ private static final String username = "root";
+ private static final String password = "";
+
+ /**
+ * Creates new form StudentForm
+ */
+ public StudentForm() {
+ initComponents();
+ setupSubmitButton(); //for the submit button
+ }
+
+ private void setupSubmitButton() {
+ submit.addActionListener(new java.awt.event.ActionListener() { //add an event listener which will submit the form
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ submitForm();
+ }
+ });
+ }
+
+
+
+ /**
+ * 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")
+ //
+ private void initComponents() {
+
+ jLabel1 = new javax.swing.JLabel();
+ firstname = new javax.swing.JTextField();
+ fnlabel = new javax.swing.JLabel();
+ jLabel3 = new javax.swing.JLabel();
+ lastname = new javax.swing.JTextField();
+ university = new javax.swing.JTextField();
+ jLabel4 = new javax.swing.JLabel();
+ college = new javax.swing.JTextField();
+ jLabel5 = new javax.swing.JLabel();
+ school = new javax.swing.JTextField();
+ jLabel6 = new javax.swing.JLabel();
+ department = new javax.swing.JTextField();
+ jLabel7 = new javax.swing.JLabel();
+ jLabel8 = new javax.swing.JLabel();
+ year = new javax.swing.JTextField();
+ jLabel9 = new javax.swing.JLabel();
+ regno = new javax.swing.JTextField();
+ submit = new javax.swing.JButton();
+ jLabel2 = new javax.swing.JLabel();
+ mathematics = new javax.swing.JCheckBox();
+ physics = new javax.swing.JCheckBox();
+ english = new javax.swing.JCheckBox();
+ database = new javax.swing.JCheckBox();
+ listbtn = new javax.swing.JButton();
+
+ setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
+
+ jLabel1.setFont(new java.awt.Font("Space Grotesk SemiBold", 1, 24)); // NOI18N
+ jLabel1.setText("STUDENT REGISTRATION FORM");
+
+ fnlabel.setFont(new java.awt.Font("Montserrat Medium", 0, 14)); // NOI18N
+ fnlabel.setText("Firstname");
+
+ jLabel3.setFont(new java.awt.Font("Montserrat Medium", 0, 14)); // NOI18N
+ jLabel3.setText("Lastname");
+
+ jLabel4.setFont(new java.awt.Font("Montserrat Medium", 0, 14)); // NOI18N
+ jLabel4.setText("University");
+
+ college.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ collegeActionPerformed(evt);
+ }
+ });
+
+ jLabel5.setFont(new java.awt.Font("Montserrat Medium", 0, 14)); // NOI18N
+ jLabel5.setText("College");
+
+ jLabel6.setFont(new java.awt.Font("Montserrat Medium", 0, 14)); // NOI18N
+ jLabel6.setText("School");
+
+ jLabel7.setFont(new java.awt.Font("Montserrat Medium", 0, 14)); // NOI18N
+ jLabel7.setText("Department");
+
+ jLabel8.setFont(new java.awt.Font("Montserrat Medium", 0, 14)); // NOI18N
+ jLabel8.setText("Year");
+
+ jLabel9.setFont(new java.awt.Font("Montserrat Medium", 0, 14)); // NOI18N
+ jLabel9.setText("Reg Number");
+
+ submit.setBackground(new java.awt.Color(0, 102, 255));
+ submit.setFont(new java.awt.Font("Space Grotesk Medium", 0, 20)); // NOI18N
+ submit.setForeground(new java.awt.Color(255, 255, 255));
+ submit.setText("SUBMIT THE FORM");
+ submit.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ submitActionPerformed(evt);
+ }
+ });
+
+ jLabel2.setText("SELECT MODULES");
+
+ mathematics.setText("Mathematics");
+ mathematics.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ mathematicsActionPerformed(evt);
+ }
+ });
+
+ physics.setText("Physics");
+ physics.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ physicsActionPerformed(evt);
+ }
+ });
+
+ english.setText("English");
+ english.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ englishActionPerformed(evt);
+ }
+ });
+
+ database.setText("Database");
+ database.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ databaseActionPerformed(evt);
+ }
+ });
+
+ listbtn.setBackground(new java.awt.Color(51, 102, 255));
+ listbtn.setForeground(new java.awt.Color(255, 255, 255));
+ listbtn.setText("VIEW STUDENTS LIST");
+ listbtn.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
+ listbtn.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ listbtnActionPerformed(evt);
+ }
+ });
+
+ javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
+ getContentPane().setLayout(layout);
+ layout.setHorizontalGroup(
+ layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+ .addGroup(layout.createSequentialGroup()
+ .addGap(59, 59, 59)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+ .addGroup(layout.createSequentialGroup()
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
+ .addGroup(layout.createSequentialGroup()
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
+ .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(fnlabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE))
+ .addGap(18, 18, 18)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
+ .addComponent(firstname, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(lastname, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(university, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(college, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(school, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(department, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(year, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(regno, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)))
+ .addComponent(submit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
+ .addGap(83, 83, 83)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
+ .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
+ .addComponent(mathematics, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(physics, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(english, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(database, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
+ .addComponent(listbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)))
+ .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addContainerGap(104, Short.MAX_VALUE))
+ );
+ layout.setVerticalGroup(
+ layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+ .addGroup(layout.createSequentialGroup()
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+ .addGroup(layout.createSequentialGroup()
+ .addGap(42, 42, 42)
+ .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addGap(18, 18, 18)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(firstname, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(fnlabel, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
+ .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
+ .addGap(75, 75, 75)
+ .addComponent(listbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
+ .addGap(18, 18, 18)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(lastname, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addGap(18, 18, 18)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(university, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(jLabel2))
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+ .addGroup(layout.createSequentialGroup()
+ .addGap(18, 18, 18)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(college, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addGap(22, 22, 22)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(school, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addGap(18, 18, 18)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(department, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addGap(18, 18, 18)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(year, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addGap(18, 18, 18)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(regno, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addGap(44, 44, 44)
+ .addComponent(submit, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addGroup(layout.createSequentialGroup()
+ .addGap(7, 7, 7)
+ .addComponent(mathematics)
+ .addGap(18, 18, 18)
+ .addComponent(physics)
+ .addGap(18, 18, 18)
+ .addComponent(database)
+ .addGap(18, 18, 18)
+ .addComponent(english)))
+ .addContainerGap(40, Short.MAX_VALUE))
+ );
+
+ pack();
+ }//
+
+
+
+
+ // a method to submit the form data
+ private void submitForm() {
+ // Collect form data
+ String fname = firstname.getText().trim();
+ String lname = lastname.getText().trim();
+ String uni = university.getText().trim();
+ String coll = college.getText().trim();
+ String sch = school.getText().trim();
+ String dept = department.getText().trim();
+ String yr = year.getText().trim();
+ String reg = regno.getText().trim();
+
+ // Collect selected modules
+ StringBuilder modules = new StringBuilder();
+ if (mathematics.isSelected()) {
+ modules.append("Mathematics,");
+ }
+ if (physics.isSelected()) {
+ modules.append("Physics,");
+ }
+ if (database.isSelected()) {
+ modules.append("Database,");
+ }
+ if (english.isSelected()) {
+ modules.append("English,");
+ }
+ String selectedModules = modules.length() > 0
+ ? modules.substring(0, modules.length() - 1) : "";
+ // Validate inputs to ensure that they are all required
+ if (fname.isEmpty() || lname.isEmpty() || uni.isEmpty() || coll.isEmpty()
+ || sch.isEmpty() || dept.isEmpty() || yr.isEmpty() || reg.isEmpty()) {
+ JOptionPane.showMessageDialog(this,
+ "Please fill in all fields",
+ "Validation Error",
+ JOptionPane.ERROR_MESSAGE);
+ return;
+ }
+ try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {
+ String sql = "INSERT INTO students (firstname, lastname, university, college, "
+ + "school, department, year, regnumber, modules) "
+ + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
+
+ try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
+ pstmt.setString(1, fname);
+ pstmt.setString(2, lname);
+ pstmt.setString(3, uni);
+ pstmt.setString(4, coll);
+ pstmt.setString(5, sch);
+ pstmt.setString(6, dept);
+ pstmt.setString(7, yr);
+ pstmt.setString(8, reg);
+ pstmt.setString(9, selectedModules);
+ int rowsAffected = pstmt.executeUpdate();
+ if (rowsAffected > 0) {
+ JOptionPane.showMessageDialog(this,
+ "Registration successful!",
+ "Success",
+ JOptionPane.INFORMATION_MESSAGE);
+ clearForm();
+ } else {
+ JOptionPane.showMessageDialog(this,
+ "Registration failed",
+ "Error",
+ JOptionPane.ERROR_MESSAGE);
+ }
+ }
+ } catch (SQLException ex) {
+ JOptionPane.showMessageDialog(this,
+ "Database error: " + ex.getMessage(),
+ "Error",
+ JOptionPane.ERROR_MESSAGE);
+ ex.printStackTrace();
+ }
+ }
+// once form is submitted then it will clear all data for the user to insert new inputs
+ private void clearForm() {
+ firstname.setText("");
+ lastname.setText("");
+ university.setText("");
+ college.setText("");
+ school.setText("");
+ department.setText("");
+ year.setText("");
+ regno.setText("");
+ mathematics.setSelected(false);
+ database.setSelected(false);
+ physics.setSelected(false);
+ english.setSelected(false);
+ }
+
+
+ private void submitActionPerformed(java.awt.event.ActionEvent evt) {
+ // TODO add your handling code here:
+ }
+
+ private void collegeActionPerformed(java.awt.event.ActionEvent evt) {
+ // TODO add your handling code here:
+ }
+
+ private void mathematicsActionPerformed(java.awt.event.ActionEvent evt) {
+ // TODO add your handling code here:
+ }
+
+ private void physicsActionPerformed(java.awt.event.ActionEvent evt) {
+ // TODO add your handling code here:
+ }
+
+ private void englishActionPerformed(java.awt.event.ActionEvent evt) {
+ // TODO add your handling code here:
+ }
+
+ private void databaseActionPerformed(java.awt.event.ActionEvent evt) {
+ // TODO add your handling code here:
+ }
+
+//this is a button that send us to a new page of viewing the inserted data(data in database)
+ private void listbtnActionPerformed(java.awt.event.ActionEvent evt) {
+ StudentList sdnlist =new StudentList();
+ StudentForm.this.setVisible(false);
+ sdnlist.setVisible(true);
+
+ }
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String args[]) {
+ /* Set the Nimbus look and feel */
+ //
+ /* 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(StudentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+ } catch (InstantiationException ex) {
+ java.util.logging.Logger.getLogger(StudentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+ } catch (IllegalAccessException ex) {
+ java.util.logging.Logger.getLogger(StudentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+ } catch (javax.swing.UnsupportedLookAndFeelException ex) {
+ java.util.logging.Logger.getLogger(StudentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+ }
+ //
+
+ /* Create and display the form */
+ java.awt.EventQueue.invokeLater(new Runnable() {
+ public void run() {
+ new StudentForm().setVisible(true);
+ }
+ });
+ }
+
+ // Variables declaration - do not modify
+ private javax.swing.JTextField college;
+ private javax.swing.JCheckBox database;
+ private javax.swing.JTextField department;
+ private javax.swing.JCheckBox english;
+ private javax.swing.JTextField firstname;
+ private javax.swing.JLabel fnlabel;
+ private javax.swing.JLabel jLabel1;
+ private javax.swing.JLabel jLabel2;
+ private javax.swing.JLabel jLabel3;
+ private javax.swing.JLabel jLabel4;
+ private javax.swing.JLabel jLabel5;
+ private javax.swing.JLabel jLabel6;
+ private javax.swing.JLabel jLabel7;
+ private javax.swing.JLabel jLabel8;
+ private javax.swing.JLabel jLabel9;
+ private javax.swing.JTextField lastname;
+ private javax.swing.JButton listbtn;
+ private javax.swing.JCheckBox mathematics;
+ private javax.swing.JCheckBox physics;
+ private javax.swing.JTextField regno;
+ private javax.swing.JTextField school;
+ private javax.swing.JButton submit;
+ private javax.swing.JTextField university;
+ private javax.swing.JTextField year;
+ // End of variables declaration
+}
+
+
+Class Three : StudentList.java (This is a JFrame with a table to display the information of student inserted into the database). With the preparedstatement, we were able to retrieve that we submitted
+to our database using the form. And with this class, we retrieved data and displayed in tabular form with attributes and from our PC, we have 4 rows affected and displayed in our table.
+
+package com.mycompany.school.gui;
+
+import java.sql.*;
+import javax.swing.*;
+import javax.swing.table.DefaultTableModel;
+
+/**
+ *
+ * @author team B
+ */
+public class StudentList extends javax.swing.JFrame {
+
+
+ private static final String jdbcUrl = "jdbc:mysql://localhost:3306/school";
+ private static final String username = "root";
+ private static final String password = "";
+
+ /**
+ * Creates new form StudentList
+ */
+ public StudentList() {
+ initComponents();
+ loadStudentData(); //method to display our data
+ }
+
+ private void loadStudentData() {
+ DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
+ model.setRowCount(0); // Clear existing data
+
+ try {
+ // Create database connection
+ Connection conn = DriverManager.getConnection(jdbcUrl, username, password);
+
+ // Create SQL query
+ String sql = "SELECT id, firstname, lastname, university, college, school, department, year, regnumber, modules FROM students";
+
+ // Create statement and execute query
+ Statement stmt = conn.createStatement();
+ ResultSet rs = stmt.executeQuery(sql);
+ // Iterate over result set and add rows to table model
+ while (rs.next()) {
+ Object[] row = {
+ rs.getInt("id"),
+ rs.getString("firstname"),
+ rs.getString("lastname"),
+ rs.getString("university"),
+ rs.getString("college"),
+ rs.getString("school"),
+ rs.getString("department"),
+ rs.getString("year"),
+ rs.getString("regnumber"),
+ rs.getString("modules")
+ };
+ model.addRow(row);
+ }
+ // Close resources
+ rs.close();
+ stmt.close();
+ conn.close();
+
+ } catch (SQLException e) {
+ e.printStackTrace();
+ JOptionPane.showMessageDialog(this,
+ "Error loading student data: " + e.getMessage(),
+ "Database Error",
+ JOptionPane.ERROR_MESSAGE);
+ }
+ }
+
+ /**
+ * 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")
+ //
+ private void initComponents() {
+
+ jLabel1 = new javax.swing.JLabel();
+ jScrollPane1 = new javax.swing.JScrollPane();
+ jTable1 = new javax.swing.JTable();
+ addbtn = new javax.swing.JButton();
+
+ setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
+
+ jLabel1.setFont(new java.awt.Font("Space Grotesk Medium", 0, 24)); // NOI18N
+ jLabel1.setText("STUDENTS LIST");
+
+ jTable1.setModel(new javax.swing.table.DefaultTableModel(
+ new Object [][] {
+ {null, null, null, null, null, null, null, null, null, null},
+ {null, null, null, null, null, null, null, null, null, null},
+ {null, null, null, null, null, null, null, null, null, null},
+ {null, null, null, null, null, null, null, null, null, null},
+ {null, null, null, null, null, null, null, null, null, null},
+ {null, null, null, null, null, null, null, null, null, null},
+ {null, null, null, null, null, null, null, null, null, null},
+ {null, null, null, null, null, null, null, null, null, null},
+ {null, null, null, null, null, null, null, null, null, null},
+ {null, null, null, null, null, null, null, null, null, null}
+ },
+ new String [] {
+ "ID", "Firstname", "Lastname", "University", "College", "School", "Department", "Year", "Reg Number", "Modules"
+ }
+ ) {
+ Class[] types = new Class [] {
+ java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
+ };
+
+ public Class getColumnClass(int columnIndex) {
+ return types [columnIndex];
+ }
+ });
+ jScrollPane1.setViewportView(jTable1);
+
+ addbtn.setBackground(new java.awt.Color(51, 102, 255));
+ addbtn.setFont(new java.awt.Font("Space Grotesk Medium", 0, 16)); // NOI18N
+ addbtn.setForeground(new java.awt.Color(255, 255, 255));
+ addbtn.setText("+ ADD NEW STUDENT");
+ addbtn.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ addbtnActionPerformed(evt);
+ }
+ });
+
+ javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
+ getContentPane().setLayout(layout);
+ layout.setHorizontalGroup(
+ layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+ .addGroup(layout.createSequentialGroup()
+ .addGap(48, 48, 48)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+ .addGroup(layout.createSequentialGroup()
+ .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(addbtn))
+ .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 699, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addContainerGap(32, Short.MAX_VALUE))
+ );
+ layout.setVerticalGroup(
+ layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+ .addGroup(layout.createSequentialGroup()
+ .addGap(45, 45, 45)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(addbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addGap(18, 18, 18)
+ .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addContainerGap(155, Short.MAX_VALUE))
+ );
+
+ pack();
+ }//
+
+ // a button to go back to our form
+ private void addbtnActionPerformed(java.awt.event.ActionEvent evt) {
+ StudentForm sdnform =new StudentForm();
+ StudentList.this.setVisible(false);
+ sdnform.setVisible(true);
+ }
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String args[]) {
+ /* Set the Nimbus look and feel */
+ //
+ /* 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(StudentList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+ } catch (InstantiationException ex) {
+ java.util.logging.Logger.getLogger(StudentList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+ } catch (IllegalAccessException ex) {
+ java.util.logging.Logger.getLogger(StudentList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+ } catch (javax.swing.UnsupportedLookAndFeelException ex) {
+ java.util.logging.Logger.getLogger(StudentList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+ }
+ //
+
+ /* Create and display the form */
+ java.awt.EventQueue.invokeLater(new Runnable() {
+ public void run() {
+ new StudentList().setVisible(true);
+ }
+ });
+ }
+
+ // Variables declaration - do not modify
+ private javax.swing.JButton addbtn;
+ private javax.swing.JLabel jLabel1;
+ private javax.swing.JScrollPane jScrollPane1;
+ private javax.swing.JTable jTable1;
+ // End of variables declaration
+}