Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package core;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.PreparedStatement;
- import com.mysql.jdbc.Statement;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Rainbow
- */
- public class PaymentManagerImpl implements PaymentManager {
- private Connection conn;
- private static final Logger logger = Logger.getLogger(
- PaymentManagerImpl.class.getName());
- public PaymentManagerImpl() {
- try {
- conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/Database1", "root", "");
- } catch (SQLException ex) {
- logger.log(Level.SEVERE, "Error when connecting to DB", ex);
- throw new RuntimeException("Error when connecting to DB", ex);
- }
- }
- public void createPayment(Payment payment1) {
- if (payment1 == null) {
- throw new IllegalArgumentException("grave");
- }
- if (payment1.getId() != null) {
- throw new IllegalArgumentException("grave id is already set");
- }
- if (payment1.getValue() < 0) {
- throw new IllegalArgumentException("grave capacity is negative");
- }
- PreparedStatement st = null;
- try {
- st = (PreparedStatement) conn.prepareStatement(
- "INSERT INTO Payment (description,value, id ) VALUES (?,?,?)",
- Statement.RETURN_GENERATED_KEYS);
- st.setString(1, payment1.getDescription());
- st.setFloat(2, payment1.getValue());
- st.setLong(3, payment1.getId());
- int count = st.executeUpdate();
- assert count == 1;
- } catch (SQLException ex) {
- logger.log(Level.SEVERE, "Error when inserting grave into DB", ex);
- throw new RuntimeException("Error when inserting into DB", ex);
- } finally {
- if (st != null) {
- try {
- st.close();
- } catch (SQLException ex) {
- logger.log(Level.SEVERE, "Error when closing connection", ex);
- }
- }
- }
- }
- public ArrayList<Payment> getAllPayments(long accountId) {
- PreparedStatement st = null;
- try {
- st = (PreparedStatement) conn.prepareStatement("SELECT * from payment");
- ResultSet rs = st.executeQuery();
- ArrayList<Payment> found = new ArrayList<Payment>();
- while (rs.next()) {
- Payment result = new Payment();
- result.setDescription(rs.getString("Description"));
- result.setDueDate(rs.getDate("Date"));
- result.setSenderAccount(rs.getLong("Sender Account"));
- result.setReceiverAccount(rs.getLong("Receiver Account"));
- result.setValue(rs.getBigDecimal("value"));
- found.add(result);
- }
- return (ArrayList<Payment>) Collections.unmodifiableCollection(found);
- } catch (SQLException ex) {
- logger.log(Level.SEVERE, "Error", ex);
- throw new RuntimeException("Error", ex);
- } finally {
- if (st != null) {
- try {
- st.close();
- } catch (SQLException ex) {
- logger.log(Level.SEVERE, "Error when closing connection", ex);
- }
- }
- }
- }
- public Payment getPayment(Long accountId) {
- if (accountId == null) {
- throw new IllegalArgumentException("id");
- }
- PreparedStatement st = null;
- try {
- st = (PreparedStatement) conn.prepareStatement(
- "SELECT id, Description,Date,Sender acount, Receiver Account, value FROM Payment WHERE ID = ?");
- st.setLong(1, accountId);
- ResultSet rs = st.executeQuery();
- if (rs.next()) {
- Payment result = new Payment();
- result.setDescription(rs.getString("Description"));
- result.setDueDate(rs.getDate("Date"));
- result.setSenderAccount(rs.getLong("Sender Account"));
- result.setReceiverAccount(rs.getLong("Receiver Account"));
- result.setValue(rs.getBigDecimal("value"));
- assert !rs.next();
- return result;
- } else {
- return null;
- }
- } catch (SQLException ex) {
- logger.log(Level.SEVERE, "Error", ex);
- throw new RuntimeException("Error", ex);
- } finally {
- if (st != null) {
- try {
- st.close();
- } catch (SQLException ex) {
- logger.log(Level.SEVERE, "Error when closing connection", ex);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement