Guest User

Untitled

a guest
Nov 25th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. package customermanager;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.util.ArrayList;
  14.  
  15.  
  16. public class CustomerDB {
  17.  
  18.  
  19. public static List<Customer> getAll(){
  20. String sql = "SELECT * FROM Customer ORDER BY LastName";
  21. List<Customer> customer = newArrayList<>();
  22. Connection connection = DBUtil.getConnection();
  23. try (PreparedStatement ps = connection.prepareStatement(sql);
  24. ResultSet rs = ps.executeQuery()) {
  25. while(rs.next()) {
  26. String firstName = rs.getString("FirstName");
  27. String lastName = rs.getString("LastName");
  28. String email = rs.getString("EmailAddress");
  29.  
  30. Customer c = new Customer();
  31. c.setFirstName(firstName);
  32. c.setLastName(lastName);
  33. c.setEmail(email);
  34. }
  35. return customer;
  36. } catch(SQLException e) {
  37. } return customer;
  38. }
  39. public static Customer get(String EmailAddress) {
  40. String sql = "SELECT * FROM Customer WHERE EmailAddress = ?";
  41. Connection connection = DBUtil.getConnection();
  42. try(PreparedStatement ps = connection.prepareStatement(sql)) {
  43. ps.setString(1, EmailAddress);
  44. ResultSet rs = ps.executeQuery();
  45. if(rs.next()) {
  46. String email = rs.getString("EmailAddress");
  47. String firstName = rs.getString("FirstName");
  48. String lastName = rs.getString("LastName");
  49. rs.close();
  50.  
  51. Customer c = new Customer();
  52. c.setEmail(email);
  53. c.setFirstName(firstName);
  54. c.setLastName(lastName);
  55.  
  56. return c;
  57. } else {
  58. rs.close();
  59. return null;
  60. }
  61. } catch(SQLException e){};
  62. return null;
  63.  
  64. }
  65. public static void add(Customer customer) {
  66. String sql = "INSERT INTO Customer (FirstName, LastName, EmailAddress) " +
  67. "VALUES (?, ?, ?)";
  68. Connection connection = DBUtil.getConnection();
  69. try(PreparedStatement ps = connection.prepareStatement(sql)) {
  70. ps.setString(1, customer.getFirstName());
  71. ps.setString(2,customer.getLastName());
  72. ps.setString(3, customer.getEmail());
  73. ps.executeUpdate();
  74. } catch(SQLException e){}
  75. }
  76.  
  77. public static void delete(Customer customer) {
  78. String sql = "DELETE FROM Customer " +
  79. "WHERE EmailAddress = ?";
  80. Connection connection = DBUtil.getConnection();
  81. try(PreparedStatement ps = connection.prepareStatement(sql)) {
  82. customer.setEmail(customer.getEmail());
  83. ps.executeUpdate();
  84. } catch(SQLException e){};
  85. }
  86. public class DBUtil() {
  87. private DBUtil(){}
  88. private static Connection connection;
  89. public static synchronized Connection getConnection() {
  90. if (connection != null) {
  91. return connection;
  92. } else {
  93. try {
  94. String url = "jdbc:mysql://localhost:3306/mma";
  95. String username = "mma_user";
  96. String password = "sesame";
  97.  
  98. connection = DriverManager.getConnection(url, username, password);
  99. return connection;
  100. } catch(SQLException e){
  101. }
  102. } return null;
  103. }
  104. public static synchronized void closeConnection() {
  105. if(connection != null) {
  106. try {
  107. connection.close();
  108. } catch(SQLException e) {
  109. } finally {
  110. connection = null;
  111. }
  112. }
  113. }
  114. }
  115. }
Add Comment
Please, Sign In to add comment