Slip No 15 Q B

Write a Java program to accept the details of Student (RNo, SName, Per, Gender,Class) and store into the database. (Use appropriate Swing Components and PreparedStatement Interface).

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class StudentEntry extends JFrame implements ActionListener {

    JTextField txtRNo, txtName, txtPer, txtClass;
    JRadioButton rbMale, rbFemale;
    JButton btnSave;

    Connection con;
    PreparedStatement ps;

    public StudentEntry() {

        setTitle("Student Registration");
        setSize(400, 350);
        setLayout(new GridLayout(7, 2, 10, 10));

        add(new JLabel("Roll No:"));
        txtRNo = new JTextField();
        add(txtRNo);

        add(new JLabel("Student Name:"));
        txtName = new JTextField();
        add(txtName);

        add(new JLabel("Percentage:"));
        txtPer = new JTextField();
        add(txtPer);

        add(new JLabel("Gender:"));
        rbMale = new JRadioButton("Male");
        rbFemale = new JRadioButton("Female");
        ButtonGroup bg = new ButtonGroup();
        bg.add(rbMale);
        bg.add(rbFemale);

        JPanel genderPanel = new JPanel();
        genderPanel.add(rbMale);
        genderPanel.add(rbFemale);
        add(genderPanel);

        add(new JLabel("Class:"));
        txtClass = new JTextField();
        add(txtClass);

        btnSave = new JButton("Save");
        add(btnSave);

        btnSave.addActionListener(this);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        connectDB();
    }

    // Database connection
    void connectDB() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/college", "root", "root");

            ps = con.prepareStatement(
                "INSERT INTO Student VALUES (?, ?, ?, ?, ?)");

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e);
        }
    }

    // Button click action
    public void actionPerformed(ActionEvent ae) {

        try {
            int rno = Integer.parseInt(txtRNo.getText());
            String name = txtName.getText();
            float per = Float.parseFloat(txtPer.getText());
            String gender = rbMale.isSelected() ? "Male" : "Female";
            String cls = txtClass.getText();

            ps.setInt(1, rno);
            ps.setString(2, name);
            ps.setFloat(3, per);
            ps.setString(4, gender);
            ps.setString(5, cls);

            ps.executeUpdate();

            JOptionPane.showMessageDialog(this, "Student Record Inserted Successfully");

            txtRNo.setText("");
            txtName.setText("");
            txtPer.setText("");
            txtClass.setText("");

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e);
        }
    }

    public static void main(String[] args) {
        new StudentEntry();
    }
}
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top