Java practical slips solution
Slips 1
Q.1 Write a Java program to display all the alphabets between ‘A’ to ‘Z’ after every 2 seconds
public class Main {
public static void main(String[] args) {
// Starting from 'A' (ASCII value 65) to 'Z' (ASCII value 90)
for (char ch = 'A'; ch <= 'Z'; ch++) {
[Link](ch + " ");
// Pause for 2 seconds
try {
[Link](2000); // 2000 milliseconds = 2 seconds
} catch (InterruptedException e) {
[Link]();
}
}
}
}
Q.2 Write a Java program to accept the details of Employee (Eno, EName, Designation,
Salary) from a user and store it into the database. (Use Swing)
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class EmployeeDetailsForm extends JFrame {
private JTextField txtEno, txtEName, txtDesignation, txtSalary;
public EmployeeDetailsForm() {
setTitle("Employee Details Form");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
[Link](new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel lblEno = new JLabel("Employee Number:");
txtEno = new JTextField(20);
JLabel lblEName = new JLabel("Employee Name:");
txtEName = new JTextField(20);
JLabel lblDesignation = new JLabel("Designation:");
txtDesignation = new JTextField(20);
JLabel lblSalary = new JLabel("Salary:");
txtSalary = new JTextField(20);
JButton btnSave = new JButton("Save");
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveEmployeeDetails();
}
});
[Link](lblEno);
[Link](txtEno);
[Link](lblEName);
[Link](txtEName);
[Link](lblDesignation);
[Link](txtDesignation);
[Link](lblSalary);
[Link](txtSalary);
[Link](btnSave);
add(panel);
setLocationRelativeTo(null);
setVisible(true);
}
private void saveEmployeeDetails() {
String url = "jdbc:mysql://localhost:3306/your_database_name";
String username = "your_username";
String password = "your_password";
try (Connection connection = [Link](url,
username, password)) {
String query = "INSERT INTO employee (Eno, EName, Designation,
Salary) VALUES (?, ?, ?, ?)";
try (PreparedStatement preparedStatement =
[Link](query)) {
[Link](1, [Link]());
[Link](2, [Link]());
[Link](3, [Link]());
[Link](4, [Link]());
int rowsInserted = [Link]();
if (rowsInserted > 0) {
[Link](this, "Employee details
saved successfully!");
} else {
[Link](this, "Failed to save
employee details.");
}
}
} catch (SQLException ex) {
[Link]();
[Link](this, "Error connecting to the
database.");
}
}
public static void main(String[] args) {
[Link](new Runnable() {
@Override
public void run() {
new EmployeeDetailsForm();
}
});
}
}