Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
209 views

Java Practical Slips Solution

The document contains two Java programming questions and their solutions. The first question displays all letters from A to Z every 2 seconds. The second question accepts employee details from the user and stores it in a database using Swing.

Uploaded by

payalwani73
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
209 views

Java Practical Slips Solution

The document contains two Java programming questions and their solutions. The first question displays all letters from A to Z every 2 seconds. The second question accepts employee details from the user and stores it in a database using Swing.

Uploaded by

payalwani73
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like