Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Database;
- import java.sql.*;
- import java.util.*;
- import java.awt.*;
- import java.awt.event.*;
- class employee implements ActionListener
- {
- Frame f=new Frame();
- Label l1=new Label("First Name");
- Label l2=new Label("Middle Name");
- Label l3=new Label("Last Name");
- Label l4=new Label("Gender");
- Label l5=new Label("Salary");
- Label l6=new Label("D.No");
- Label l7=new Label("SSN");
- TextField t1=new TextField();
- TextField t2=new TextField();
- TextField t3=new TextField();
- TextField t4=new TextField();
- TextField t5=new TextField();
- TextField t6=new TextField();
- TextField t7=new TextField();
- TextField a1=new TextField();
- Button b1=new Button("Search");
- Button b2=new Button("Insert");
- Button b3=new Button("Exit");
- employee()
- {
- l7.setBounds(50,100,100,20);
- l1.setBounds(160,100,100,20);
- l2.setBounds(270,100,100,20);
- l3.setBounds(380,100,100,20);
- l4.setBounds(490,100,100,20);
- l5.setBounds(600,100,100,20);
- l6.setBounds(720,100,100,20);
- t7.setBounds(50,140,100,20);
- t1.setBounds(160,140,100,20);
- t2.setBounds(270,140,100,20);
- t3.setBounds(380,140,100,20);
- t4.setBounds(490,140,100,20);
- t5.setBounds(600,140,100,20);
- t6.setBounds(710,140,100,20);
- a1.setBounds(50,220,100,20);
- b1.setBounds(50,180,60,20);
- b2.setBounds(160,180,60,20);
- b3.setBounds(270,180,60,20);
- f.add(l1);
- f.add(l2);
- f.add(l3);
- f.add(l4);
- f.add(l5);
- f.add(l6);
- f.add(l7);
- f.add(t1);
- f.add(t2);
- f.add(t3);
- f.add(t4);
- f.add(t5);
- f.add(t6);
- f.add(t7);
- f.add(a1);
- f.add(b1);
- f.add(b2);
- f.add(b3);
- b1.addActionListener(this);
- b2.addActionListener(this);
- b3.addActionListener(this);
- a1.setEditable(false);
- f.setLayout(null);
- f.setVisible(true);
- f.setSize(1000,500);
- }
- public void actionPerformed(ActionEvent e)
- { String z1 = null,sn = null,n = null,m = null,l = null,g = null,s = null,d = null,q = null;
- if(e.getSource()==b1)
- {
- try{
- Connection con=getConnection();
- sn=String.valueOf(t7.getText());
- n=String.valueOf(t1.getText());
- if(sn.length()==0)
- { q ="fname";
- z1=n;
- sn = null;
- }
- else
- {
- q="ssn";
- z1=sn;
- n = null;
- }
- PreparedStatement statement = con.prepareStatement("SELECT * FROM employee WHERE "+q+" = '"+z1+"'");
- ResultSet result = statement.executeQuery();
- while(result.next()){
- n = result.getString("fname");
- m = result.getString("minit");
- l = result.getString("lname");
- g = result.getString("sex");
- s = result.getString("salary");
- d = result.getString("dno");
- sn = result.getString("ssn");
- }
- }
- catch (Exception e1){}
- t1.setText(String.valueOf(n));
- t2.setText(String.valueOf(m));
- t3.setText(String.valueOf(l));
- t4.setText(String.valueOf(g));
- t5.setText(String.valueOf(s));
- t6.setText(String.valueOf(d));
- t7.setText(String.valueOf(sn));
- if(d==null)
- a1.setText(String.valueOf("Not Found"));
- else
- a1.setText(String.valueOf("Found"));
- }
- if (e.getSource()==b2)
- {
- try{
- Connection con=getConnection();
- sn=String.valueOf(t7.getText());
- n=String.valueOf(t1.getText());
- m=String.valueOf(t2.getText());
- l=String.valueOf(t3.getText());
- g=String.valueOf(t4.getText());
- s=String.valueOf(t5.getText());
- d=String.valueOf(t6.getText());
- PreparedStatement posted = con.prepareStatement("INSERT INTO employee VALUES ('"+sn+"','"+n+"','"+m+"','"+l+"','"+g+"','"+d+"',"+s+")");
- posted.executeUpdate();
- a1.setText("Success");
- }
- catch(Exception e1){
- a1.setText("Fail");
- }
- }
- if (e.getSource()==b3)
- {
- System.exit(0);
- }
- }
- public static void main(String args[])
- {
- new employee();
- }
- public static Connection getConnection() throws Exception{
- try{
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost/company";
- String username = "root";
- String password = "kota1998";
- Class.forName(driver); Connection conn = DriverManager.getConnection(url,username,password);
- System.out.println("Connected \n\n");
- return conn;
- } catch(Exception e){System.out.println(e);}
- return null;
- }
- }
Add Comment
Please, Sign In to add comment