Q2
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class EmployeeForm extends JFrame {
private JTextField txtEno, txtEName, txtDesignation, txtSalary;
private JButton btnSave;
public EmployeeForm() {
setTitle("Employee Form");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(5, 2));
add(new JLabel("Employee No:"));
txtEno = new JTextField();
add(txtEno);
add(new JLabel("Employee Name:"));
txtEName = new JTextField();
add(txtEName);
add(new JLabel("Designation:"));
txtDesignation = new JTextField();
add(txtDesignation);
add(new JLabel("Salary:"));
txtSalary = new JTextField();
add(txtSalary);
btnSave = new JButton("Save");
add(btnSave);
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveEmployeeData();
}
});
setVisible(true);
}
private void saveEmployeeData() {
String url = "jdbc:mysql://localhost:3306/your_database";
String user = "your_username";
String password = "your_password";
String eno = txtEno.getText();
String ename = txtEName.getText();
String designation = txtDesignation.getText();
String salary = txtSalary.getText();
String query = "INSERT INTO Employee (Eno, EName, Designation, Salary)
VALUES (?, ?, ?, ?)";
try (Connection con = DriverManager.getConnection(url, user, password);
PreparedStatement pst = con.prepareStatement(query)) {
pst.setInt(1, Integer.parseInt(eno));
pst.setString(2, ename);
pst.setString(3, designation);
pst.setDouble(4, Double.parseDouble(salary));
int rowsInserted = pst.executeUpdate();
if (rowsInserted > 0) {
JOptionPane.showMessageDialog(this, "Employee data saved
successfully!");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
}
public static void main(String[] args) {
new EmployeeForm();
}
}
Q1
public class AlphabetDisplay {
public static void main(String[] args) {
for (char letter = 'A'; letter <= 'Z'; letter++) {
System.out.println(letter);
try {
Thread.sleep(2000); // Pause for 2 seconds
} catch (InterruptedException e) {
System.err.println("Thread interrupted: " + e.getMessage());
}
}
}
}