|
| 1 | +package org.bee.sqlite.controllers; |
| 2 | + |
| 3 | +import org.bee.sqlite.helpers.DBConnectionProvider; |
| 4 | +import org.bee.sqlite.models.Customer; |
| 5 | +import java.sql.Connection; |
| 6 | +import java.sql.ResultSet; |
| 7 | +import java.sql.SQLException; |
| 8 | +import java.sql.Statement; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +/** |
| 13 | + * A class that handles database operations. |
| 14 | + * @author Sleiman R. |
| 15 | + */ |
| 16 | +public class CustomerController { |
| 17 | + |
| 18 | + private final String DATABASE_FILE_NAME = "chinook_sqlite.db"; |
| 19 | + private final String CUST_TABLE_NAME = "Customer"; |
| 20 | + //-- Column names |
| 21 | + private final String CUSTOMER_ID = "CustomerId"; |
| 22 | + private final String FIRSTNAME = "FirstName"; |
| 23 | + private final String LASTNAME = "LastName"; |
| 24 | + private final String COMPANY = "Company"; |
| 25 | + private final String ADDRESS = "Address"; |
| 26 | + private final String CITY = "City"; |
| 27 | + private final String STATE = "State"; |
| 28 | + private final String COUNTRY = "Country"; |
| 29 | + private final String POSTALCODE = "PostalCode"; |
| 30 | + private final String PHONE = "Phone"; |
| 31 | + private final String FAX = "Fax"; |
| 32 | + private final String EMAIL = "Email"; |
| 33 | + private final String SUPPORTREP_ID = "SupportRepId"; |
| 34 | + |
| 35 | + |
| 36 | + /** |
| 37 | + * Retrieves a list of customers stored in an SQLite database table. |
| 38 | + * |
| 39 | + * @see |
| 40 | + * {@link https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html} |
| 41 | + * @return the retrieved list of customers. |
| 42 | + */ |
| 43 | + public List<Customer> getCustomersList() { |
| 44 | + List<Customer> customers = new ArrayList<>(); |
| 45 | + String query = ""; |
| 46 | + //-- Step 1 & 2) Open a connection to the specified database |
| 47 | + //-- and create a prepared statement for executing SQL queries. |
| 48 | + try ( Connection dbConnection = DBConnectionProvider.getInstance().getConnection(DATABASE_FILE_NAME); Statement stmt = dbConnection.createStatement()) { |
| 49 | + query = String.format("SELECT * FROM %s ", CUST_TABLE_NAME); |
| 50 | + try ( ResultSet resSet = stmt.executeQuery(query)) { |
| 51 | + //-- Step 3) Process the list of records that are stored in the result set (in memory records). |
| 52 | + while (resSet.next()) { |
| 53 | + //-- Retreive values from the current record. |
| 54 | + Customer customer = new Customer(); |
| 55 | + customer.setCustomerId(resSet.getInt(CUSTOMER_ID)); |
| 56 | + customer.setFirstName(resSet.getString(FIRSTNAME)); |
| 57 | + customer.setLastName(resSet.getString(LASTNAME)); |
| 58 | + customer.setCompany(resSet.getString(COMPANY)); |
| 59 | + customer.setCountry(resSet.getString(COUNTRY)); |
| 60 | + customer.setPostalCode(resSet.getString(POSTALCODE)); |
| 61 | + customer.setPhone(resSet.getString(PHONE)); |
| 62 | + customer.setAddress(resSet.getString(ADDRESS)); |
| 63 | + customer.setCity(resSet.getString(CITY)); |
| 64 | + customer.setState(resSet.getString(STATE)); |
| 65 | + customer.setFax(resSet.getString(FAX)); |
| 66 | + customer.setEmail(resSet.getString(EMAIL)); |
| 67 | + customer.setSupporterId(resSet.getInt(SUPPORTREP_ID)); |
| 68 | + //-- Step 4) Add the retrieved customer from the result set to the list. |
| 69 | + customers.add(customer); |
| 70 | + } |
| 71 | + } |
| 72 | + } catch (SQLException ex) { |
| 73 | + System.err.println("An error has occured while trying to execute the following query: " + query); |
| 74 | + System.err.println("Error message: " + ex); |
| 75 | + } |
| 76 | + return customers; |
| 77 | + } |
| 78 | +} |
0 commit comments