Guest User

CustomerRecords.java

a guest
Dec 2nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. package com.affinityjava.solidexample.service;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14. import com.affinityjava.solidexample.model.Customer;
  15. import com.opencsv.CSVWriter;
  16.  
  17. public class CustomerRecords implements ICustomerRecords {
  18.  
  19.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  20.     static final String DB_URL = "jdbc:mysql://localhost/H2DB";
  21.     static final String USER = "username";
  22.     static final String PASS = "password";
  23.  
  24.     @Override
  25.     public List<Customer> findAll() {
  26.         Connection conn = null;
  27.         Statement stmt = null;
  28.         List<Customer> customerList = new ArrayList<Customer>();
  29.         try {
  30.             Class.forName("com.mysql.jdbc.Driver");
  31.             conn = DriverManager.getConnection(DB_URL, USER, PASS);
  32.             stmt = conn.createStatement();
  33.             String sql;
  34.             sql = "SELECT id, customerLastName, customerFirstName, customerAddress FROM CUSTOMERS";
  35.             ResultSet rs = stmt.executeQuery(sql);
  36.             while (rs.next()) {
  37.                 Customer customer = new Customer();
  38.                 customer.setId(rs.getLong("id"));
  39.                 customer.setCustomerFirstName(rs.getString("customerFirstName"));
  40.                 customer.setCustomerLastName(rs.getString("customerLastName"));
  41.                 customer.setCustomerAddress(rs.getString("customerAddress"));
  42.                 customerList.add(customer);
  43.             }
  44.             rs.close();
  45.             stmt.close();
  46.             conn.close();
  47.         } catch (SQLException se) {
  48.             se.printStackTrace();
  49.         } catch (Exception e) {
  50.             e.printStackTrace();
  51.         } finally {
  52.             try {
  53.                 if (stmt != null)
  54.                     stmt.close();
  55.             } catch (SQLException se2) {
  56.             }
  57.             try {
  58.                 if (conn != null)
  59.                     conn.close();
  60.             } catch (SQLException se) {
  61.                 se.printStackTrace();
  62.             }
  63.         }
  64.         return customerList;
  65.     }
  66.  
  67.     @Override
  68.     public void findCustomersAndWriteToCSV() {
  69.  
  70.         String filePath = "C:\\Customer.csv";
  71.         File file = new File(filePath);
  72.         try {
  73.             FileWriter outputfile = new FileWriter(file);
  74.  
  75.             CSVWriter writer = new CSVWriter(outputfile);
  76.             String[] header = { "id", "first name", "last name", "address" };
  77.             writer.writeNext(header);
  78.             for(Customer customer: this.findAll()) {
  79.                 String[] data = new String[4];
  80.                 data[0] = String.valueOf(customer.getId());
  81.                 data[1] = customer.getCustomerFirstName();
  82.                 data[2] = customer.getCustomerLastName();
  83.                 data[3] = customer.getCustomerAddress();
  84.                 writer.writeNext(data);
  85.             }
  86.             writer.close();
  87.         } catch (IOException e) {
  88.             e.printStackTrace();
  89.         }
  90.  
  91.     }
  92.  
  93. }
Add Comment
Please, Sign In to add comment