ja1
-
Write a Java program to display all the alphabets between ‘A’ to ‘Z’ after every 2
seconds. [15 M]
public class AlphabetPrinter {
public static void main(String[] args) {
char alphabet = 'A';
while (alphabet <= 'Z') {
System.out.print(alphabet + " ");
alphabet++;
try {
Thread.sleep(2000); // Sleep for 2 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} -
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 implements ActionListener {
private JTextField enoField, enameField, designationField, salaryField;
private JButton saveButton;
public EmployeeDetailsForm() {
setTitle("Employee Details Form");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
enoField = new JTextField(10);
enameField = new JTextField(10);
designationField = new JTextField(10);
salaryField = new JTextField(10);
saveButton = new JButton("Save");
saveButton.addActionListener(this);
JPanel panel = new JPanel();
panel.add(new JLabel("Eno:"));
panel.add(enoField);
panel.add(new JLabel("EName:"));
panel.add(enameField);
panel.add(new JLabel("Designation:"));
panel.add(designationField);
panel.add(new JLabel("Salary:"));
panel.add(salaryField);
panel.add(saveButton);
add(panel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == saveButton) {
String eno = enoField.getText();
String ename = enameField.getText();
String designation = designationField.getText();
double salary = Double.parseDouble(salaryField.getText());
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
String query = "INSERT INTO Employee (Eno, EName, Designation, Salary) VALUES (?, ?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(query);
statement.setString(1, eno);
statement.setString(2, ename);
statement.setString(3, designation);
statement.setDouble(4, salary);
statement.executeUpdate();
JOptionPane.showMessageDialog(this, "Employee details saved successfully!");
statement.close();
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new EmployeeDetailsForm();
}
}