Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.35 KB | None | 0 0
  1. # To change this template, choose Tools | Templates
  2. # and open the template in the editor.
  3.  
  4. jdbc.drivers:   com.mysql.jdbc.Driver
  5. user:       gast
  6. password:   gast
  7. url:        jdbc:mysql://localhost/classicmodels
  8. getProducts:    select * from Products
  9. getOrders:      select * from Orders
  10.  
  11.  
  12.  
  13. -----------------------------------------
  14.  
  15. /*
  16.  * To change this template, choose Tools | Templates
  17.  * and open the template in the editor.
  18.  */
  19. package be.hogent.iii.cmodels.bo;
  20.  
  21. import be.hogent.iii.cmodels.excepties.JDBCExceptie;
  22. import be.hogent.iii.cmodels.interfaces.IDataStorage;
  23. import be.hogent.iii.cmodels.interfaces.IOrder;
  24. import be.hogent.iii.cmodels.interfaces.IOrderDetail;
  25. import be.hogent.iii.cmodels.interfaces.ICustomer;
  26. import be.hogent.iii.cmodels.interfaces.IProduct;
  27. import java.sql.CallableStatement;
  28. import java.sql.Connection;
  29. import java.sql.Date;
  30. import java.sql.DriverManager;
  31. import java.sql.PreparedStatement;
  32. import java.sql.ResultSet;
  33. import java.sql.SQLException;
  34. import java.sql.Statement;
  35. import java.text.DateFormat;
  36. import java.text.SimpleDateFormat;
  37. import java.util.ArrayList;
  38. import java.util.Enumeration;
  39. import java.util.List;
  40. import java.util.Properties;
  41. import java.util.PropertyResourceBundle;
  42. import java.util.ResourceBundle;
  43. import java.util.logging.Level;
  44. import java.util.logging.Logger;
  45.  
  46. /**
  47.  *
  48.  * @author kvdw
  49.  */
  50. public class JDBCDataStorage implements IDataStorage {
  51.  
  52.     private PropertyResourceBundle properties;
  53.  
  54.     private Connection maakConnectie() {
  55.         Connection conn = null;
  56.         properties = (PropertyResourceBundle) PropertyResourceBundle.getBundle("be.hogent.iii.cmodels.db");
  57.  
  58.         // Every example in the JDBC world has the jdbc.drivers property set at the system level.
  59.         Properties p = System.getProperties();
  60.         p.setProperty("jdbc.drivers", properties.getString("jdbc.drivers"));
  61.         System.setProperties(p);
  62.         try {
  63.             /* Instantiate the new driver.  This is the most reliable way to load a jdbc driver because some
  64.             implementations do not get completely initialized by the getConnection call in the DriverManager.
  65.             This forces the driver class to completely load and initialize a first new class instance.
  66.              */
  67.             System.out.println(properties.getString("jdbc.drivers"));
  68.             try {
  69.                 Object c = Class.forName(properties.getString("jdbc.drivers")).newInstance();
  70.             } catch (ClassNotFoundException ex) {
  71.                 Logger.getLogger(JDBCDataStorage.class.getName()).log(Level.SEVERE, null, ex);
  72.             }
  73.         } catch (InstantiationException ex) {
  74.             System.out.println(ex.getMessage());
  75.         } catch (IllegalAccessException ex) {
  76.             System.out.println(ex.getMessage());
  77.         }
  78.  
  79.         /* The JDBC driver has a number of connection creation calls.  Here we use the property version.  By
  80.         using the enumeration of the keys that the owner of the properties file has listed, we can make
  81.         this code sequence immune to the various different properties used by a particular JDBC driver
  82.         by copying each resource bundle property into a standard property class property list.
  83.          */
  84.         Properties dbp = new Properties();
  85.         for (Enumeration e = properties.getKeys(); e.hasMoreElements();) {
  86.             String prop = (String) e.nextElement();
  87.             dbp.setProperty(prop, properties.getString(prop));
  88.         }
  89.         try {
  90.             conn = DriverManager.getConnection(properties.getString("url"), dbp);
  91.         } catch (SQLException ex) {
  92.             System.out.println(ex.getMessage());
  93.         }
  94.  
  95.         return conn;
  96.     }
  97.  
  98.     public JDBCDataStorage() {
  99.     }
  100.  
  101.     private IProduct leesProduct(ResultSet result) {
  102.  
  103.         IProduct p;
  104.         String productCode, productName, productLine, productScale, productVendor, productDescription;
  105.         int quantityInStock;
  106.         double buyPrice, MSRP;
  107.         try {
  108.             productCode = result.getString("productCode");
  109.             productName = result.getString("productName");
  110.             productLine = result.getString("productLine");
  111.             productScale = result.getString("productScale");
  112.             productVendor = result.getString("productVendor");
  113.             productDescription = result.getString("productDescription");
  114.             quantityInStock = result.getInt("quantityInStock");
  115.             buyPrice = result.getDouble("buyPrice");
  116.             MSRP = result.getDouble("MSRP");
  117.             return new Product(productCode, productName, productLine, productScale, productVendor, productDescription,
  118.                     quantityInStock, buyPrice, MSRP);
  119.         } catch (SQLException ex) {
  120.             Logger.getLogger(JDBCDataStorage.class.getName()).log(Level.SEVERE, null, ex);
  121.             return null;
  122.         }
  123.  
  124.     }
  125.  
  126.     private IOrder leesOrder(ResultSet result) {
  127.         IOrder o = null;
  128.         int orderNumber, customerNumber;
  129.         Date orderDate, requiredDate, shippedDate;
  130.         String status, comments;
  131.         try {
  132.             orderNumber = result.getInt("orderNumber");
  133.             orderDate = result.getDate("orderDate");
  134.             requiredDate = result.getDate("requiredDate");
  135.             shippedDate = result.getDate("shippedDate");
  136.             status = result.getString("stauts");
  137.             comments = result.getString("comments");
  138.             customerNumber = result.getInt("customerNumber");
  139.  
  140.             List<IOrderDetail> details =  null; //details inlezen
  141.  
  142.             o = new Order(orderNumber, orderDate, requiredDate, shippedDate, status, comments, customerNumber,details);
  143.         } catch (SQLException ex) {
  144.             Logger.getLogger(JDBCDataStorage.class.getName()).log(Level.SEVERE, null, ex);
  145.         }
  146.         return o;
  147.  
  148.     }
  149.  
  150.     public List<IProduct> getProducts() throws JDBCExceptie {
  151.        
  152.        
  153.         //Connectie maken
  154.         Connection conn = maakConnectie();
  155.  
  156.  
  157.  
  158.         //Init
  159.         Statement st = null;
  160.         List<IProduct> lijst = new ArrayList<IProduct>();
  161.  
  162.  
  163.  
  164.         //Query
  165.         try {
  166.             st = conn.createStatement();
  167.  
  168.             try {
  169.                 ResultSet result = st.executeQuery(properties.getString("getProducts"));
  170.  
  171.                 result.first();
  172.                 lijst.add(leesProduct(result));
  173.                 while (result.next()) {
  174.                     lijst.add(leesProduct(result));
  175.  
  176.                 }
  177.  
  178.             } catch (SQLException ex) {
  179.                 System.out.println(ex.getMessage());
  180.             } finally {
  181.                 st.close();
  182.  
  183.             }
  184.         } catch (SQLException ex) {
  185.             System.out.println(ex.getMessage());
  186.         }
  187.  
  188.  
  189.  
  190.         //Verbindingen sluiten
  191.         try {
  192.             st.close();
  193.             conn.close();
  194.         } catch (SQLException ex) {
  195.             Logger.getLogger(JDBCDataStorage.class.getName()).log(Level.SEVERE, null, ex);
  196.         }
  197.  
  198.  
  199.         return lijst;
  200.     }
  201.  
  202.  
  203.  
  204.     public List<ICustomer> getCustomers() throws JDBCExceptie {
  205.         List<ICustomer> customers = null;
  206.  
  207.  
  208.  
  209.         return customers;
  210.     }
  211.  
  212.     //Lijst van bestellingen zonder details
  213.     public List<IOrder> getOrders() throws JDBCExceptie {
  214.         //Connectie
  215.         Connection conn = maakConnectie();
  216.  
  217.         //Init
  218.         List<IOrder> orders = null;
  219.         Statement st = null;
  220.  
  221.         //Query
  222.        try {
  223.             st = conn.createStatement();
  224.  
  225.             try {
  226.                 ResultSet result = st.executeQuery(properties.getString("getrders"));
  227.  
  228.                 result.first();
  229.                 orders.add(leesOrder(result));
  230.                 while (result.next()) {
  231.                     orders.add(leesOrder(result));
  232.  
  233.                 }
  234.  
  235.             } catch (SQLException ex) {
  236.                 System.out.println(ex.getMessage());
  237.             } finally {
  238.                 st.close();
  239.  
  240.             }
  241.         } catch (SQLException ex) {
  242.             System.out.println(ex.getMessage());
  243.         }
  244.  
  245.  
  246.  
  247.         //Verbindingen sluiten
  248.         try {
  249.             st.close();
  250.             conn.close();
  251.         } catch (SQLException ex) {
  252.             Logger.getLogger(JDBCDataStorage.class.getName()).log(Level.SEVERE, null, ex);
  253.         }
  254.  
  255.  
  256.         return orders;
  257.     }
  258.  
  259.     //Lijst van bestellingen van een gegeven klant (zonder details)
  260.     public List<IOrder> getOrders(int customerNumber) throws JDBCExceptie {
  261.         //Connectie
  262.         Connection conn = maakConnectie();
  263.        
  264.         //Init
  265.         List<IOrder> orders = null;
  266.         Statement st = null;
  267.  
  268.         //Query
  269.        try {
  270.             st = conn.createStatement();
  271.  
  272.             try {
  273.                 ResultSet result = st.executeQuery(properties.getString("getCustomerOrders"));
  274.  
  275.                 result.first();
  276.                 orders.add(leesOrder(result));
  277.                 while (result.next()) {
  278.                     orders.add(leesOrder(result));
  279.  
  280.                 }
  281.  
  282.             } catch (SQLException ex) {
  283.                 System.out.println(ex.getMessage());
  284.             } finally {
  285.                 st.close();
  286.  
  287.             }
  288.         } catch (SQLException ex) {
  289.             System.out.println(ex.getMessage());
  290.         }
  291.  
  292.  
  293.  
  294.         //Verbindingen sluiten
  295.         try {
  296.             st.close();
  297.             conn.close();
  298.         } catch (SQLException ex) {
  299.             Logger.getLogger(JDBCDataStorage.class.getName()).log(Level.SEVERE, null, ex);
  300.         }
  301.  
  302.  
  303.         return orders;
  304.     }
  305.  
  306.     public boolean addProduct(IProduct product) throws JDBCExceptie {
  307.         boolean resultaat = false;
  308.  
  309.  
  310.         return resultaat;
  311.     }
  312.  
  313.     public boolean addOrder(IOrder order) throws JDBCExceptie {
  314.         boolean resultaat = false;
  315.  
  316.         return resultaat;
  317.  
  318.     }
  319.  
  320.     public boolean addCustomer(ICustomer customer) throws JDBCExceptie {
  321.         boolean resultaat = false;
  322.  
  323.  
  324.  
  325.         return resultaat;
  326.     }
  327.  
  328.     public boolean modifyCustomer(ICustomer customer) throws JDBCExceptie {
  329.         boolean resultaat = false;
  330.  
  331.  
  332.  
  333.         return resultaat;
  334.     }
  335.  
  336.     public boolean deleteCustomer(int customerNumber) throws JDBCExceptie {
  337.         boolean resultaat = false;
  338.  
  339.  
  340.  
  341.         return resultaat;
  342.     }
  343.  
  344.     public double getTotal(int customerNumber) throws JDBCExceptie {
  345.         double resultaat = 0;
  346.  
  347.  
  348.  
  349.         return resultaat;
  350.     }
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement