Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 15 Answers

 


A) Write a java program to display each alphabet after 2 seconds between ‘a’ to ‘z’.

Answer :

public class Slip15A {

    public static void main(String args[]) {

        alpha a1 = new alpha();

        a1.start();

    }

}

 

class alpha extends Thread {

    public void run() {

        try {

            for(int i=97; i<=122; i++){

                System.out.println((char)i);

                sleep(2000);

            }

        } catch (Exception e) {

        }

    }

}

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).

Answer : 

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

public class StudentDetails extends JFrame implements ActionListener {

    JLabel l1, l2, l3, l4, l5;
    JTextField t1, t2, t3, t4;
    JButton b1, b2;
    JComboBox<String> comboBox;
    Connection conn = null;
    PreparedStatement pst = null;

    public StudentDetails() {
        setTitle("Student Details");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);

        l1 = new JLabel("Roll No:");
        l2 = new JLabel("Name:");
        l3 = new JLabel("Percentage:");
        l4 = new JLabel("Gender:");
        l5 = new JLabel("Class:");

        t1 = new JTextField();
        t2 = new JTextField();
        t3 = new JTextField();

        String[] genders = {"Male", "Female", "Other"};
        comboBox = new JComboBox<>(genders);

        t4 = new JTextField();

        b1 = new JButton("Save");
        b2 = new JButton("Cancel");

        JPanel p = new JPanel(new GridLayout(6, 2));
        p.add(l1);
        p.add(t1);
        p.add(l2);
        p.add(t2);
        p.add(l3);
        p.add(t3);
        p.add(l4);
        p.add(comboBox);
        p.add(l5);
        p.add(t4);
        p.add(b1);
        p.add(b2);

        add(p);

        b1.addActionListener(this);
        b2.addActionListener(this);

        setVisible(true);

        // Connect to the database
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }
    }

    public static void main(String[] args) {
        new StudentDetails();
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == b1) { // Save button is clicked
            try {
                String query = "insert into students(RNo, SName, Per, Gender, Class) values (?, ?, ?, ?, ?)";
                pst = conn.prepareStatement(query);
                pst.setInt(1, Integer.parseInt(t1.getText()));
                pst.setString(2, t2.getText());
                pst.setDouble(3, Double.parseDouble(t3.getText()));
                pst.setString(4, (String) comboBox.getSelectedItem());
                pst.setInt(5, Integer.parseInt(t4.getText()));
                pst.executeUpdate();
                JOptionPane.showMessageDialog(null, "Data saved successfully!");
                t1.setText("");
                t2.setText("");
                t3.setText("");
                t4.setText("");
            } catch (SQLException ex) {
                System.out.println("Error: " + ex.getMessage());
            }
        } else if (ae.getSource() == b2) { // Cancel button is clicked
            System.exit(0);
        }
    }
}

Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 15 Answers Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 15 Answers Reviewed by technical_saurabh on December 15, 2022 Rating: 5

No comments:

Powered by Blogger.