Dbms Miniproject-2
Dbms Miniproject-2
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)
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)
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)
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() {
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());
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);
}
return data.toString();
}
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);
}
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!");
}
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);
}
}
// 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());
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);
}
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
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!");
}
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.");
}
}
add(nameLabel);
add(nameField);
add(deleteButton);
setVisible(true);
}
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
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());
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);
}
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
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!");
}
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.");
}
}
add(nameLabel);
add(nameField);
add(deleteButton);
setVisible(true);
}
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
add(nameLabel);
add(nameField);
add(rankLabel);
add(rankField);
add(ageLabel);
add(ageField);
add(dateLabel);
add(dateField);
add(deptidLabel);
add(deptidField);
add(updateButton);
setVisible(true);
}
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);
}
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
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);
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.");
}
}
add(nameLabel);
add(nameField);
add(deleteButton);
setVisible(true);
}
add(nameLabel);
add(nameField);
add(rankLabel);
add(rankField);
add(ageLabel);
add(ageField);
add(dateLabel);
add(dateField);
add(deptidLabel);
add(deptidField);
add(updateButton);
setVisible(true);
}