Write a Java program to accept the details of employee(eno, ename, salary) using AWT and insert into database
Write a Java program to accept the details of employee(eno, ename, salary) using AWT and insert into database.
Program:
Program:
import java.awt.*; import java.awt.event.*; import java.io.*; import java.sql.*; public class Employee extends Frame implements ActionListener { Label labelEno, labelEname, labelSalary; TextField textEno, textEname, textSalary; Button btnAdd; String sqlQuery; Statement s; Connection dbConnection; public Employee() throws ClassNotFoundException, SQLException { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); dbConnection = DriverManager.getConnection("jdbc.odbc:EmployeeDSN"); setLayout(new FlowLayout()); setSize(300,300); labelEno = new Label("Employee No:"); labelEname = new Label("Employee Name:"); labelSalary = new Label(Employee Salary:); textEno = new TextField(20); textEname = new TextField(20); textSalary = new TextField(20); btnAdd = new Button("Add"); btnAdd.addActionListener(this); add(labelEno).add(textEno); add(labelEname).add(textEname); add(labelSalary).add(textSalary); add(btnAdd); addWindowListener(new WClose()); setVisible(true); } public void actionPerformed(ActionEvent ae) { int eno; double salary; String ename; try { eno = Integer.parseInt(textEno.getText()); ename = textEname.getText(); salary = Double.parseDouble(textSalary.getText()); sqlQuery = "insert into Employee values("+eno+",'"+ename+"',"+salary+")"; s = dbConnection.createStatement(); int k = s.executeUpdate(sqlQuery); if(k>0) { System.out.println("Employee record inserted."); s.close(); } } catch(SQLException e) { System.out.println("SQL exception."); } } public static void main(String args[])throws Exception { new Employee(); } } class WClose extends WindowAdapter { public void windowClosing(windowEvent we) { System.exit(0); } }
Comments
Post a Comment