0% found this document useful (0 votes)
5 views

java slip 1

The document contains two Java classes: EmployeeForm, which creates a GUI for entering employee details and saves them to a MySQL database, and AlphabetDisplay, which prints the letters A to Z with a 2-second pause between each letter. EmployeeForm includes fields for employee number, name, designation, and salary, and handles database connections and error messages. AlphabetDisplay demonstrates a simple loop with a sleep function to control the output timing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

java slip 1

The document contains two Java classes: EmployeeForm, which creates a GUI for entering employee details and saves them to a MySQL database, and AlphabetDisplay, which prints the letters A to Z with a 2-second pause between each letter. EmployeeForm includes fields for employee number, name, designation, and salary, and handles database connections and error messages. AlphabetDisplay demonstrates a simple loop with a sleep function to control the output timing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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());
}
}
}
}

You might also like