Advertisement
Guest User

sin

a guest
Mar 22nd, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.*;
  3.  
  4. public class Controller
  5. {
  6.  
  7. private static Connection con;
  8.  
  9. public Controller()
  10. {
  11.  
  12. try
  13. {
  14. Class.forName("com.mysql.jdbc.Driver");
  15.  
  16. con = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "");
  17. }
  18. catch (Exception e)
  19. {
  20. System.out.println(e);
  21. }
  22.  
  23. }
  24.  
  25. public ArrayList<Customer> getAllCustomerDetails()
  26. {
  27.  
  28. ArrayList<Customer> customerList = new ArrayList<Customer>();
  29. try
  30. {
  31. PreparedStatement pst = con.prepareStatement("select * from Customer");
  32. ResultSet rs = pst.executeQuery();
  33.  
  34. while(rs.next())
  35. {
  36. Customer c = new Customer();
  37.  
  38. c.setCustomerId(rs.getInt("customer_id"));
  39. c.setCustomerName(rs.getString("customer_name"));
  40. c.setAddress(rs.getString("address"));
  41.  
  42. customerList.add(c);
  43. }
  44. }
  45. catch (Exception e)
  46. {
  47. System.out.println("Exception " + e);
  48. }
  49.  
  50. return customerList;
  51.  
  52. }
  53.  
  54. public void insertCustomer(int thatId, String thatName, String thatAddress){
  55.  
  56. try
  57. {
  58.  
  59. Statement st = con.createStatement();
  60.  
  61. PreparedStatement pst = con.prepareStatement("insert into customer values (?,?,?)");
  62.  
  63. Scanner sc = new Scanner(System.in);
  64.  
  65. pst.setInt(1,thatId);
  66. pst.setString(2,thatName);
  67. pst.setString(3,thatAddress);
  68.  
  69. int ret = pst.executeUpdate();
  70.  
  71. if (ret>0)
  72. {
  73. System.out.println(ret + " record created");
  74.  
  75. }
  76. else
  77. System.out.println("Error");
  78.  
  79. }
  80. catch (Exception e){
  81. System.out.println(e);
  82. }
  83.  
  84. }
  85.  
  86. public void updateCustomer(int thatId,String thatName ,String thatAddress){
  87.  
  88. try
  89. {
  90.  
  91. Statement st = con.createStatement();
  92.  
  93. PreparedStatement pst = con.prepareStatement("update customer set customer_name = ?, address = ? where customer_id = ?");
  94.  
  95. Scanner sc = new Scanner(System.in);
  96.  
  97.  
  98.  
  99. pst.setInt(3,thatId);
  100. pst.setString(1,thatName);
  101. pst.setString(2,thatAddress);
  102.  
  103. int ret = pst.executeUpdate();
  104.  
  105. if (ret>0)
  106. {
  107. System.out.println(ret + " record updated");
  108.  
  109. }
  110. else
  111. System.out.println("Error");
  112.  
  113. }
  114. catch (Exception e){
  115. System.out.println(e);
  116. }
  117.  
  118. }
  119.  
  120. public void deleteCustomer(int thatId){
  121.  
  122. try
  123. {
  124.  
  125. Statement st = con.createStatement();
  126.  
  127. PreparedStatement pst = con.prepareStatement("delete from customer where customer_id = ?");
  128.  
  129. Scanner sc = new Scanner(System.in);
  130.  
  131. pst.setInt(1,thatId);
  132.  
  133. boolean ret = pst.execute();
  134.  
  135. if (!ret)
  136. {
  137. System.out.println(ret + " record deleted");
  138.  
  139. }
  140. else
  141. System.out.println("Error");
  142.  
  143. }
  144. catch (Exception e){
  145. System.out.println(e);
  146. }
  147.  
  148. }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement