LAB ACTIVITY 4B
Manipulates JDBC connectivity.
Manipulates JDBC connectivity.
Learning Outcomes
By the end of this lab, students should be able to :
Arranges JDBC for database connectivity by applying the appropriate steps.
Hardware/ Software : Personal Computer, Java Development Kit version X.x.x., NetBeans IDE 12.0
LAB ACTIVITY 4B
Activity Outcome: Student know how to use JDBC
First, we will create a simple Swing interface that includes:
Labels and text fields for "Name" and "IC"
A button labeled "ADD" to insert data
Here’s the basic Java Swing code for the interface:
In this step, we will add an ActionListener to the "ADD" button. This listener will trigger an action when the button is clicked.
We need to:
Capture the input from the text fields.
Call the insertData method to insert data into the database.
Here’s how we can modify the previous code to include the button listener:
With this code, when the "ADD" button is clicked, the inputted name and IC will be captured, and the insertData method will be called with these values.
Next, we will implement the JDBC logic to insert the data into a MySQL database.
Before you start, ensure you:
Have a MySQL database named student_db.
Have a table students in the database with the following structure:
Have added the MySQL JDBC driver (mysql-connector-java.jar) to your project libraries.
Here is the code for the insertData method:
After writing the code:
Enter Name and IC into the text fields.
Click the ADD button.
The inputted data will be inserted into your MySQL database.
Follow the video version below for ADD BUTTON Or click <<THIS LINK>>
Now we will add functionality to UPDATE the student data in the MySQL database based on the "IC" as the unique identifier. Here is how we can achieve this.
First, let’s extend the interface by adding a button for updating the student information.
Here’s the updated interface with an additional UPDATE button:
Now, let’s implement the JDBC Update logic. The updateData method will update the student information in the MySQL database based on the IC number.
The SQL query for updating data would look like this:
UPDATE students SET name = ? WHERE ic = ?;
Here is the code for the updateData method:
Follow the video version below for ADD BUTTON Or click <<THIS LINK>>
Run the application.
Enter a student’s Name and IC.
First, click ADD to insert the new data into the database.
To test the UPDATE functionality, change the Name and click UPDATE. The student information will be updated in the database based on the IC number.
In this section, we will add the DELETE functionality to the Student Information System. The DELETE button will remove a student’s record from the MySQL database based on the "IC" number.
First, let's add a DELETE button to the interface, just like how we added the ADD and UPDATE buttons.
Here’s the updated interface with the new DELETE button:
Now, let's implement the JDBC Delete logic. The deleteData method will delete a student’s record based on their IC number.
The SQL query for deleting a record would look like this:
DELETE FROM students WHERE ic = ?;
Here is the code for the deleteData method
The deleteData method will take the IC input from the text field and use a PreparedStatement to delete the record from the database.
If the record is successfully deleted, it will show a confirmation message using JOptionPane.
Run the application.
Enter a student's IC in the text field.
Click DELETE to remove that student's record from the database.
Follow the following video for UPDATE and DELETE buttons Or click <<THIS LINK>>