0% found this document useful (0 votes)
10 views62 pages

Dbms Miniproject-2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views62 pages

Dbms Miniproject-2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 62

ENTITY RELATIONSHIP DIAGRAM (ERD):

TABLES MADE FROM ERD


NORMALIZATION UPTO 3 NF

1st Normal Form (1NF):


All tables already contain atomic values and no repeating groups. Hence, they are in 1NF.

2nd Normal Form (2NF):


ARMY, NAVY, and AIRFORCE contain partial dependencies on their respective primary
keys.
These tables are merged into a single PERSONNEL table with a BRANCH column to
distinguish between ARMY, NAVY, and AIRFORCE.
- The PERSONNEL table is now fully dependent on its primary key (ID).

3rd Normal Form (3NF):


There are no transitive dependencies in the tables. Although RANK and AGE might relate,
no further normalization is needed unless RANK becomes more complex.
The PERSONNEL table remains unchanged as there are no transitive dependencies.
Final Schema (After 3NF):
1. DEPARTMENT:
DEPT_ID (Primary Key)
DEPT_NAME
2. PERSONNEL:
ID (Primary Key)
NAME
RANK
AGE
DEPT_ID (Foreign Key)
JOINING_DATE
BRANCH (values: 'ARMY', 'NAVY', 'AIRFORCE')
CREATE TABLE COMMANDS FOR NORMALIZED

CREATE TABLE DEPARTMENT (


DEPT_ID INT PRIMARY KEY,
DEPT_NAME VARCHAR(100) NOT NULL
);

CREATE TABLE PERSONNEL (


ID INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(100) NOT NULL,
RANK VARCHAR(50) NOT NULL,
AGE INT NOT NULL,
DEPT_ID INT,
JOINING_DATE DATE,
BRANCH VARCHAR(20) CHECK (BRANCH IN ('ARMY', 'NAVY', 'AIRFORCE')),
FOREIGN KEY (DEPT_ID) REFERENCES DEPARTMENT(DEPT_ID)
);
20 queries solved on MySQl in the form (Question, Query,Output) on the
above tables covering
1. Retrieve all personnel aged between 25 and 40.
mysql> SELECT * FROM PERSONNEL WHERE AGE BETWEEN 25 AND 40;
-> //
+----+----------------------+-------------------+------+---------+--------------+----------+
| ID | NAME | RANK | AGE | DEPT_ID | JOINING_DATE | BRANCH |
+----+----------------------+-------------------+------+---------+--------------+----------+
| 1 | Vikram Batra | CAPTAIN | 29 | 101 | 1997-06-06 | Army |
| 4 | Abhinandan Varthaman | Wing Commander | 35 | 102 | 2004-01-15 | Airforce |
| 5 | Gunjan Saxena | Flight Lieutenant | 30 | 104 | 2006-09-25 | Airforce |
| 8 | Rakesh Sharma | Squadron Leader | 38 | 102 | 1984-09-01 | Airforce |
| 9 | Harita Kaur Deol | Flight Lieutenant | 26 | 104 | 1994-05-15 | Airforce |
+----+----------------------+-------------------+------+---------+--------------+----------+
5 rows in set (0.00 sec)

2. Find all personnel with names that having second letter as 'A'.
mysql> SELECT * FROM PERSONNEL
-> WHERE NAME LIKE '_A%';
+----+------------------+-------------------+------+---------+--------------+----------+
| ID | NAME | RANK | AGE | DEPT_ID | JOINING_DATE | BRANCH |
+----+------------------+-------------------+------+---------+--------------+----------+
| 3 | Rajendra Singh | Admiral | 50 | 103 | 1990-07-20 | Navy |
| 6 | Karambir Singh | Vice Admiral | 48 | 103 | 1995-11-18 | Navy |
| 7 | Sam Manekshaw | Field Marshal | 50 | 101 | 1973-03-20 | Army |
| 8 | Rakesh Sharma | Squadron Leader | 38 | 102 | 1984-09-01 | Airforce |
| 9 | Harita Kaur Deol | Flight Lieutenant | 26 | 104 | 1994-05-15 | Airforce |
+----+------------------+-------------------+------+---------+--------------+----------+
5 rows in set (0.00 sec)

3. List personnel who joined after 2000-01-01.


mysql> SELECT * FROM PERSONNEL
-> WHERE JOINING_DATE > '2000-01-01';
+----+----------------------+-------------------+------+---------+--------------+----------+
| ID | NAME | RANK | AGE | DEPT_ID | JOINING_DATE | BRANCH |
+----+----------------------+-------------------+------+---------+--------------+----------+
| 4 | Abhinandan Varthaman | Wing Commander | 35 | 102 | 2004-01-15 | Airforce |
| 5 | Gunjan Saxena | Flight Lieutenant | 30 | 104 | 2006-09-25 | Airforce |
| 10 | Krishna Kant | Commander | 42 | 103 | 2003-12-10 | Navy |
+----+----------------------+-------------------+------+---------+--------------+----------+
3 rows in set (0.00 sec)

4. Display all Army personnel, sorted by their rank in descending order.


mysql> SELECT * FROM ARMY
-> ORDER BY `RANK` DESC;
+------+----------------+------------+------+---------+--------------+
| A_ID | A_NAME | RANK | AGE | DEPT_ID | JOINING_DATE |
+------+----------------+------------+------+---------+--------------+
| 4 | Pooja Reddy | Major | 38 | 101 | 2010-07-21 |
| 6 | Neha Thakur | Lieutenant | 28 | 101 | 2020-04-04 |
| 3 | Vikram Chauhan | Colonel | 45 | 101 | 2005-09-18 |
| 1 | Riya | Captain | 20 | 101 | 2009-09-09 |
| 2 | Shruti | Captain | 30 | 103 | 2010-10-10 |
| 5 | Ramesh Iyer | Captain | 35 | 102 | 2013-01-12 |
| 7 | Akhil Nair | Brigadier | 50 | 104 | 2000-02-14 |
+------+----------------+------------+------+---------+--------------+
7 rows in set (0.00 sec)

5. Count the number of personnel in each department.


mysql> SELECT DEPT_ID, COUNT(*) AS PersonnelCount
-> FROM PERSONNEL
-> GROUP BY DEPT_ID;
+---------+----------------+
| DEPT_ID | PersonnelCount |
+---------+----------------+
| 101 | 2|
| 102 | 3|
| 103 | 3|
| 104 | 2|
+---------+----------------+
4 rows in set (0.00 sec)

6. Generate a Cartesian product of NAVY and DEPARTMENT tables.


mysql> SELECT *
-> FROM NAVY, DEPARTMENT;
+------+----------------+-------------------+------+---------+-----------------+---------
+--------------------------+
| N_ID | N_NAME | RANK | AGE | DEPT_ID | DATE_OF_JOINING |
DEPT_ID | DEPT_NAME |
+------+----------------+-------------------+------+---------+-----------------+---------
+--------------------------+
| 6 | Meera Kulkarni | Navigator | 30 | 102 | 2018-06-17 | 101 | Army
|
| 5 | Amit Singh | Submarine Officer | 32 | 102 | 2015-08-30 | 101 | Army
|
| 4 | Sneha Patil | Captain | 38 | 102 | 2011-11-11 | 101 | Army |
| 3 | Aditya Varma | Lieutenant | 35 | 102 | 2012-03-22 | 101 | Army
|
| 2 | Rajesh Sharma | Commander | 40 | 102 | 2009-05-14 | 101 | Army
|
| 1 | SHRAVANI | CAPTAIN | 35 | 102 | 2010-10-10 | 101 | Army
|
| 6 | Meera Kulkarni | Navigator | 30 | 102 | 2018-06-17 | 102 | Navy
|
| 5 | Amit Singh | Submarine Officer | 32 | 102 | 2015-08-30 | 102 | Navy
|
| 4 | Sneha Patil | Captain | 38 | 102 | 2011-11-11 | 102 | Navy |
| 3 | Aditya Varma | Lieutenant | 35 | 102 | 2012-03-22 | 102 | Navy
|
| 2 | Rajesh Sharma | Commander | 40 | 102 | 2009-05-14 | 102 | Navy
|
| 1 | SHRAVANI | CAPTAIN | 35 | 102 | 2010-10-10 | 102 | Navy
|
| 6 | Meera Kulkarni | Navigator | 30 | 102 | 2018-06-17 | 103 | Airforce
|
| 5 | Amit Singh | Submarine Officer | 32 | 102 | 2015-08-30 | 103 | Airforce
|
| 4 | Sneha Patil | Captain | 38 | 102 | 2011-11-11 | 103 | Airforce |
| 3 | Aditya Varma | Lieutenant | 35 | 102 | 2012-03-22 | 103 | Airforce
|
| 2 | Rajesh Sharma | Commander | 40 | 102 | 2009-05-14 | 103 | Airforce
|
| 1 | SHRAVANI | CAPTAIN | 35 | 102 | 2010-10-10 | 103 | Airforce
|
| 6 | Meera Kulkarni | Navigator | 30 | 102 | 2018-06-17 | 104 | Infantry
|
| 5 | Amit Singh | Submarine Officer | 32 | 102 | 2015-08-30 | 104 | Infantry
|
| 4 | Sneha Patil | Captain | 38 | 102 | 2011-11-11 | 104 | Infantry |
| 3 | Aditya Varma | Lieutenant | 35 | 102 | 2012-03-22 | 104 | Infantry
|
| 2 | Rajesh Sharma | Commander | 40 | 102 | 2009-05-14 | 104 | Infantry
|
| 1 | SHRAVANI | CAPTAIN | 35 | 102 | 2010-10-10 | 104 | Infantry
|
| 6 | Meera Kulkarni | Navigator | 30 | 102 | 2018-06-17 | 105 | Artillery
|
| 5 | Amit Singh | Submarine Officer | 32 | 102 | 2015-08-30 | 105 | Artillery
|
| 4 | Sneha Patil | Captain | 38 | 102 | 2011-11-11 | 105 | Artillery |
| 3 | Aditya Varma | Lieutenant | 35 | 102 | 2012-03-22 | 105 | Artillery
|
| 2 | Rajesh Sharma | Commander | 40 | 102 | 2009-05-14 | 105 | Artillery
|
| 1 | SHRAVANI | CAPTAIN | 35 | 102 | 2010-10-10 | 105 | Artillery
|
| 6 | Meera Kulkarni | Navigator | 30 | 102 | 2018-06-17 | 106 | Naval
Operations |
| 5 | Amit Singh | Submarine Officer | 32 | 102 | 2015-08-30 | 106 | Naval
Operations |
| 4 | Sneha Patil | Captain | 38 | 102 | 2011-11-11 | 106 | Naval Operations
|
| 3 | Aditya Varma | Lieutenant | 35 | 102 | 2012-03-22 | 106 | Naval
Operations |
| 2 | Rajesh Sharma | Commander | 40 | 102 | 2009-05-14 | 106 | Naval
Operations |
| 1 | SHRAVANI | CAPTAIN | 35 | 102 | 2010-10-10 | 106 | Naval
Operations |
| 6 | Meera Kulkarni | Navigator | 30 | 102 | 2018-06-17 | 107 | Air Force
Command |
| 5 | Amit Singh | Submarine Officer | 32 | 102 | 2015-08-30 | 107 | Air Force
Command |
| 4 | Sneha Patil | Captain | 38 | 102 | 2011-11-11 | 107 | Air Force
Command |
| 3 | Aditya Varma | Lieutenant | 35 | 102 | 2012-03-22 | 107 | Air Force
Command |
| 2 | Rajesh Sharma | Commander | 40 | 102 | 2009-05-14 | 107 | Air Force
Command |
| 1 | SHRAVANI | CAPTAIN | 35 | 102 | 2010-10-10 | 107 | Air Force
Command |
| 6 | Meera Kulkarni | Navigator | 30 | 102 | 2018-06-17 | 108 | Research and
Development |
| 5 | Amit Singh | Submarine Officer | 32 | 102 | 2015-08-30 | 108 | Research and
Development |
| 4 | Sneha Patil | Captain | 38 | 102 | 2011-11-11 | 108 | Research and
Development |
| 3 | Aditya Varma | Lieutenant | 35 | 102 | 2012-03-22 | 108 | Research and
Development |
| 2 | Rajesh Sharma | Commander | 40 | 102 | 2009-05-14 | 108 | Research and
Development |
| 1 | SHRAVANI | CAPTAIN | 35 | 102 | 2010-10-10 | 108 | Research and
Development |
+------+----------------+-------------------+------+---------+-----------------+---------
+--------------------------+
48 rows in set (0.00 sec)

7. Retrieve personnel details along with their department names.


mysql> SELECT P.ID, P.NAME, P.RANK, D.DEPT_NAME
-> FROM PERSONNEL P
-> INNER JOIN DEPARTMENT D ON P.DEPT_ID = D.DEPT_ID;
+----+----------------------+-------------------+-----------+
| ID | NAME | RANK | DEPT_NAME |
+----+----------------------+-------------------+-----------+
| 1 | Vikram Batra | Captain | Army |
| 7 | Sam Manekshaw | Field Marshal | Army |
| 2 | Arjan Singh | Marshal | Navy |
| 4 | Abhinandan Varthaman | Wing Commander | Navy |
| 8 | Rakesh Sharma | Squadron Leader | Navy |
| 3 | Rajendra Singh | Admiral | Airforce |
| 6 | Karambir Singh | Vice Admiral | Airforce |
| 10 | Krishna Kant | Commander | Airforce |
| 5 | Gunjan Saxena | Flight Lieutenant | Infantry |
| 9 | Harita Kaur Deol | Flight Lieutenant | Infantry |
+----+----------------------+-------------------+-----------+
10 rows in set (0.00 sec)

8. List all personnel and their departments, including personnel with no


department assigned.
mysql> SELECT P.ID, P.NAME, D.DEPT_NAME
-> FROM PERSONNEL P
-> LEFT JOIN DEPARTMENT D ON P.DEPT_ID = D.DEPT_ID;
+----+----------------------+-----------+
| ID | NAME | DEPT_NAME |
+----+----------------------+-----------+
| 1 | Vikram Batra | Army |
| 2 | Arjan Singh | Navy |
| 3 | Rajendra Singh | Airforce |
| 4 | Abhinandan Varthaman | Navy |
| 5 | Gunjan Saxena | Infantry |
| 6 | Karambir Singh | Airforce |
| 7 | Sam Manekshaw | Army |
| 8 | Rakesh Sharma | Navy |
| 9 | Harita Kaur Deol | Infantry |
| 10 | Krishna Kant | Airforce |
+----+----------------------+-----------+
10 rows in set (0.00 sec)

9. List all departments and the personnel assigned, including departments


with no personnel.
mysql> SELECT D.DEPT_NAME, P.NAME
-> FROM DEPARTMENT D
-> RIGHT JOIN PERSONNEL P ON D.DEPT_ID = P.DEPT_ID;
+-----------+----------------------+
| DEPT_NAME | NAME |
+-----------+----------------------+
| Army | Vikram Batra |
| Navy | Arjan Singh |
| Airforce | Rajendra Singh |
| Navy | Abhinandan Varthaman |
| Infantry | Gunjan Saxena |
| Airforce | Karambir Singh |
| Army | Sam Manekshaw |
| Navy | Rakesh Sharma |
| Infantry | Harita Kaur Deol |
| Airforce | Krishna Kant |
+-----------+----------------------+
10 rows in set (0.00 sec)

10. Find personnel who belong to the department with the highest number
of members.
mysql> SELECT *
-> FROM PERSONNEL
-> WHERE DEPT_ID = (
-> SELECT DEPT_ID
-> FROM PERSONNEL
-> GROUP BY DEPT_ID
-> ORDER BY COUNT(*) DESC
-> LIMIT 1
-> );
+----+----------------------+-----------------+------+---------+--------------+----------+
| ID | NAME | RANK | AGE | DEPT_ID | JOINING_DATE | BRANCH |
+----+----------------------+-----------------+------+---------+--------------+----------+
| 2 | Arjan Singh | Marshal | 45 | 102 | 1985-04-14 | Airforce |
| 4 | Abhinandan Varthaman | Wing Commander | 35 | 102 | 2004-01-15 | Airforce |
| 8 | Rakesh Sharma | Squadron Leader | 38 | 102 | 1984-09-01 | Airforce |
+----+----------------------+-----------------+------+---------+--------------+----------+
3 rows in set (0.00 sec)

11. Create a view to list all personnel and their department names.
mysql> CREATE VIEW PersonnelWithDept AS
-> SELECT P.ID, P.NAME, P.RANK, D.DEPT_NAME
-> FROM PERSONNEL P
-> INNER JOIN DEPARTMENT D ON P.DEPT_ID = D.DEPT_ID;
Query OK, 0 rows affected (0.01 sec)

mysql> SELECT * FROM PERSONNELWITHDEPT;


+----+----------------------+-------------------+-----------+
| ID | NAME | RANK | DEPT_NAME |
+----+----------------------+-------------------+-----------+
| 1 | Vikram Batra | Captain | Army |
| 2 | Arjan Singh | Marshal | Navy |
| 3 | Rajendra Singh | Admiral | Airforce |
| 4 | Abhinandan Varthaman | Wing Commander | Navy |
| 5 | Gunjan Saxena | Flight Lieutenant | Infantry |
| 6 | Karambir Singh | Vice Admiral | Airforce |
| 7 | Sam Manekshaw | Field Marshal | Army |
| 8 | Rakesh Sharma | Squadron Leader | Navy |
| 9 | Harita Kaur Deol | Flight Lieutenant | Infantry |
| 10 | Krishna Kant | Commander | Airforce |
+----+----------------------+-------------------+-----------+
10 rows in set (0.00 sec)

12. Create an index on the RANK column in the PERSONNEL table for
faster querying.
mysql> CREATE INDEX idx_rank ON PERSONNEL(`RANK`);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

13. Create a trigger to automatically log deletions from the NAVY table.
mysql> CREATE TRIGGER log_navy_deletion
-> AFTER DELETE ON NAVY
-> FOR EACH ROW
-> INSERT INTO DELETION_LOG (TABLE_NAME, RECORD_ID, DELETED_AT)
-> VALUES ('NAVY', OLD.N_ID, NOW());
Query OK, 0 rows affected (0.01 sec)

mysql> SELECT * FROM PERSONNEL;


+----+----------------------+-------------------+------+---------+--------------+----------+
| ID | NAME | RANK | AGE | DEPT_ID | JOINING_DATE | BRANCH |
+----+----------------------+-------------------+------+---------+--------------+----------+
| 1 | Vikram Batra | Captain | 29 | 101 | 1997-06-06 | Army |
| 2 | Arjan Singh | Marshal | 45 | 102 | 1985-04-14 | Airforce |
| 3 | Rajendra Singh | Admiral | 50 | 103 | 1990-07-20 | Navy |
| 4 | Abhinandan Varthaman | Wing Commander | 35 | 102 | 2004-01-15 | Airforce |
| 5 | Gunjan Saxena | Flight Lieutenant | 30 | 104 | 2006-09-25 | Airforce |
| 6 | Karambir Singh | Vice Admiral | 48 | 103 | 1995-11-18 | Navy |
| 7 | Sam Manekshaw | Field Marshal | 50 | 101 | 1973-03-20 | Army |
| 8 | Rakesh Sharma | Squadron Leader | 38 | 102 | 1984-09-01 | Airforce |
| 9 | Harita Kaur Deol | Flight Lieutenant | 26 | 104 | 1994-05-15 | Airforce |
| 10 | Krishna Kant | Commander | 42 | 103 | 2003-12-10 | Navy |
+----+----------------------+-------------------+------+---------+--------------+----------+
10 rows in set (0.00 sec)

mysql> DELETE FROM PERSONNEL WHERE ID = 10;


Query OK, 1 row affected (0.01 sec)

14. Create a procedure to update the rank of a personnel by their ID.


mysql> DELIMITER //
mysql> CREATE PROCEDURE UpdateRank(IN personnelId INT, IN newRank
VARCHAR(20))
-> BEGIN
-> UPDATE PERSONNEL
-> SET `RANK` = newRank
-> WHERE ID = personnelId;
-> END //
Query OK, 0 rows affected (0.01 sec)

mysql> CALL UpdateRank(1, 'CAPTAIN');


-> //
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM PERSONNEL WHERE ID = 1;
-> //
+----+--------------+---------+------+---------+--------------+--------+
| ID | NAME | RANK | AGE | DEPT_ID | JOINING_DATE | BRANCH |
+----+--------------+---------+------+---------+--------------+--------+
| 1 | Vikram Batra | CAPTAIN | 29 | 101 | 1997-06-06 | Army |
+----+--------------+---------+------+---------+--------------+--------+
1 row in set (0.00 sec)

15. Retrieve all AIRFORCE personnel working in the 'Wing Commander'


department.
mysql> SELECT AF.F_ID, AF.F_NAME, D.DEPT_NAME
-> FROM AIRFORCE AF
-> INNER JOIN DEPARTMENT D ON AF.DEPT_ID = D.DEPT_ID
-> WHERE D.DEPT_NAME = 'WING COMMANDER';
-> //
Query OK, 1 row affected (0.01 sec)

16. Retrieve the top 5 oldest personnel in the Army.


mysql> SELECT *
-> FROM ARMY
-> ORDER BY AGE DESC
-> LIMIT 5;
-> //
+------+----------------+-----------+------+---------+--------------+
| A_ID | A_NAME | RANK | AGE | DEPT_ID | JOINING_DATE |
+------+----------------+-----------+------+---------+--------------+
| 7 | Akhil Nair | Brigadier | 50 | 104 | 2000-02-14 |
| 3 | Vikram Chauhan | Colonel | 45 | 101 | 2005-09-18 |
| 4 | Pooja Reddy | Major | 38 | 101 | 2010-07-21 |
| 5 | Ramesh Iyer | Captain | 35 | 102 | 2013-01-12 |
| 2 | Shruti | Captain | 30 | 103 | 2010-10-10 |
+------+----------------+-----------+------+---------+--------------+
5 rows in set (0.00 sec)

17. Find personnel with 'AIRFORCE' anywhere in their BRANCH.


mysql> SELECT *
-> FROM PERSONNEL
-> WHERE BRANCH LIKE '%AIRFORCE%';
-> //
+----+----------------------+-------------------+------+---------+--------------+----------+
| ID | NAME | RANK | AGE | DEPT_ID | JOINING_DATE | BRANCH |
+----+----------------------+-------------------+------+---------+--------------+----------+
| 2 | Arjan Singh | Marshal | 45 | 102 | 1985-04-14 | Airforce |
| 4 | Abhinandan Varthaman | Wing Commander | 35 | 102 | 2004-01-15 | Airforce |
| 5 | Gunjan Saxena | Flight Lieutenant | 30 | 104 | 2006-09-25 | Airforce |
| 8 | Rakesh Sharma | Squadron Leader | 38 | 102 | 1984-09-01 | Airforce |
| 9 | Harita Kaur Deol | Flight Lieutenant | 26 | 104 | 1994-05-15 | Airforce |
+----+----------------------+-------------------+------+---------+--------------+----------+
5 rows in set (0.00 sec)

18. List all departments that currently have no personnel assigned.


mysql> SELECT DEPT_NAME
-> FROM DEPARTMENT D
-> LEFT JOIN PERSONNEL P ON D.DEPT_ID = P.DEPT_ID
-> WHERE P.ID IS NULL;
-> //
+--------------------------+
| DEPT_NAME |
+--------------------------+
| Artillery |
| Naval Operations |
| Air Force Command |
| Research and Development |
| WING COMMANDER |
+--------------------------+
5 rows in set (0.00 sec)

19. Count personnel in each branch (NAVY, ARMY, AIRFORCE).


mysql> SELECT 'NAVY' AS Branch, COUNT(*) AS PersonnelCount FROM NAVY
-> UNION
-> SELECT 'ARMY' AS Branch, COUNT(*) AS PersonnelCount FROM ARMY
-> UNION
-> SELECT 'AIRFORCE' AS Branch, COUNT(*) AS PersonnelCount FROM
AIRFORCE;
-> //
+----------+----------------+
| Branch | PersonnelCount |
+----------+----------------+
| NAVY | 6|
| ARMY | 7|
| AIRFORCE | 6|
+----------+----------------+
3 rows in set (0.01 sec)

20. List all NAVY personnel along with their details from the PERSONNEL
table.
mysql> SELECT N.N_ID, N.N_NAME, P.AGE, P.JOINING_DATE
-> FROM NAVY N
-> INNER JOIN PERSONNEL P ON N.N_ID = P.ID;
-> //
+------+----------------+------+--------------+
| N_ID | N_NAME | AGE | JOINING_DATE |
+------+----------------+------+--------------+
| 1 | SHRAVANI | 29 | 1997-06-06 |
| 2 | Rajesh Sharma | 45 | 1985-04-14 |
| 3 | Aditya Varma | 50 | 1990-07-20 |
| 4 | Sneha Patil | 35 | 2004-01-15 |
| 5 | Amit Singh | 30 | 2006-09-25 |
| 6 | Meera Kulkarni | 48 | 1995-11-18 |
+------+----------------+------+--------------+

CODE:
package DBMS_MiniProject;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class MainPage {
private static final String DB_URL = "jdbc:mysql://localhost:3307/DefensePersonnal"; //
Update with your DB URL
private static final String USER = "root"; // Update with your DB username
private static final String PASSWORD = "nalam@4"; // Update with your DB password
// Method to establish connection with database
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(DB_URL, USER, PASSWORD);
}
// Main Page UI
public static class Main extends Frame {
Main() {

setTitle("Indian Defense System");


setSize(500, 500);
setLayout(new FlowLayout());
Button deptButton = new Button("DEPARTMENT");
Button armyButton = new Button("Army Personnel");
Button navyButton = new Button("Navy Personnel");
Button airforceButton = new Button("Airforce Personnel");
deptButton.setSize(100,100);
armyButton.setSize(100, 100);// Width 200, Height 50
navyButton.setSize(200, 50);
airforceButton.setSize(200, 50);
deptButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

new DepartmentPage();
}
});
armyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

new ArmyPage();
}
});
navyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new NavyPage();
}
});
airforceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new AirforcePage();
}
});
add(deptButton);
add(armyButton);
add(navyButton);
add(airforceButton);
setVisible(true);
}

}
public static class DepartmentPage extends Frame {

DepartmentPage() {
setTitle("Department Details");
setSize(800, 800);
setLayout(new BorderLayout());
final TextArea textArea = new TextArea();
textArea.setEditable(false);
add(textArea, BorderLayout.CENTER);
Panel panel = new Panel();
panel.setLayout(new FlowLayout());

Button fetchButton = new Button("Fetch Data");


Button insertButton = new Button("Insert");
Button deleteButton = new Button("Delete");
Button updateButton = new Button("Update");

fetchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
textArea.setText(getDepartmentData());
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
insertButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DepartmentInsertPage();
}
});

deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DepartmentDeletePage();
}
});

updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DepartmentUpdatePage();
}
});

panel.add(fetchButton);
panel.add(insertButton);
panel.add(deleteButton);
panel.add(updateButton);
add(panel, BorderLayout.SOUTH);
setVisible(true);
}

// Fetch Department Data from DB


private String getDepartmentData() throws SQLException {
StringBuilder data = new StringBuilder();
String query = "SELECT dept_id, dept_name FROM DEPARTMENT";
Connection connection = getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
data.append(rs.getInt("dept_id")).append(" | ")
.append(rs.getString("dept_name")).append("\n");
}

return data.toString();
}

private void DepartmentInsertPage() {


setTitle("Insert Department Details");
setSize(800, 800);
setLayout(new FlowLayout());

Label idLabel = new Label("Department ID:");


final TextField idField = new TextField(20);
Label nameLabel = new Label("Department Name:");
final TextField nameField = new TextField(20);
Button insertButton = new Button("Insert");

insertButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int id = Integer.parseInt(idField.getText());
String name = nameField.getText();
insertDepartment(id, name);
} catch (SQLException | NumberFormatException ex) {
ex.printStackTrace();
}
}
});

add(idLabel);
add(idField);
add(nameLabel);
add(nameField);
add(insertButton);

setVisible(true);
}

// Insert Department Details


private void insertDepartment(int id, String name) throws SQLException {
String query = "INSERT INTO DEPARTMENT (dept_id, dept_name) VALUES
(?, ?)";
Connection connection = getConnection();
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.executeUpdate();
System.out.println("Department inserted successfully!");
}

private void DepartmentDeletePage() {


setTitle("Delete Department");
setSize(800, 800);
setLayout(new FlowLayout());
Label idLabel = new Label("Enter Department ID to Delete:");
final TextField idField = new TextField(20);
Button deleteButton = new Button("Delete");

deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int id = Integer.parseInt(idField.getText());
deleteDepartment(id);
} catch (SQLException | NumberFormatException ex) {
ex.printStackTrace();
}
}
});

add(idLabel);
add(idField);
add(deleteButton);

setVisible(true);
}

// Delete Department
private void deleteDepartment(int id) throws SQLException {
String query = "DELETE FROM DEPARTMENT WHERE dept_id = ?";
Connection connection = getConnection();
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setInt(1, id);
pstmt.executeUpdate();
System.out.println("Department deleted successfully!");
}

private void DepartmentUpdatePage() {


setTitle("Update Department Details");
setSize(800, 800);
setLayout(new FlowLayout());

Label idLabel = new Label("Department ID:");


final TextField idField = new TextField(20);
Label nameLabel = new Label("Department Name:");
final TextField nameField = new TextField(20);
Button updateButton = new Button("Update");

updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int id = Integer.parseInt(idField.getText());
String name = nameField.getText();
updateDepartment(id, name);
} catch (SQLException | NumberFormatException ex) {
ex.printStackTrace();
}
}
});

add(idLabel);
add(idField);
add(nameLabel);
add(nameField);
add(updateButton);
setVisible(true);
}

// Update Department Details


private void updateDepartment(int id, String name) throws SQLException {
String query = "UPDATE DEPARTMENT SET dept_name = ? WHERE dept_id = ?";
Connection connection = getConnection();
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, name);
pstmt.setInt(2, id);
pstmt.executeUpdate();
System.out.println("Department updated successfully!");
}

}
// Army Personnel Page
public static class ArmyPage extends Frame {

ArmyPage() {
setTitle("Army Personnel");
setSize(800, 800);
setLayout(new BorderLayout());
final TextArea textArea = new TextArea();
textArea.setEditable(false);
add(textArea, BorderLayout.CENTER);
Panel panel = new Panel();
panel.setLayout(new FlowLayout());

Button fetchButton = new Button("Fetch Data");


Button insertButton = new Button("Insert");
Button deleteButton = new Button("Delete");
Button updateButton = new Button("Update");

fetchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Fetch data and set it to the text area
textArea.setText(getArmyData());
} catch (SQLException ex) {
ex.printStackTrace(); // Print the exception trace to debug
textArea.setText("Error fetching data: " + ex.getMessage()); // Show error in
the text area
}
}
});

insertButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Open the Insert page (ensure the ArmyInsertPage is correctly implemented)
ArmyInsertPage();
}
});

deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Open the Delete page (ensure the ArmyDeletePage is correctly implemented)
ArmyDeletePage();
}
});
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Open the Update page (ensure the ArmyUpdatePage is correctly implemented)
ArmyUpdatePage();
}
});

panel.add(fetchButton);
panel.add(insertButton);
panel.add(deleteButton);
panel.add(updateButton);
add(panel, BorderLayout.SOUTH);
setVisible(true);
}

// Fetch Army Personnel Data from DB


private String getArmyData() throws SQLException {
StringBuilder data = new StringBuilder();
String query = "SELECT A_ID, A_NAME, RANK, AGE,
JOINING_DATE,DEPT_ID FROM ARMY";
Connection connection = getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
data.append(rs.getInt("A_ID")).append(" | ")
.append(rs.getString("A_NAME")).append(" | ")
.append(rs.getString("RANK")).append(" | ")
.append(rs.getInt("AGE")).append("|")
.append(rs.getDate("JOINING_DATE")).append("|")
.append(rs.getInt("DEPT_ID")).append("\n");
}
return data.toString();
}

private void ArmyInsertPage() {


setTitle("Insert Army Personnel");
setSize(800, 800);
setLayout(new FlowLayout());

// Add fields for inserting personnel


Label idLabel = new Label("A_ID:");
final TextField idField = new TextField(20);
Label nameLabel = new Label("A_Name:");
final TextField nameField = new TextField(20);
Label rankLabel = new Label("Rank:");
final TextField rankField = new TextField(20);
Label ageLabel = new Label("Age:");
final TextField ageField = new TextField(5);
Label dateLabel = new Label("Date of Joining (YYYY-MM-DD):");
final TextField dateField = new TextField(10);
Label deptidLabel = new Label("Dept ID:");
final TextField deptidField = new TextField(10);
Button insertButton = new Button("Insert");

insertButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int id = Integer.parseInt(idField.getText() );
String name = nameField.getText();
String rank = rankField.getText();
int age =Integer.parseInt(ageField.getText() );
String dateStr = dateField.getText(); // Get date as a string

// Convert the date string into a Date object


java.sql.Date joiningDate = java.sql.Date.valueOf(dateStr);
int deptid = Integer.parseInt(deptidField.getText());

// Call method to insert data into ARMY table


insertArmyPersonnel(id,name, rank, age, joiningDate, deptid);
} catch (NumberFormatException nfe) {
System.out.println("Invalid age input");
} catch (SQLException ex) {
ex.printStackTrace();
} catch (IllegalArgumentException ex) {
System.out.println("Invalid date format. Please use YYYY-MM-DD.");
}
}
});

// Add the components to the frame


add(idLabel);
add(idField);
add(nameLabel);
add(nameField);
add(rankLabel);
add(rankField);
add(ageLabel);
add(ageField);
add(dateLabel);
add(dateField);
add(deptidLabel);
add(deptidField);
add(insertButton);

setVisible(true);
}
private void insertArmyPersonnel(int aId, String name, String rank, int age,
java.sql.Date joiningDate, int deptid) throws SQLException {
String query = "INSERT INTO ARMY (A_ID, A_NAME, RANK, AGE,
JOINING_DATE, DEPT_ID) VALUES (?, ?, ?, ?, ?, ?)";
Connection connection = getConnection();
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setInt(1, aId); // Pass the A_ID here
pstmt.setString(2, name);
pstmt.setString(3, rank);
pstmt.setInt(4, age);
pstmt.setDate(5, joiningDate);
pstmt.setInt(6, deptid);
pstmt.executeUpdate();
System.out.println("Personnel inserted successfully!");
}

private void ArmyDeletePage() {


setTitle("Delete Army Personnel");
setSize(800, 800);
setLayout(new FlowLayout());

Label nameLabel = new Label("Name of Personnel to Delete:");


final TextField nameField = new TextField(20);
Button deleteButton = new Button("Delete");

deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();

if (!name.isEmpty()) {
try {
deleteArmyPersonnel(name);
showMessage("Personnel deleted successfully!");
} catch (SQLException ex) {
ex.printStackTrace();
showMessage("Error deleting personnel: " + ex.getMessage());
}
} else {
showMessage("Please enter a valid name.");
}
}

private void showMessage(String string) {


// Add logic to display the message in the GUI (optional)
}
});

add(nameLabel);
add(nameField);
add(deleteButton);

setVisible(true);
}

// Delete Army Personnel


private static void deleteArmyPersonnel(String name) throws SQLException {
String query = "DELETE FROM ARMY WHERE A_NAME = ?";
Connection connection = getConnection();
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, name);
pstmt.executeUpdate();
System.out.println("Personnel deleted successfully!");
}

private void ArmyUpdatePage() {


setTitle("Update Army Personnel");
setSize(800, 800);
setLayout(new FlowLayout());

Label nameLabel = new Label("Name:");


final TextField nameField = new TextField(20);
Label rankLabel = new Label("Rank:");
final TextField rankField = new TextField(20);
Label ageLabel = new Label("Age:");
final TextField ageField = new TextField(5);
Label dateLabel = new Label("Joining Date:");
final TextField dateField = new TextField(5);
Label deptidLabel = new Label("Dept ID:");
final TextField deptidField = new TextField(5); // Added deptId field
Button updateButton = new Button("Update");

updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String name = nameField.getText();
String rank = rankField.getText();
int age = Integer.parseInt(ageField.getText());
String dateStr = dateField.getText(); // Get date as a string
int deptId = Integer.parseInt(deptidField.getText()); // Get deptId as integer

// Convert the date string into a Date object


java.sql.Date joiningDate = java.sql.Date.valueOf(dateStr);

updateArmyPersonnel(name, rank, age, joiningDate, deptId);


} catch (SQLException ex) {
ex.printStackTrace();
} catch (IllegalArgumentException ex) {
System.out.println("Invalid date format. Please use YYYY-MM-DD.");
}
}
});

add(nameLabel);
add(nameField);
add(rankLabel);
add(rankField);
add(ageLabel);
add(ageField);
add(dateLabel);
add(dateField);
add(deptidLabel);
add(deptidField);
add(updateButton);

setVisible(true);
}
// Update Army Personnel (with DEPT_ID)
private void updateArmyPersonnel(String name, String rank, int age, java.sql.Date
joiningDate, int deptId) throws SQLException {
String query = "UPDATE ARMY SET RANK = ?, AGE = ?, JOINING_DATE = ?,
DEPT_ID = ? WHERE A_NAME = ?";
Connection connection = getConnection();
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, rank);
pstmt.setInt(2, age);
pstmt.setDate(3, joiningDate);
pstmt.setInt(4, deptId); // Set DEPT_ID in the query
pstmt.setString(5, name);
pstmt.executeUpdate();
System.out.println("Personnel updated successfully!");
}
}
// Navy Personnel Page
public static class NavyPage extends Frame {

NavyPage() {
setTitle("Navy Personnel");
setSize(800, 800);
setLayout(new BorderLayout());
final TextArea textArea = new TextArea();
textArea.setEditable(false);
add(textArea, BorderLayout.CENTER);
Panel panel = new Panel();
panel.setLayout(new FlowLayout());

Button fetchButton = new Button("Fetch Data");


Button insertButton = new Button("Insert");
Button deleteButton = new Button("Delete");
Button updateButton = new Button("Update");

fetchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Fetch data and set it to the text area
textArea.setText(getNavyData());
} catch (SQLException ex) {
ex.printStackTrace(); // Print the exception trace to debug
textArea.setText("Error fetching data: " + ex.getMessage()); // Show error in
the text area
}
}
});

insertButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Open the Insert page (ensure the NavyInsertPage is correctly implemented)
NavyInsertPage();
}
});

deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Open the Delete page (ensure the NavyDeletePage is correctly implemented)
NavyDeletePage();
}
});
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Open the Update page (ensure the NavyUpdatePage is correctly implemented)
NavyUpdatePage();
}
});

panel.add(fetchButton);
panel.add(insertButton);
panel.add(deleteButton);
panel.add(updateButton);
add(panel, BorderLayout.SOUTH);
setVisible(true);
}

// Fetch Navy Personnel Data from DB


private String getNavyData() throws SQLException {
StringBuilder data = new StringBuilder();
String query = "SELECT N_ID, N_NAME, RANK, AGE, DATE_OF_JOINING,
DEPT_ID FROM NAVY";
Connection connection = getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
data.append(rs.getInt("N_ID")).append(" | ")
.append(rs.getString("N_NAME")).append(" | ")
.append(rs.getString("RANK")).append(" | ")
.append(rs.getInt("AGE")).append(" | ")
.append(rs.getDate("DATE_OF_JOINING")).append(" | ")
.append(rs.getInt("DEPT_ID")).append("\n");
}
return data.toString();
}

private void NavyInsertPage() {


setTitle("Insert Navy Personnel");
setSize(800, 800);
setLayout(new FlowLayout());

// Add fields for inserting personnel


Label idLabel = new Label("N_ID:");
final TextField idField = new TextField(20);
Label nameLabel = new Label("N_Name:");
final TextField nameField = new TextField(20);
Label rankLabel = new Label("Rank:");
final TextField rankField = new TextField(20);
Label ageLabel = new Label("Age:");
final TextField ageField = new TextField(5);
Label dateLabel = new Label("Date of Joining (YYYY-MM-DD):");
final TextField dateField = new TextField(10);
Label deptidLabel = new Label("Dept ID:");
final TextField deptidField = new TextField(10);
Button insertButton = new Button("Insert");

insertButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int id = Integer.parseInt(idField.getText());
String name = nameField.getText();
String rank = rankField.getText();
int age = Integer.parseInt(ageField.getText());
String dateStr = dateField.getText(); // Get date as a string

// Convert the date string into a Date object


java.sql.Date joiningDate = java.sql.Date.valueOf(dateStr);
int deptid = Integer.parseInt(deptidField.getText());

// Call method to insert data into NAVY table


insertNavyPersonnel(id, name, rank, age, joiningDate, deptid);
} catch (NumberFormatException nfe) {
System.out.println("Invalid age input");
} catch (SQLException ex) {
ex.printStackTrace();
} catch (IllegalArgumentException ex) {
System.out.println("Invalid date format. Please use YYYY-MM-DD.");
}
}
});

// Add the components to the frame


add(idLabel);
add(idField);
add(nameLabel);
add(nameField);
add(rankLabel);
add(rankField);
add(ageLabel);
add(ageField);
add(dateLabel);
add(dateField);
add(deptidLabel);
add(deptidField);
add(insertButton);

setVisible(true);
}

private void insertNavyPersonnel(int nId, String name, String rank, int age, java.sql.Date
joiningDate, int deptid) throws SQLException {
String query = "INSERT INTO NAVY (N_ID, N_NAME, RANK, AGE,
DATE_OF_JOINING, DEPT_ID) VALUES (?, ?, ?, ?, ?, ?)";
Connection connection = getConnection();
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setInt(1, nId); // Pass the N_ID here
pstmt.setString(2, name);
pstmt.setString(3, rank);
pstmt.setInt(4, age);
pstmt.setDate(5, joiningDate);
pstmt.setInt(6, deptid);
pstmt.executeUpdate();
System.out.println("Personnel inserted successfully!");
}

private void NavyDeletePage() {


setTitle("Delete Navy Personnel");
setSize(800, 800);
setLayout(new FlowLayout());

Label nameLabel = new Label("Name of Personnel to Delete:");


final TextField nameField = new TextField(20);
Button deleteButton = new Button("Delete");
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();

if (!name.isEmpty()) {
try {
deleteNavyPersonnel(name);
showMessage("Personnel deleted successfully!");
} catch (SQLException ex) {
ex.printStackTrace();
showMessage("Error deleting personnel: " + ex.getMessage());
}
} else {
showMessage("Please enter a valid name.");
}
}

private void showMessage(String string) {


// Add logic to display the message in the GUI (optional)
}
});

add(nameLabel);
add(nameField);
add(deleteButton);

setVisible(true);
}

// Delete Navy Personnel


private static void deleteNavyPersonnel(String name) throws SQLException {
String query = "DELETE FROM NAVY WHERE N_NAME = ?";
Connection connection = getConnection();
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, name);
pstmt.executeUpdate();
System.out.println("Personnel deleted successfully!");
}

private void NavyUpdatePage() {


setTitle("Update Navy Personnel");
setSize(800, 800);
setLayout(new FlowLayout());

Label nameLabel = new Label("Name:");


final TextField nameField = new TextField(20);
Label rankLabel = new Label("Rank:");
final TextField rankField = new TextField(20);
Label ageLabel = new Label("Age:");
final TextField ageField = new TextField(5);
Label dateLabel = new Label("Joining Date:");
final TextField dateField = new TextField(5);
Label deptidLabel = new Label("Dept ID:");
final TextField deptidField = new TextField(5); // Added deptId field
Button updateButton = new Button("Update");

updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String name = nameField.getText();
String rank = rankField.getText();
int age = Integer.parseInt(ageField.getText());
String dateStr = dateField.getText(); // Get date as a string
int deptId = Integer.parseInt(deptidField.getText()); // Get deptId as integer

// Convert the date string into a Date object


java.sql.Date joiningDate = java.sql.Date.valueOf(dateStr);

updateNavyPersonnel(name, rank, age, joiningDate, deptId);


} catch (SQLException ex) {
ex.printStackTrace();
} catch (IllegalArgumentException ex) {
System.out.println("Invalid date format. Please use YYYY-MM-DD.");
}
}
});

add(nameLabel);
add(nameField);
add(rankLabel);
add(rankField);
add(ageLabel);
add(ageField);
add(dateLabel);
add(dateField);
add(deptidLabel);
add(deptidField);
add(updateButton);

setVisible(true);
}

// Update Navy Personnel (with DEPT_ID)


private void updateNavyPersonnel(String name, String rank, int age, java.sql.Date
joiningDate, int deptId) throws SQLException {
String query = "UPDATE NAVY SET RANK = ?, AGE = ?, DATE_OF_JOINING
= ?, DEPT_ID = ? WHERE N_NAME = ?";
Connection connection = getConnection();
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, rank);
pstmt.setInt(2, age);
pstmt.setDate(3, joiningDate);
pstmt.setInt(4, deptId); // Set DEPT_ID in the query
pstmt.setString(5, name);
pstmt.executeUpdate();
System.out.println("Personnel updated successfully!");
}
}
// Airforce Personnel Page
public static class AirforcePage extends Frame {

AirforcePage() {
setTitle("Airforce Personnel");
setSize(800, 800);
setLayout(new BorderLayout());
final TextArea textArea = new TextArea();
textArea.setEditable(false);
add(textArea, BorderLayout.CENTER);
Panel panel = new Panel();
panel.setLayout(new FlowLayout());
Button fetchButton = new Button("Fetch Data");
Button insertButton = new Button("Insert");
Button deleteButton = new Button("Delete");
Button updateButton = new Button("Update");

fetchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
// Fetch data and set it to the text area
textArea.setText(getAirforceData());
} catch (SQLException ex) {
ex.printStackTrace(); // Print the exception trace to debug
textArea.setText("Error fetching data: " + ex.getMessage()); // Show error in
the text area
}
}
});

insertButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Open the Insert page (ensure the AirforceInsertPage is correctly implemented)
AirforceInsertPage();
}
});

deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Open the Delete page (ensure the AirforceDeletePage is correctly
implemented)
AirforceDeletePage();
}
});

updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Open the Update page (ensure the AirforceUpdatePage is correctly
implemented)
AirforceUpdatePage();
}
});

panel.add(fetchButton);
panel.add(insertButton);
panel.add(deleteButton);
panel.add(updateButton);
add(panel, BorderLayout.SOUTH);
setVisible(true);
}

// Fetch Airforce Personnel Data from DB


private String getAirforceData() throws SQLException {
StringBuilder data = new StringBuilder();
String query = "SELECT F_ID, F_NAME, RANK, AGE, JOINING_DATE,
DEPT_ID FROM AIRFORCE";
Connection connection = getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
data.append(rs.getInt("F_ID")).append(" | ")
.append(rs.getString("F_NAME")).append(" | ")
.append(rs.getString("RANK")).append(" | ")
.append(rs.getInt("AGE")).append(" | ")
.append(rs.getDate("JOINING_DATE")).append(" | ")
.append(rs.getInt("DEPT_ID")).append("\n");
}
return data.toString();
}

private void AirforceInsertPage() {


setTitle("Insert Airforce Personnel");
setSize(800, 800);
setLayout(new FlowLayout());

// Add fields for inserting personnel


Label idLabel = new Label("F_ID:");
final TextField idField = new TextField(20);
Label nameLabel = new Label("F_Name:");
final TextField nameField = new TextField(20);
Label rankLabel = new Label("Rank:");
final TextField rankField = new TextField(20);
Label ageLabel = new Label("Age:");
final TextField ageField = new TextField(5);
Label dateLabel = new Label("Date of Joining (YYYY-MM-DD):");
final TextField dateField = new TextField(10);
Label deptidLabel = new Label("Dept ID:");
final TextField deptidField = new TextField(10);
Button insertButton = new Button("Insert");

insertButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int id = Integer.parseInt(idField.getText());
String name = nameField.getText();
String rank = rankField.getText();
int age = Integer.parseInt(ageField.getText());
String dateStr = dateField.getText(); // Get date as a string

// Convert the date string into a Date object


java.sql.Date joiningDate = java.sql.Date.valueOf(dateStr);
int deptid = Integer.parseInt(deptidField.getText());

// Call method to insert data into AIRFORCE table


insertAirforcePersonnel(id, name, rank, age, joiningDate, deptid);
} catch (NumberFormatException nfe) {
System.out.println("Invalid age input");
} catch (SQLException ex) {
ex.printStackTrace();
} catch (IllegalArgumentException ex) {
System.out.println("Invalid date format. Please use YYYY-MM-DD.");
}
}
});

// Add the components to the frame


add(idLabel);
add(idField);
add(nameLabel);
add(nameField);
add(rankLabel);
add(rankField);
add(ageLabel);
add(ageField);
add(dateLabel);
add(dateField);
add(deptidLabel);
add(deptidField);
add(insertButton);

setVisible(true);
}

private void insertAirforcePersonnel(int aId, String name, String rank, int age,
java.sql.Date joiningDate, int deptId) throws SQLException {
String query = "INSERT INTO AIRFORCE (F_ID, F_NAME, RANK, AGE,
JOINING_DATE, DEPT_ID) VALUES (?, ?, ?, ?, ?, ?)";
Connection connection = getConnection();
PreparedStatement pstmt = connection.prepareStatement(query);

pstmt.setInt(1, aId); // Set A_ID


pstmt.setString(2, name); // Set A_NAME
pstmt.setString(3, rank); // Set RANK
pstmt.setInt(4, age); // Set AGE
pstmt.setDate(5, joiningDate); // Set JOINING_DATE
pstmt.setInt(6, deptId); // Set DEPT_ID

pstmt.executeUpdate(); // Execute the insert query


System.out.println("Personnel inserted successfully!");
}

private void AirforceDeletePage() {


setTitle("Delete Airforce Personnel");
setSize(800, 800);
setLayout(new FlowLayout());
Label nameLabel = new Label("Name of Personnel to Delete:");
final TextField nameField = new TextField(20);
Button deleteButton = new Button("Delete");

deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();

if (!name.isEmpty()) {
try {
deleteAirforcePersonnel(name);
showMessage("Personnel deleted successfully!");
} catch (SQLException ex) {
ex.printStackTrace();
showMessage("Error deleting personnel: " + ex.getMessage());
}
} else {
showMessage("Please enter a valid name.");
}
}

private void showMessage(String string) {


// Add logic to display the message in the GUI (optional)
}
});

add(nameLabel);
add(nameField);
add(deleteButton);
setVisible(true);
}

// Delete Airforce Personnel


private static void deleteAirforcePersonnel(String name) throws SQLException {
String query = "DELETE FROM AIRFORCE WHERE F_NAME = ?";
Connection connection = getConnection();
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, name);
pstmt.executeUpdate();
System.out.println("Personnel deleted successfully!");
}

private void AirforceUpdatePage() {


setTitle("Update Airforce Personnel");
setSize(800, 800);
setLayout(new FlowLayout());

Label nameLabel = new Label("F_Name:");


final TextField nameField = new TextField(20);
Label rankLabel = new Label("Rank:");
final TextField rankField = new TextField(20);
Label ageLabel = new Label("Age:");
final TextField ageField = new TextField(5);
Label dateLabel = new Label("Joining Date:");
final TextField dateField = new TextField(5);
Label deptidLabel = new Label("Dept ID:");
final TextField deptidField = new TextField(5); // Added deptId field
Button updateButton = new Button("Update");
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String name = nameField.getText();
String rank = rankField.getText();
int age = Integer.parseInt(ageField.getText());
String dateStr = dateField.getText(); // Get date as a string
int deptId = Integer.parseInt(deptidField.getText()); // Get deptId as integer

// Convert the date string into a Date object


java.sql.Date joiningDate = java.sql.Date.valueOf(dateStr);

updateAirforcePersonnel(name, rank, age, joiningDate, deptId);


} catch (SQLException ex) {
ex.printStackTrace();
} catch (IllegalArgumentException ex) {
System.out.println("Invalid date format. Please use YYYY-MM-DD.");
}
}
});

add(nameLabel);
add(nameField);
add(rankLabel);
add(rankField);
add(ageLabel);
add(ageField);
add(dateLabel);
add(dateField);
add(deptidLabel);
add(deptidField);
add(updateButton);

setVisible(true);
}

// Update Airforce Personnel (with DEPT_ID)


private void updateAirforcePersonnel(String name, String rank, int age, java.sql.Date
joiningDate, int deptId) throws SQLException {
String query = "UPDATE AIRFORCE SET RANK = ?, AGE = ?, JOINING_DATE
= ?, DEPT_ID = ? WHERE F_NAME = ?";
Connection connection = getConnection();
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, rank);
pstmt.setInt(2, age);
pstmt.setDate(3, joiningDate);
pstmt.setInt(4, deptId); // Set DEPT_ID in the query
pstmt.setString(5, name);
pstmt.executeUpdate();
System.out.println("Personnel updated successfully!");
}
}

public static void main(String[] args) {


new Main();
}
}
OUTPUT:

You might also like