Guest User

Untitled

a guest
May 1st, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. package Database;
  2. import java.sql.*;
  3. import java.util.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. class employee implements ActionListener
  8. {
  9. Frame f=new Frame();
  10. Label l1=new Label("First Name");
  11. Label l2=new Label("Middle Name");
  12. Label l3=new Label("Last Name");
  13. Label l4=new Label("Gender");
  14. Label l5=new Label("Salary");
  15. Label l6=new Label("D.No");
  16. Label l7=new Label("SSN");
  17.  
  18. TextField t1=new TextField();
  19. TextField t2=new TextField();
  20. TextField t3=new TextField();
  21. TextField t4=new TextField();
  22. TextField t5=new TextField();
  23. TextField t6=new TextField();
  24. TextField t7=new TextField();
  25.  
  26. TextField a1=new TextField();
  27.  
  28. Button b1=new Button("Search");
  29. Button b2=new Button("Insert");
  30. Button b3=new Button("Exit");
  31.  
  32. employee()
  33. {
  34. l7.setBounds(50,100,100,20);
  35. l1.setBounds(160,100,100,20);
  36. l2.setBounds(270,100,100,20);
  37. l3.setBounds(380,100,100,20);
  38. l4.setBounds(490,100,100,20);
  39. l5.setBounds(600,100,100,20);
  40. l6.setBounds(720,100,100,20);
  41.  
  42. t7.setBounds(50,140,100,20);
  43. t1.setBounds(160,140,100,20);
  44. t2.setBounds(270,140,100,20);
  45. t3.setBounds(380,140,100,20);
  46. t4.setBounds(490,140,100,20);
  47. t5.setBounds(600,140,100,20);
  48. t6.setBounds(710,140,100,20);
  49.  
  50. a1.setBounds(50,220,100,20);
  51.  
  52. b1.setBounds(50,180,60,20);
  53. b2.setBounds(160,180,60,20);
  54. b3.setBounds(270,180,60,20);
  55.  
  56. f.add(l1);
  57. f.add(l2);
  58. f.add(l3);
  59. f.add(l4);
  60. f.add(l5);
  61. f.add(l6);
  62. f.add(l7);
  63.  
  64. f.add(t1);
  65. f.add(t2);
  66. f.add(t3);
  67. f.add(t4);
  68. f.add(t5);
  69. f.add(t6);
  70. f.add(t7);
  71.  
  72. f.add(a1);
  73.  
  74. f.add(b1);
  75. f.add(b2);
  76. f.add(b3);
  77.  
  78. b1.addActionListener(this);
  79. b2.addActionListener(this);
  80. b3.addActionListener(this);
  81.  
  82. a1.setEditable(false);
  83.  
  84. f.setLayout(null);
  85. f.setVisible(true);
  86. f.setSize(1000,500);
  87. }
  88.  
  89. public void actionPerformed(ActionEvent e)
  90. { String z1 = null,sn = null,n = null,m = null,l = null,g = null,s = null,d = null,q = null;
  91.  
  92. if(e.getSource()==b1)
  93. {
  94. try{
  95. Connection con=getConnection();
  96. sn=String.valueOf(t7.getText());
  97. n=String.valueOf(t1.getText());
  98. if(sn.length()==0)
  99. { q ="fname";
  100. z1=n;
  101. sn = null;
  102. }
  103. else
  104. {
  105. q="ssn";
  106. z1=sn;
  107. n = null;
  108. }
  109. PreparedStatement statement = con.prepareStatement("SELECT * FROM employee WHERE "+q+" = '"+z1+"'");
  110. ResultSet result = statement.executeQuery();
  111. while(result.next()){
  112. n = result.getString("fname");
  113. m = result.getString("minit");
  114. l = result.getString("lname");
  115. g = result.getString("sex");
  116. s = result.getString("salary");
  117. d = result.getString("dno");
  118. sn = result.getString("ssn");
  119. }
  120. }
  121. catch (Exception e1){}
  122. t1.setText(String.valueOf(n));
  123. t2.setText(String.valueOf(m));
  124. t3.setText(String.valueOf(l));
  125. t4.setText(String.valueOf(g));
  126. t5.setText(String.valueOf(s));
  127. t6.setText(String.valueOf(d));
  128. t7.setText(String.valueOf(sn));
  129. if(d==null)
  130. a1.setText(String.valueOf("Not Found"));
  131. else
  132. a1.setText(String.valueOf("Found"));
  133. }
  134. if (e.getSource()==b2)
  135. {
  136. try{
  137. Connection con=getConnection();
  138. sn=String.valueOf(t7.getText());
  139. n=String.valueOf(t1.getText());
  140. m=String.valueOf(t2.getText());
  141. l=String.valueOf(t3.getText());
  142. g=String.valueOf(t4.getText());
  143. s=String.valueOf(t5.getText());
  144. d=String.valueOf(t6.getText());
  145. PreparedStatement posted = con.prepareStatement("INSERT INTO employee VALUES ('"+sn+"','"+n+"','"+m+"','"+l+"','"+g+"','"+d+"',"+s+")");
  146. posted.executeUpdate();
  147. a1.setText("Success");
  148. }
  149. catch(Exception e1){
  150. a1.setText("Fail");
  151. }
  152. }
  153. if (e.getSource()==b3)
  154. {
  155. System.exit(0);
  156. }
  157. }
  158.  
  159. public static void main(String args[])
  160. {
  161. new employee();
  162. }
  163. public static Connection getConnection() throws Exception{
  164. try{
  165. String driver = "com.mysql.jdbc.Driver";
  166. String url = "jdbc:mysql://localhost/company";
  167. String username = "root";
  168. String password = "kota1998";
  169. Class.forName(driver); Connection conn = DriverManager.getConnection(url,username,password);
  170. System.out.println("Connected \n\n");
  171. return conn;
  172. } catch(Exception e){System.out.println(e);}
  173. return null;
  174. }
  175. }
Add Comment
Please, Sign In to add comment