0% found this document useful (0 votes)
18 views7 pages

Exp 12

Uploaded by

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

Exp 12

Uploaded by

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

Exp 12 :

Code :
// java -cp .:mysql-connector-j-9.0.0.jar AdmissionForm.java
// CREATE TABLE students ( id INT AUTO_INCREMENT, name
VARCHAR(255) NOT NULL, father_name VARCHAR(255) NOT NULL,
mother_name VARCHAR(255) NOT NULL, dob DATE NOT NULL, contact
VARCHAR(20) NOT NULL, email VARCHAR(255) NOT NULL, address TEXT
NOT NULL, PRIMARY KEY (id) );
// ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY
'kjs2024'; FLUSH PRIVILEGES;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdmissionForm extends JFrame {


private JLabel nameLabel, fatherNameLabel, motherNameLabel, dobLabel,
contactLabel, emailLabel, addressLabel, idLabel;
private JTextField nameField, fatherNameField, motherNameField,
dobField, contactField, emailField, addressField, idField;
private JButton submitButton, resetButton, retrieveButton,
updateButton, deleteButton;

private Connection connection;

public AdmissionForm() {
super("Admission Form");
setLayout(new GridLayout(14, 1));

idLabel = new JLabel("ID:");


add(idLabel);
idField = new JTextField(20);
add(idField);
nameLabel = new JLabel("Name:");
add(nameLabel);
nameField = new JTextField(20);
add(nameField);

fatherNameLabel = new JLabel("Father's Name:");


add(fatherNameLabel);
fatherNameField = new JTextField(20);
add(fatherNameField);

motherNameLabel = new JLabel("Mother's Name:");


add(motherNameLabel);
motherNameField = new JTextField(20);
add(motherNameField);

dobLabel = new JLabel("Date of Birth (YYYY/MM/DD):");


add(dobLabel);
dobField = new JTextField(20);
add(dobField);

contactLabel = new JLabel("Contact Number:");


add(contactLabel);
contactField = new JTextField(20);
add(contactField);

emailLabel = new JLabel("Email ID:");


add(emailLabel);
emailField = new JTextField(20);
add(emailField);

addressLabel = new JLabel("Address:");


add(addressLabel);
addressField = new JTextField(20);
add(addressField);

submitButton = new JButton("Submit");


add(submitButton);
submitButton.addActionListener(new SubmitButtonListener());

resetButton = new JButton("Reset");


add(resetButton);
resetButton.addActionListener(new ResetButtonListener());

retrieveButton = new JButton("Retrieve");


add(retrieveButton);
retrieveButton.addActionListener(new RetrieveButtonListener());

updateButton = new JButton("Update");


add(updateButton);
updateButton.addActionListener(new UpdateButtonListener());

deleteButton = new JButton("Delete");


add(deleteButton);
deleteButton.addActionListener(new DeleteButtonListener());

setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/admission",
"root", "kjs2024");
} catch (SQLException e) {
System.err.println("Error connecting to database: " +
e.getMessage());
// You can also exit the program or take other actions based on
your requirements
System.exit(1);
}
catch (ClassNotFoundException en) {
System.err.println("Error connecting to database: " +
en.getMessage());
// You can also exit the program or take other actions based on
your requirements
System.exit(1);
}
}
private class SubmitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String fatherName = fatherNameField.getText();
String motherName = motherNameField.getText();
String dob = dobField.getText();
String contact = contactField.getText();
String email = emailField.getText();
String address = addressField.getText();

try {
PreparedStatement statement =
connection.prepareStatement("INSERT INTO students (name, father_name,
mother_name, dob, contact, email, address) VALUES (?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, name);
statement.setString(2, fatherName);
statement.setString(3, motherName);
statement.setString(4, dob);
statement.setString(5, contact);
statement.setString(6, email);
statement.setString(7, address);
statement.executeUpdate();

JOptionPane.showMessageDialog(AdmissionForm.this, "Student
added successfully!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(AdmissionForm.this, "Error
adding student: " + ex.getMessage());
}
}
}

private class ResetButtonListener implements ActionListener {


public void actionPerformed(ActionEvent e) {
idField.setText("");
}}
private class RetrieveButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
PreparedStatement statement =
connection.prepareStatement("SELECT * FROM students");
ResultSet resultSet = statement.executeQuery();

while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String fatherName = resultSet.getString("father_name");
String motherName = resultSet.getString("mother_name");
String dob = resultSet.getString("dob");
String contact = resultSet.getString("contact");
String email = resultSet.getString("email");
String address = resultSet.getString("address");

System.out.println("ID: "+ id + ", Name: " + name + ",


Father's Name: " + fatherName + ", Mother's Name: " + motherName + ", DOB:
" + dob + ", Contact: " + contact + ", Email: " + email + ", Address: " +
address);
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(AdmissionForm.this, "Error
retrieving students: " + ex.getMessage());
}
}
}

private class UpdateButtonListener implements ActionListener {


public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
String name = nameField.getText();
String fatherName = fatherNameField.getText();
String motherName = motherNameField.getText();
String dob = dobField.getText();
String contact = contactField.getText();
String email = emailField.getText();
String address = addressField.getText();

try {
PreparedStatement statement =
connection.prepareStatement("UPDATE students SET name =?, father_name =?,
mother_name =?, dob =?, contact =?, email =?, address =? WHERE id =?");
statement.setString(1, name);
statement.setString(2, fatherName);
statement.setString(3, motherName);
statement.setString(4, dob);
statement.setString(5, contact);
statement.setString(6, email);
statement.setString(7, address);
statement.setInt(8, id);
statement.executeUpdate();

JOptionPane.showMessageDialog(AdmissionForm.this, "Student
updated successfully!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(AdmissionForm.this, "Error
updating student: " + ex.getMessage());
}
}
}

private class DeleteButtonListener implements ActionListener {


public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());

try {
PreparedStatement statement =
connection.prepareStatement("DELETE FROM students WHERE id =?");
statement.setInt(1, id);
statement.executeUpdate();

JOptionPane.showMessageDialog(AdmissionForm.this, "Student
deleted successfully!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(AdmissionForm.this, "Error
deleting student: " + ex.getMessage());
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new AdmissionForm();
}
});
}
}

Output :

You might also like