mnj83

slip 20 q2

Mar 17th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. Q2-phonebook
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.sql.*;
  6. class PhoneBook extends JFrame implements ActionListener
  7. {
  8. JLabel l1,l2,l3,l4;
  9. JTextField tf1,tf2,tf3,tf4;
  10. JButton b1,b2,b3,b4;
  11.  
  12.  
  13. Connection con;
  14. PreparedStatement ps;
  15. Statement stmt;
  16. ResultSet rs;
  17.  
  18.  
  19.  
  20. PhoneBook()
  21. {
  22.  
  23. try
  24. {
  25. Class.forName("org.postgresql.Driver");
  26. con=DriverManager.getConnection("jdbc:postgresql://localhost/tydb","root","");
  27. if(con==null)
  28. {
  29. JOptionPane.showMessageDialog(null,"Unable to connect database : ");
  30. System.exit(0);
  31. }
  32. stmt=con.createStatement();
  33. rs=stmt.executeQuery("select * from phone");
  34.  
  35.  
  36. }
  37. catch(Exception e)
  38. {
  39.  
  40. JOptionPane.showMessageDialog(null,e);
  41. }
  42.  
  43.  
  44.  
  45. setLayout(null);
  46. l1=new JLabel("Name:");
  47. l1.setSize(100,30);
  48. l1.setLocation(10,30);
  49. add(l1);
  50.  
  51. tf1=new JTextField();
  52. tf1.setSize(200,30);
  53. tf1.setLocation(150,30);
  54. add(tf1);
  55.  
  56. b1=new JButton("ADD");
  57. b1.addActionListener(this);
  58. b1.setSize(150,30);
  59. b1.setLocation(400,30);
  60. add(b1);
  61.  
  62.  
  63. l2=new JLabel("Address:");
  64. l2.setSize(100,30);
  65. l2.setLocation(10,80);
  66. add(l2);
  67.  
  68. tf2=new JTextField();
  69. tf2.setSize(200,30);
  70. tf2.setLocation(150,80);
  71. add(tf2);
  72.  
  73. b2=new JButton("DELETE");
  74. b2.addActionListener(this);
  75. b2.setSize(150,30);
  76. b2.setLocation(400,80);
  77. add(b2);
  78.  
  79.  
  80.  
  81. l3=new JLabel("Phone");
  82. l3.setSize(100,30);
  83. l3.setLocation(10,130);
  84. add(l3);
  85.  
  86. tf3=new JTextField();
  87. tf3.setSize(200,30);
  88. tf3.setLocation(150,130);
  89. add(tf3);
  90.  
  91. b3=new JButton("NEXT");
  92. b3.addActionListener(this);
  93. b3.setSize(150,30);
  94. b3.setLocation(400,130);
  95. add(b3);
  96.  
  97.  
  98.  
  99. l4=new JLabel("Email:");
  100. l4.setSize(100,30);
  101. l4.setLocation(10,180);
  102. add(l4);
  103.  
  104. tf4=new JTextField();
  105. tf4.setSize(200,30);
  106. tf4.setLocation(150,180);
  107. add(tf4);
  108.  
  109. b4=new JButton("PREVIOUS");
  110. b4.addActionListener(this);
  111. b4.setSize(150,30);
  112. b4.setLocation(400,180);
  113. add(b4);
  114.  
  115. setTitle("Q.20 Phone Book Demo");
  116. setSize(600,500);
  117. setVisible(true);
  118. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  119.  
  120. }
  121. public void actionPerformed(ActionEvent ae)
  122. {
  123. String s=(String)ae.getActionCommand();
  124. if(s.equals("ADD"))
  125. {
  126. String name=tf1.getText();
  127. String address=tf2.getText();
  128. String phone=tf3.getText();
  129. String email=tf4.getText();
  130.  
  131. if(name.length()==0 || address.length()==0||phone.length()==0||email.length()==0)
  132. {
  133. JOptionPane.showMessageDialog(null,"Can not add data because few Fields are empty");
  134. return;
  135. }
  136.  
  137. if(phone.charAt(0)=='-')
  138. {
  139. JOptionPane.showMessageDialog(null,"Can not add data because phone number field is negative");
  140. return;
  141. }
  142.  
  143. try
  144. {
  145. ps=con.prepareStatement("insert into phone values(?,?,?,?)");
  146. ps.setString(1,name);
  147. ps.setString(2,address);
  148. ps.setString(3,phone);
  149. ps.setString(4,email);
  150. ps.executeUpdate();
  151. JOptionPane.showMessageDialog(null,"Record is added Succesfully....");
  152. tf1.setText("");
  153. tf2.setText("");
  154. tf3.setText("");
  155. tf4.setText("");
  156.  
  157.  
  158. stmt=con.createStatement();
  159. rs=stmt.executeQuery("select * from phone");
  160.  
  161.  
  162. }
  163. catch(Exception e)
  164. {
  165. JOptionPane.showMessageDialog(null,"Problem While Inserting Record...."+e);
  166.  
  167. }
  168. }//ADD
  169.  
  170.  
  171. if(s.equals("DELETE"))
  172. {
  173. String name=tf1.getText();
  174. String address=tf2.getText();
  175. String phone=t
Add Comment
Please, Sign In to add comment