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++) {
System.out.print(ch + " ");
// Pause for 2 seconds
try {
Thread.sleep(2000); // 2000 milliseconds = 2 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
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 javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
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();
panel.setLayout(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");
btnSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveEmployeeDetails();
}
});
panel.add(lblEno);
panel.add(txtEno);
panel.add(lblEName);
panel.add(txtEName);
panel.add(lblDesignation);
panel.add(txtDesignation);
panel.add(lblSalary);
panel.add(txtSalary);
panel.add(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 = DriverManager.getConnection(url,
username, password)) {
String query = "INSERT INTO employee (Eno, EName, Designation,
Salary) VALUES (?, ?, ?, ?)";
try (PreparedStatement preparedStatement =
connection.prepareStatement(query)) {
preparedStatement.setString(1, txtEno.getText());
preparedStatement.setString(2, txtEName.getText());
preparedStatement.setString(3, txtDesignation.getText());
preparedStatement.setString(4, txtSalary.getText());
int rowsInserted = preparedStatement.executeUpdate();
if (rowsInserted > 0) {
JOptionPane.showMessageDialog(this, "Employee details
saved successfully!");
} else {
JOptionPane.showMessageDialog(this, "Failed to save
employee details.");
}
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error connecting to the
database.");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new EmployeeDetailsForm();
}
});
}
}