Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. //get a menu page to create, update, delete, view and exit db
  2.  
  3. import java.sql.*;
  4. import java.util.*;
  5. public class ViewTest2
  6. {
  7. public static void main(String[] args)
  8. {
  9. boolean b = true;
  10. while (b == true)
  11. {
  12.  
  13.  
  14. //Create an object of controller class
  15. Controller ctrl = new Controller(); // invokes the default constructor from Controller class
  16.  
  17.  
  18. System.out.println(" ");
  19. System.out.println(" ");
  20. System.out.println("***** Menu *****");
  21. System.out.println(" ");
  22. System.out.println("1. Create Record");
  23. System.out.println(" ");
  24. System.out.println("2. Update Record");
  25. System.out.println(" ");
  26. System.out.println("3. Delete Record");
  27. System.out.println(" ");
  28. System.out.println("4. View Customer Details");
  29. System.out.println(" ");
  30. System.out.println("5. Exit Menu");
  31. System.out.println(" ");
  32. System.out.println(" ");
  33.  
  34.  
  35. Scanner sc1 = new Scanner(System.in);
  36. System.out.println("Please select one of the options: ");
  37. int a = sc1.nextInt();
  38.  
  39.  
  40. if (a == 1){
  41. try
  42. {
  43. //step 1
  44. Class.forName("com.mysql.jdbc.Driver");
  45.  
  46. //step 2
  47. Connection con = DriverManager.getConnection("jdbc:mysql://localhost/samdb","root","");
  48.  
  49. //step 3
  50. //Statement st = con.createStatement();
  51.  
  52. PreparedStatement pst = con.prepareStatement("insert into customer values(?,?,?)");
  53.  
  54. Scanner sc = new Scanner(System.in);
  55.  
  56. System.out.println("Enter customer id: ");
  57.  
  58. int id = Integer.parseInt(sc.nextLine());
  59. System.out.println("Enter Customer Name: ");
  60. String name = sc.nextLine();
  61. System.out.println("Enter Customer Address: ");
  62. String address = sc.nextLine();
  63.  
  64. pst.setInt(1,id);
  65. pst.setString(2,name);
  66. pst.setString(3,address);
  67.  
  68. int i = pst.executeUpdate();
  69.  
  70. if(i>0)
  71. {
  72. System.out.println(i+" record created");
  73. }
  74. else
  75. {
  76. System.out.println("Error");
  77. }
  78. con.close();
  79.  
  80. //step 4
  81. //st.execute("create table customer2(customer_id int,customer_name varchar(30),address varchar(30))");
  82. //System.out.println("table created");
  83.  
  84. //step 5
  85. //st.close();
  86. //con.close();
  87.  
  88. }
  89. catch (Exception e)
  90. {
  91. System.out.println(e);
  92. }
  93. }
  94. else if (a == 2)
  95. {
  96. try
  97. {
  98. //step 1
  99. Class.forName("com.mysql.jdbc.Driver");
  100.  
  101.  
  102. Connection con = DriverManager.getConnection("jdbc:mysql://localhost/alydb","root","");
  103.  
  104. //step 3
  105. //Statement st = con.createStatement();
  106.  
  107. PreparedStatement pst = con.prepareStatement("UPDATE customer SET customer_name = ?, address = ? where customer_id = ?");
  108.  
  109. Scanner sc2 = new Scanner(System.in);
  110.  
  111. //Step 3
  112.  
  113. System.out.println("Enter customer id to Update: ");
  114.  
  115. int id = Integer.parseInt(sc2.nextLine());
  116. System.out.println("Enter New Customer Name: ");
  117. String name = sc2.nextLine();
  118. System.out.println("Enter New Customer Address: ");
  119. String address = sc2.nextLine();
  120.  
  121.  
  122. pst.setString(1,name);
  123. pst.setString(2,address);
  124. pst.setInt(3,id);
  125.  
  126. //Step 4
  127.  
  128. int i = pst.executeUpdate();
  129.  
  130. if(i>0)
  131. {
  132. System.out.println(i+" record updated");
  133. }
  134. else
  135. {
  136. System.out.println("Error");
  137. }
  138. con.close();
  139.  
  140.  
  141. }
  142. catch (Exception e)
  143. {
  144. System.out.println(e);
  145. }
  146. }
  147.  
  148. else if (a == 3)
  149. {
  150. try
  151. {
  152. //step 1
  153. Class.forName("com.mysql.jdbc.Driver");
  154.  
  155.  
  156. Connection con = DriverManager.getConnection("jdbc:mysql://localhost/alydb","root","");
  157.  
  158. //step 3
  159. //Statement st = con.createStatement();
  160.  
  161. PreparedStatement pst = con.prepareStatement("DELETE from customer where customer_id = ?");
  162.  
  163. Scanner sc = new Scanner(System.in);
  164.  
  165. //Step 3
  166.  
  167. System.out.println("Enter customer id to delete: ");
  168.  
  169. int id = Integer.parseInt(sc.nextLine());
  170.  
  171. pst.setInt(1,id);
  172.  
  173. //Step 4
  174.  
  175. int i = pst.executeUpdate();
  176.  
  177. if(i>0)
  178. {
  179. System.out.println(i+" record deleted");
  180. }
  181. else
  182. {
  183. System.out.println("Error");
  184. }
  185. con.close();
  186.  
  187.  
  188. }
  189. catch (Exception e)
  190. {
  191. System.out.println(e);
  192. }
  193. }
  194.  
  195. else if (a == 4)
  196. {
  197. //Create an object of controller class
  198. Controller ctrl1 = new Controller(); // invokes the default constructor from Controller class
  199.  
  200. ArrayList<Customer> customerList = ctrl1.getAllCustomerDetails();
  201.  
  202. // for-each loop
  203. System.out.println("***********************************************");
  204. for (Customer c : customerList)
  205. {
  206. System.out.println("Customer ID : "+c.getId());
  207. System.out.println("Customer Name : "+c.getName());
  208. System.out.println("Customer Address : "+c.getAddress());
  209.  
  210. System.out.println("*******************************************");
  211. }
  212. }
  213.  
  214. else if (a == 5)
  215. {
  216. System.out.println(" ");
  217. System.out.println("Goodbye!");
  218. System.out.println(" ");
  219. b = false;
  220. }
  221.  
  222. else
  223. {
  224. System.out.println(" ");
  225. System.out.println("Incorrect input, Please try again!");
  226. System.out.println(" ");
  227. }
  228. }
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement