Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. package core;
  7.  
  8. import com.mysql.jdbc.Connection;
  9. import com.mysql.jdbc.PreparedStatement;
  10. import com.mysql.jdbc.Statement;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.util.ArrayList;
  15. import java.util.Collection;
  16. import java.util.Collections;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19.  
  20. /**
  21. *
  22. * @author Rainbow
  23. */
  24. public class PaymentManagerImpl implements PaymentManager {
  25.  
  26.  
  27. private Connection conn;
  28. private static final Logger logger = Logger.getLogger(
  29. PaymentManagerImpl.class.getName());
  30.  
  31. public PaymentManagerImpl() {
  32. try {
  33. conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/Database1", "root", "");
  34.  
  35. } catch (SQLException ex) {
  36. logger.log(Level.SEVERE, "Error when connecting to DB", ex);
  37. throw new RuntimeException("Error when connecting to DB", ex);
  38. }
  39. }
  40.  
  41. public void createPayment(Payment payment1) {
  42. if (payment1 == null) {
  43. throw new IllegalArgumentException("grave");
  44. }
  45. if (payment1.getId() != null) {
  46. throw new IllegalArgumentException("grave id is already set");
  47. }
  48. if (payment1.getValue() < 0) {
  49. throw new IllegalArgumentException("grave capacity is negative");
  50. }
  51.  
  52. PreparedStatement st = null;
  53. try {
  54. st = (PreparedStatement) conn.prepareStatement(
  55. "INSERT INTO Payment (description,value, id ) VALUES (?,?,?)",
  56. Statement.RETURN_GENERATED_KEYS);
  57. st.setString(1, payment1.getDescription());
  58. st.setFloat(2, payment1.getValue());
  59. st.setLong(3, payment1.getId());
  60. int count = st.executeUpdate();
  61. assert count == 1;
  62.  
  63.  
  64.  
  65. } catch (SQLException ex) {
  66. logger.log(Level.SEVERE, "Error when inserting grave into DB", ex);
  67. throw new RuntimeException("Error when inserting into DB", ex);
  68. } finally {
  69. if (st != null) {
  70. try {
  71. st.close();
  72. } catch (SQLException ex) {
  73. logger.log(Level.SEVERE, "Error when closing connection", ex);
  74. }
  75. }
  76. }
  77.  
  78.  
  79. }
  80.  
  81.  
  82.  
  83. public ArrayList<Payment> getAllPayments(long accountId) {
  84. PreparedStatement st = null;
  85. try {
  86. st = (PreparedStatement) conn.prepareStatement("SELECT * from payment");
  87. ResultSet rs = st.executeQuery();
  88. ArrayList<Payment> found = new ArrayList<Payment>();
  89. while (rs.next()) {
  90. Payment result = new Payment();
  91. result.setDescription(rs.getString("Description"));
  92. result.setDueDate(rs.getDate("Date"));
  93. result.setSenderAccount(rs.getLong("Sender Account"));
  94. result.setReceiverAccount(rs.getLong("Receiver Account"));
  95. result.setValue(rs.getBigDecimal("value"));
  96. found.add(result);
  97. }
  98. return (ArrayList<Payment>) Collections.unmodifiableCollection(found);
  99. } catch (SQLException ex) {
  100. logger.log(Level.SEVERE, "Error", ex);
  101. throw new RuntimeException("Error", ex);
  102. } finally {
  103. if (st != null) {
  104. try {
  105. st.close();
  106. } catch (SQLException ex) {
  107. logger.log(Level.SEVERE, "Error when closing connection", ex);
  108. }
  109. }
  110. }
  111.  
  112.  
  113. }
  114.  
  115.  
  116. public Payment getPayment(Long accountId) {
  117. if (accountId == null) {
  118. throw new IllegalArgumentException("id");
  119. }
  120. PreparedStatement st = null;
  121. try {
  122. st = (PreparedStatement) conn.prepareStatement(
  123. "SELECT id, Description,Date,Sender acount, Receiver Account, value FROM Payment WHERE ID = ?");
  124. st.setLong(1, accountId);
  125.  
  126. ResultSet rs = st.executeQuery();
  127.  
  128. if (rs.next()) {
  129. Payment result = new Payment();
  130. result.setDescription(rs.getString("Description"));
  131. result.setDueDate(rs.getDate("Date"));
  132. result.setSenderAccount(rs.getLong("Sender Account"));
  133. result.setReceiverAccount(rs.getLong("Receiver Account"));
  134. result.setValue(rs.getBigDecimal("value"));
  135. assert !rs.next();
  136. return result;
  137. } else {
  138. return null;
  139. }
  140.  
  141. } catch (SQLException ex) {
  142. logger.log(Level.SEVERE, "Error", ex);
  143. throw new RuntimeException("Error", ex);
  144. } finally {
  145. if (st != null) {
  146. try {
  147. st.close();
  148. } catch (SQLException ex) {
  149. logger.log(Level.SEVERE, "Error when closing connection", ex);
  150. }
  151. }
  152. }
  153. }
  154.  
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement