0% found this document useful (0 votes)
2 views

Assignment 6

The document contains a Java Swing application for managing a student database, allowing users to add, update, and delete student records. It establishes a JDBC connection to a MySQL database and includes GUI components such as text fields and buttons for user interaction. Additionally, it provides SQL code to create the necessary database and table structure for storing student information.

Uploaded by

itsvaibhavpujari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 6

The document contains a Java Swing application for managing a student database, allowing users to add, update, and delete student records. It establishes a JDBC connection to a MySQL database and includes GUI components such as text fields and buttons for user interaction. Additionally, it provides SQL code to create the necessary database and table structure for storing student information.

Uploaded by

itsvaibhavpujari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

import java.awt.

*;

import java.sql.*;

import javax.swing.*;

public class StudentDatabaseGUI extends JFrame {

// GUI Components

private JTextField rollField, nameField;

private JButton addBtn, updateBtn, deleteBtn;

// JDBC Variables

private Connection conn;

public StudentDatabaseGUI() {

setTitle("Student DB Manager");

setSize(400, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

setLayout(new GridLayout(4, 2, 10, 10));

// Fields

add(new JLabel("Roll No:"));

rollField = new JTextField();

add(rollField);

add(new JLabel("Name:"));
nameField = new JTextField();

add(nameField);

// Buttons

addBtn = new JButton("Add");

updateBtn = new JButton("Update");

deleteBtn = new JButton("Delete");

add(addBtn);

add(updateBtn);

add(deleteBtn);

// Button Events

addBtn.addActionListener(e -> addStudent());

updateBtn.addActionListener(e -> updateStudent());

deleteBtn.addActionListener(e -> deleteStudent());

// JDBC Connection

connectToDatabase();

setVisible(true);

private void connectToDatabase() {

try {

// Load the JDBC driver


Class.forName("com.mysql.cj.jdbc.Driver");

// Change the URL, username, and password as needed

conn = DriverManager.getConnection(

"jdbc:mysql://localhost:3306/studentdb", "root", "chinmay"

);

System.out.println("Connected to database.");

} catch (Exception e) {

JOptionPane.showMessageDialog(this, "Database connection failed: " + e.getMessage());

private void addStudent() {

try {

String sql = "INSERT INTO students (roll_no, name) VALUES (?, ?)";

PreparedStatement ps = conn.prepareStatement(sql);

ps.setInt(1, Integer.parseInt(rollField.getText()));

ps.setString(2, nameField.getText());

ps.executeUpdate();

JOptionPane.showMessageDialog(this, "Student added.");

} catch (SQLException e) {

JOptionPane.showMessageDialog(this, "Add Error: " + e.getMessage());

private void updateStudent() {


try {

String sql = "UPDATE students SET name = ? WHERE roll_no = ?";

PreparedStatement ps = conn.prepareStatement(sql);

ps.setString(1, nameField.getText());

ps.setInt(2, Integer.parseInt(rollField.getText()));

int rowsAffected = ps.executeUpdate();

if (rowsAffected > 0)

JOptionPane.showMessageDialog(this, "Student updated.");

else

JOptionPane.showMessageDialog(this, "Roll No not found.");

} catch (SQLException e) {

JOptionPane.showMessageDialog(this, "Update Error: " + e.getMessage());

private void deleteStudent() {

try {

String sql = "DELETE FROM students WHERE roll_no = ?";

PreparedStatement ps = conn.prepareStatement(sql);

ps.setInt(1, Integer.parseInt(rollField.getText()));

int rowsAffected = ps.executeUpdate();

if (rowsAffected > 0)

JOptionPane.showMessageDialog(this, "Student deleted.");

else

JOptionPane.showMessageDialog(this, "Roll No not found.");

} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Delete Error: " + e.getMessage());

public static void main(String[] args) {

// Set Look and Feel (optional)

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (Exception ignored) {}

new StudentDatabaseGUI();

SQL code

CREATE DATABASE IF NOT EXISTS studentdb;

USE studentdb;

CREATE TABLE IF NOT EXISTS students (

roll_no INT PRIMARY KEY,

name VARCHAR(100)

);

output

You might also like