import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class empawt extends Frame implements ActionListener
{
TextField t1 = new TextField(50);
TextField t2 = new TextField(50);
TextField t3 = new TextField(50);
Label l1 = new Label("Emp No :");
Label l2 = new Label("Emp Name :");
Label l3 = new Label("Emp Salary :");
Button b1 = new Button("Insert");
PreparedStatement pstmt;
Connection con;
empawt() throws Exception
{
setSize(300,300);
setLayout(null);
setVisible(true);
l1.setBounds(50,50,100,30);
l1.setVisible(true);
add(l1);
t1.setBounds(170,50,100,30);
t1.setVisible(true);
add(t1);
l2.setBounds(50,100,100,30);
l2.setVisible(true);
add(l2);
t2.setBounds(170,100,100,30);
t2.setVisible(true);
add(t2);
l3.setBounds(50,150,100,30);
l3.setVisible(true);
add(l3);
t3.setBounds(170,150,100,30);
t3.setVisible(true);
add(t3);
b1.setBounds(100,220,80,30);
b1.setVisible(true);
add(b1);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
dispose();
}
});
b1.addActionListener(this);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:dbAccess","my","asa");
pstmt = con.prepareStatement("Insert into employee values(?,?,?)");
}
public void actionPerformed(ActionEvent ae)
{ try
{
int no = Integer.parseInt(t1.getText());
String str = (t2.getText());
int sal = Integer.parseInt(t3.getText());
pstmt.setInt(1,no);
pstmt.setString(2,str);
pstmt.setInt(3,sal);
int i = pstmt.executeUpdate();
System.out.println("Record Inserted");
} catch(NumberFormatException e) { System.out.println("Type Casting error ");
e.printStackTrace();}
catch(Exception e) {e.printStackTrace();}
}
public static void main(String ar[]) throws Exception
{ empawt ea = new empawt();
}
}
Post a Comment