Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 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. public Payment getPayment(long accountId) {
  82. throw new UnsupportedOperationException("Not supported yet.");
  83. }
  84.  
  85. public ArrayList<Payment> getAllPayments(long accountId) {
  86. PreparedStatement st = null;
  87. try {
  88. st = (PreparedStatement) conn.prepareStatement("SELECT * from grave");
  89. ResultSet rs = st.executeQuery();
  90. ArrayList<Payment> found = new ArrayList<Payment>();
  91. while (rs.next()) {
  92. Payment result = new Payment();
  93. result.setDescription(rs.getString("Description"));
  94. result.setDueDate(rs.getDate("Date"));
  95. result.setSenderAccount(rs.getLong("Sender Account"));
  96. result.setReceiverAccount(rs.getLong("Receiver Account"));
  97. result.setValue(rs.getBigDecimal("value"));
  98. found.add(result);
  99. }
  100. return (ArrayList<Payment>) Collections.unmodifiableCollection(found);
  101. } catch (SQLException ex) {
  102. logger.log(Level.SEVERE, "Error", ex);
  103. throw new RuntimeException("Error", ex);
  104. } finally {
  105. if (st != null) {
  106. try {
  107. st.close();
  108. } catch (SQLException ex) {
  109. logger.log(Level.SEVERE, "Error when closing connection", ex);
  110. }
  111. }
  112. }
  113.  
  114.  
  115. }
  116.  
  117.  
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement