NUML
National University of Modern Languages (Lahore Campus)
Database Systems
Assignment No. 2
Make your CV using Swing and store it in Database. Make 3 text fields and respective labels, fetch data
from text field and store it into database, make a table of qualifications of your degrees and store it into
DB.
Submitted To:
Ms. Makia Nazir
Submitted By:
Irfan Rasheed { CS-357 }
MySQL:
CREATE DATABASE CVDatabase;
USE CVDatabase;
CREATE TABLE PersonalInfo (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255),
phone VARCHAR(20)
);
CREATE TABLE Qualifications (
id INT AUTO_INCREMENT PRIMARY KEY,
degree VARCHAR(255),
university VARCHAR(255),
year VARCHAR(20)
);
SELECT * FROM PersonalInfo;
SELECT * FROM Qualifications;
JAVA:
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
class CVApp extends JFrame {
private JTextField nameField, emailField, phoneField;
private JTable qualificationTable;
private DefaultTableModel tableModel;
private JButton saveButton, addRowButton;
private static final String URL = "jdbc:mysql://localhost:3306/CVDatabase";
private static final String USER = "root";
private static final String PASSWORD = "12345";
public CVApp() {
setTitle("CV Application");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel formPanel = new JPanel(new GridLayout(4, 2, 10, 10));
formPanel.setBorder(BorderFactory.createTitledBorder("Personal Information"));
formPanel.add(new JLabel("Name:"));
nameField = new JTextField();
formPanel.add(nameField);
formPanel.add(new JLabel("Email:"));
emailField = new JTextField();
formPanel.add(emailField);
formPanel.add(new JLabel("Phone:"));
phoneField = new JTextField();
formPanel.add(phoneField);
add(formPanel, BorderLayout.NORTH);
tableModel = new DefaultTableModel(new String[]{"Degree", "University", "Year"}, 0);
qualificationTable = new JTable(tableModel);
JScrollPane tableScrollPane = new JScrollPane(qualificationTable);
tableScrollPane.setBorder(BorderFactory.createTitledBorder("Qualifications"));
add(tableScrollPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
addRowButton = new JButton("Add Row");
saveButton = new JButton("Save to DB");
buttonPanel.add(addRowButton);
buttonPanel.add(saveButton);
add(buttonPanel, BorderLayout.SOUTH);
addRowButton.addActionListener(e -> tableModel.addRow(new Object[]{"", "", ""}));
saveButton.addActionListener(e -> saveToDatabase());
setVisible(true);
}
private void saveToDatabase() {
try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
String sql = "INSERT INTO PersonalInfo (name, email, phone) VALUES (?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, nameField.getText());
stmt.setString(2, emailField.getText());
stmt.setString(3, phoneField.getText());
stmt.executeUpdate();
ResultSet rs = stmt.getGeneratedKeys();
int userId = rs.next() ? rs.getInt(1) : 0;
sql = "INSERT INTO Qualifications (degree, university, year) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(sql);
for (int i = 0; i < tableModel.getRowCount(); i++) {
stmt.setString(1, tableModel.getValueAt(i, 0).toString());
stmt.setString(2, tableModel.getValueAt(i, 1).toString());
Object yearValue = tableModel.getValueAt(i, 2);
String year = (yearValue != null && !yearValue.toString().trim().isEmpty())
? yearValue.toString().trim()
: null;
stmt.setString(3, year);
stmt.executeUpdate();
}
JOptionPane.showMessageDialog(this, "Data saved successfully!");
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Database Error: " + e.getMessage());
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(CVApp::new);
}
}