ns1
Q1]
public class Main extends Thread
{
char c;
public void run()
{
for(c = 'A'; c<='Z';c++)
{
System.out.println(""+c);
try
{
Thread.sleep(3000);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public static void main(String args[])
{
Main t = new Main();
t.start();
}
}
Q2]
import javax.swing.;
import java.awt.event.;
import java.sql.*;
public class EmployeeDetailsForm extends JFrame implements ActionListener {
JLabel label1, label2, label3, label4;
JTextField textField1, textField2, textField3, textField4;
JButton button;
public EmployeeDetailsForm() {
label1 = new JLabel("Employee Number:");
label1.setBounds(50, 50, 150, 20);
label2 = new JLabel("Employee Name:");
label2.setBounds(50, 80, 150, 20);
label3 = new JLabel("Designation:");
label3.setBounds(50, 110, 150, 20);
label4 = new JLabel("Salary:");
label4.setBounds(50, 140, 150, 20);
textField1 = new JTextField();
textField1.setBounds(200, 50, 150, 20);
textField2 = new JTextField();
textField2.setBounds(200, 80, 150, 20);
textField3 = new JTextField();
textField3.setBounds(200, 110, 150, 20);
textField4 = new JTextField();
textField4.setBounds(200, 140, 150, 20);
button = new JButton("Submit");
button.setBounds(150, 180, 100, 30);
button.addActionListener(this);
add(label1);
add(label2);
add(label3);
add(label4);
add(textField1);
add(textField2);
add(textField3);
add(textField4);
add(button);
setSize(400, 300);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
String eno = textField1.getText();
String ename = textField2.getText();
String designation = textField3.getText();
String salary = textField4.getText();
try {
Connection con = DriverManager.getConnection("jdbc:postgresql:database-name:postgres:postgres);
PreparedStatement stmt = con.prepareStatement("INSERT INTO employee (eno, ename, designation, salary) VALUES (?, ?, ?, ?)");
stmt.setString(1, eno);
stmt.setString(2, ename);
stmt.setString(3, designation);
stmt.setString(4, salary);
int rowsAffected = stmt.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(this, "Employee details added successfully!");
textField1.setText("");
textField2.setText("");
textField3.setText("");
textField4.setText("");
} else {
JOptionPane.showMessageDialog(this, "Failed to add employee details!");
}
con.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
}
}
public static void main(String[] args) {
new EmployeeDetailsForm();
}
}