j1
1)/*
Write a Java program using Multithreading to display all the alphabets between ‘A’ to
‘Z’ after every 2 seconds.
*/
package com.mycompany.javaslip;
import java.util.logging.*;
public class slip1_1
{
public static void main(String[] args)
{
Thread t = new Thread(() ->
{
while(true)
{
for(char ch = 'A'; ch <= 'Z'; ch++)
System.out.print(ch + " ");
System.out.println();
try
{
Thread.sleep(2000);
}
catch (InterruptedException ex)
{
Logger.getLogger(slip1_1.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("2 seconds are passed....");
}
});
t.start();
}
}
/*
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)
*/
package com.mycompany.practical_slip;
//slip 1-2
import java.awt.;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.sql.;
import java.util.logging.;
import javax.swing.;
class EmpApp {
private JFrame frame;
private JTextField eno, ename, desig, sal;
private JButton clear, insert;
EmpApp() throws SQLException {
frame = new JFrame("Employee App");
frame.setSize(400, 200);
frame.setLayout(new GridLayout(5,2));
eno = new JTextField();
ename = new JTextField();
desig = new JTextField();
sal = new JTextField();
frame.add(new JLabel("Eno."));
frame.add(eno);
frame.add(new JLabel("EName"));
frame.add(ename);
frame.add(new JLabel("Designation"));
frame.add(desig);
frame.add(new JLabel("Salary"));
frame.add(sal);
clear = new JButton("Clear");
insert = new JButton("insert");
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "bhalchandra");
insert.addActionListener((ActionEvent e) -> {
try {
insertEmp(conn, eno, ename, desig, sal);
} catch (IOException | SQLException ex) {
Logger.getLogger(EmpApp.class.getName()).log(Level.SEVERE, null, ex);
}
});
clear.addActionListener((ActionEvent e) -> {
eno.setText("");
ename.setText("");
desig.setText("");
sal.setText("");
});
frame.add(insert);
frame.add(clear);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static void insertEmp(Connection conn, JTextField eno, JTextField ename, JTextField desig, JTextField sal)
throws IOException, SQLException {
String sql = "insert into emp values(?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, Integer.parseInt(eno.getText()));
ps.setString(2, ename.getText());
ps.setString(3, desig.getText());
ps.setFloat(4, Float.parseFloat(sal.getText()));
ps.executeUpdate();
}
}
public class slip1_2
{
public static void main(String[] args) throws SQLException {
new EmpApp();
}
}