0% found this document useful (0 votes)
4 views21 pages

Java Lab 2

The document contains Java code for a student management system with functionalities to insert, delete, update, and display student information using a graphical user interface (GUI). It includes classes for each operation, utilizing JDBC for database connectivity to a MySQL database. The code handles user input, performs database operations, and provides feedback through dialog messages.

Uploaded by

ko htet
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)
4 views21 pages

Java Lab 2

The document contains Java code for a student management system with functionalities to insert, delete, update, and display student information using a graphical user interface (GUI). It includes classes for each operation, utilizing JDBC for database connectivity to a MySQL database. The code handles user input, performs database operations, and provides feedback through dialog messages.

Uploaded by

ko htet
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

Name : Tay Zar Min Htay

Roll No : III IT – 22

Lab : 2

Subject : IT – 32045
Date : 1.9.2025

1. [Link]
package lab2;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class InsertStudent extends JFrame {

JTextField nameField;
JTextField rollNoField;
JTextField yearField;
JTextField phnoField;
JButton insertButton;
JButton clearButton;

public InsertStudent() {
setTitle("Student Information");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(null);

JLabel titleLabel = new JLabel("Student Information");


[Link](new Font("Arial", [Link], 16));
[Link](180, 20, 200, 30);
add(titleLabel);
JLabel nameLabel = new JLabel("Name");
[Link](50, 80, 100, 25);
add(nameLabel);

nameField = new JTextField();


[Link](200, 80, 250, 25);
add(nameField);

JLabel rollNoLabel = new JLabel("RollNo.");


[Link](50, 120, 100, 25);
add(rollNoLabel);

rollNoField = new JTextField();


[Link](200, 120, 250, 25);
add(rollNoField);

JLabel yearLabel = new JLabel("Year");


[Link](50, 160, 100, 25);
add(yearLabel);

yearField = new JTextField();


[Link](200, 160, 250, 25);
add(yearField);

JLabel phnoLabel = new JLabel("Phno");


[Link](50, 200, 100, 25);
add(phnoLabel);

phnoField = new JTextField();


[Link](200, 200, 250, 25);
add(phnoField);

insertButton = new JButton("INSERT");


[Link](120, 280, 100, 35);
add(insertButton);

clearButton = new JButton("CLEAR");


[Link](280, 280, 100, 35);
add(clearButton);

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
insertStudent();
}
});

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearFields();
}
});
}

public void insertStudent() {


String name = [Link]();
String rollNo = [Link]();
String year = [Link]();
String phno = [Link]();

if ([Link]("") || [Link]("") || [Link]("") || [Link]("")) {


[Link](this, "Please fill all fields!");
return;
}

try {
Connection con =
[Link]("jdbc:mysql://localhost/studentdatabase", "root", "");
String sql = "INSERT INTO studentinfo (Name, Rollno, Year, Phno) VALUES (?, ?,
?, ?)";
PreparedStatement pst = [Link](sql);
[Link](1, name);
[Link](2, rollNo);
[Link](3, year);
[Link](4, phno);
int result = [Link]();
if (result > 0) {
[Link](this, "Student inserted successfully!");
clearFields();
}
[Link]();
} catch (Exception ex) {
[Link](this, "Error: " + [Link]());
}
}

public void clearFields() {


[Link]("");
[Link]("");
[Link]("");
[Link]("");
}

public static void main(String[] args) {


try {
[Link]("[Link]");
} catch (ClassNotFoundException e) {
[Link]("Driver not found!");
}
new InsertStudent().setVisible(true);
}
}
[Link]
package lab2;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class DeleteStudent extends JFrame {

JTextField nameField;
JTextField rollNoField;
JTextField yearField;
JTextField phnoField;
JButton deleteButton;
JButton clearButton;

public DeleteStudent() {
setTitle("Student Information");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(null);
JLabel titleLabel = new JLabel("Student Information");
[Link](new Font("Arial", [Link], 16));
[Link](180, 20, 200, 30);
add(titleLabel);

JLabel nameLabel = new JLabel("Name");


[Link](50, 80, 100, 25);
add(nameLabel);

nameField = new JTextField();


[Link](200, 80, 250, 25);
add(nameField);

JLabel rollNoLabel = new JLabel("RollNo.");


[Link](50, 120, 100, 25);
add(rollNoLabel);

rollNoField = new JTextField();


[Link](200, 120, 250, 25);
add(rollNoField);

JLabel yearLabel = new JLabel("Year");


[Link](50, 160, 100, 25);
add(yearLabel);

yearField = new JTextField();


[Link](200, 160, 250, 25);
add(yearField);
JLabel phnoLabel = new JLabel("Phno");
[Link](50, 200, 100, 25);
add(phnoLabel);

phnoField = new JTextField();


[Link](200, 200, 250, 25);
add(phnoField);

deleteButton = new JButton("DELETE");


[Link](120, 280, 100, 35);
add(deleteButton);

clearButton = new JButton("CLEAR");


[Link](280, 280, 100, 35);
add(clearButton);

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteStudent();
}
});

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearFields();
}
});

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchStudent();
}
});
}

public void searchStudent() {


String rollNo = [Link]();
if ([Link]("")) {
return;
}
try {
Connection con =
[Link]("jdbc:mysql://localhost/studentdatabase", "root", "");
String sql = "SELECT * FROM studentinfo WHERE Rollno = ?";
PreparedStatement pst = [Link](sql);
[Link](1, rollNo);
ResultSet rs = [Link]();
if ([Link]()) {
[Link]([Link]("Name"));
[Link]([Link]("Year"));
[Link]([Link]("Phno"));
} else {
[Link](this, "Student not found!");
[Link]("");
[Link]("");
[Link]("");
}
[Link]();
} catch (Exception ex) {
[Link](this, "Error: " + [Link]());
}
}

public void deleteStudent() {


String rollNo = [Link]();
if ([Link]("")) {
[Link](this, "Please enter Roll Number!");
return;
}
int confirm = [Link](this, "Are you sure you want to delete
this student?", "Confirm Delete", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
try {
Connection con =
[Link]("jdbc:mysql://localhost/studentdatabase", "root", "");
String sql = "DELETE FROM studentinfo WHERE Rollno = ?";
PreparedStatement pst = [Link](sql);
[Link](1, rollNo);
int result = [Link]();
if (result > 0) {
[Link](this, "Student deleted successfully!");
clearFields();
} else {
[Link](this, "Student not found!");
}
[Link]();
} catch (Exception ex) {
[Link](this, "Error: " + [Link]());
}
}
}

public void clearFields() {


[Link]("");
[Link]("");
[Link]("");
[Link]("");
}

public static void main(String[] args) {


try {
[Link]("[Link]");
} catch (ClassNotFoundException e) {
[Link]("Driver not found!");
}
new DeleteStudent().setVisible(true);
}
}
[Link]
package lab2;

import [Link];

import [Link];

import [Link];
import [Link];

import [Link];

import [Link];
import [Link];

import [Link];
import [Link];

import [Link];

import [Link];

import [Link];

public class UpdateStudent extends JFrame {

JTextField nameField;

JTextField rollNoField;

JTextField yearField;

JTextField phnoField;
JButton updateButton;
JButton clearButton;

public UpdateStudent() {

setTitle("Student Information");

setSize(500, 400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);
setLayout(null);

JLabel titleLabel = new JLabel("Student Information");

[Link](new Font("Arial", [Link], 16));

[Link](180, 20, 200, 30);

add(titleLabel);

JLabel nameLabel = new JLabel("Name");

[Link](50, 80, 100, 25);


add(nameLabel);
nameField = new JTextField();

[Link](200, 80, 250, 25);

add(nameField);

JLabel rollNoLabel = new JLabel("RollNo.");

[Link](50, 120, 100, 25);

add(rollNoLabel);

rollNoField = new JTextField();

[Link](200, 120, 250, 25);

add(rollNoField);

JLabel yearLabel = new JLabel("Year");

[Link](50, 160, 100, 25);

add(yearLabel);

yearField = new JTextField();

[Link](200, 160, 250, 25);

add(yearField);

JLabel phnoLabel = new JLabel("Phno");

[Link](50, 200, 100, 25);

add(phnoLabel);

phnoField = new JTextField();

[Link](200, 200, 250, 25);


add(phnoField);

updateButton = new JButton("UPDATE");

[Link](120, 280, 100, 35);


add(updateButton);

clearButton = new JButton("CLEAR");

[Link](280, 280, 100, 35);


add(clearButton);

[Link](new ActionListener() {

public void actionPerformed(ActionEvent e) {

updateStudent();

});

[Link](new ActionListener() {

public void actionPerformed(ActionEvent e) {

clearFields();

});

[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {

searchStudent();

});

public void searchStudent() {


String rollNo = [Link]();

if ([Link]("")) {
return;

}
try {

Connection con =
[Link]("jdbc:mysql://localhost/studentdatabase", "root", "");

String sql = "SELECT * FROM studentinfo WHERE Rollno = ?";

PreparedStatement pst = [Link](sql);

[Link](1, rollNo);

ResultSet rs = [Link]();
if ([Link]()) {

[Link]([Link]("Name"));

[Link]([Link]("Year"));

[Link]([Link]("Phno"));

} else {

[Link](this, "Student not found!");

[Link]("");
[Link]("");

[Link]("");

[Link]();

} catch (Exception ex) {

[Link](this, "Error: " + [Link]());

}
}

public void updateStudent() {

String name = [Link]();

String rollNo = [Link]();

String year = [Link]();

String phno = [Link]();


if ([Link]("")) {

[Link](this, "Please enter Roll Number!");

return;

try {

Connection con =
[Link]("jdbc:mysql://localhost/studentdatabase", "root", "");

String sql1 = "SELECT * FROM studentinfo WHERE Rollno = ?";

PreparedStatement pst1 = [Link](sql1);

[Link](1, rollNo);
ResultSet rs = [Link]();

if ([Link]()) {

String newName = [Link]("") ? [Link]("Name") : name;

String newYear = [Link]("") ? [Link]("Year") : year;

String newPhno = [Link]("") ? [Link]("Phno") : phno;

String sql2 = "UPDATE studentinfo SET Name = ?, Year = ?, Phno = ? WHERE


Rollno = ?";

PreparedStatement pst2 = [Link](sql2);

[Link](1, newName);

[Link](2, newYear);

[Link](3, newPhno);
[Link](4, rollNo);
int result = [Link]();

if (result > 0) {

[Link](this, "Student updated successfully!");


}

} else {

[Link](this, "Student not found!");

[Link]();

} catch (Exception ex) {

[Link](this, "Error: " + [Link]());


}
}

public void clearFields() {

[Link]("");

[Link]("");

[Link]("");

[Link]("");
}

public static void main(String[] args) {

try {

[Link]("[Link]");

} catch (ClassNotFoundException e) {

[Link]("Driver not found!");


}

new UpdateStudent().setVisible(true);
}

}
[Link]
package lab2;

import [Link];

import [Link];
import [Link];

import [Link];
import [Link];

import [Link];

import [Link];

import [Link];
import [Link];

import [Link];

public class DisplayStudent extends JFrame {

JTable studentTable;

DefaultTableModel tableModel;

public DisplayStudent() {

setTitle("Student Information Display");

setSize(600, 400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

setLayout(new BorderLayout());

tableModel = new DefaultTableModel();

studentTable = new JTable(tableModel);

JScrollPane scrollPane = new JScrollPane(studentTable);

add(scrollPane, [Link]);

displayData();

public void displayData() {


try {
Connection con =
[Link]("jdbc:mysql://localhost/studentdatabase", "root", "");

String sql = "SELECT * FROM studentinfo";

Statement stmt = [Link]();

ResultSet rs = [Link](sql);

String[] columnNames = {"Name", "RollNo", "Year", "Phno"};


[Link](columnNames);

[Link](0);

while ([Link]()) {

String name = [Link]("Name");

String rollNo = [Link]("Rollno");


String year = [Link]("Year");

String phno = [Link]("Phno");

Object[] row = {name, rollNo, year, phno};

[Link](row);

[Link]();

} catch (Exception ex) {

[Link](this, "Error: " + [Link]());

public static void main(String[] args) {


try {
[Link]("[Link]");

} catch (ClassNotFoundException e) {

[Link]("Driver not found!");

}
new DisplayStudent().setVisible(true);

Fig. Original Student Information Table

Fig. Student Information Table after INSERT, DELETE and UPDATE

You might also like