SHARE
TWEET

Untitled

a guest Jun 19th, 2017 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package DB;
  2.  
  3. import Main.Main;
  4. import Model.*;
  5.  
  6. import java.sql.*;
  7. import java.time.LocalDateTime;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12.  
  13. import static Main.SingleRequest.Controller.Format;
  14.  
  15. /**
  16.  * Created by eee on 14.05.2017.
  17.  */
  18. public class Database {
  19.     private static final String IP = "Localhost";
  20.     private static final String PORT = "5432";
  21.     private static final String DB = "Delivery_Food";//mary92 //Delivery_Food
  22.  
  23.     private static final String USER = "postgres";
  24.     private static final String PASSWORD = "123"; //qwerty321 //123
  25.  
  26.     private static final String Category_TableName = "Category";
  27.     private static final String CategoryID_ColumnName = "category_id";
  28.     private static final String CategoryName_ColumnName = "category_name";
  29.  
  30.  
  31.     private static final String Client_TableName = "Clients";
  32.     private static final String ClientID_ColumnName = "client_id";
  33.     private static final String ClientName_ColumnName = "client_name";
  34.     private static final String ClientAddress_ColumnName = "address";
  35.     private static final String ClientPhone_ColumnName = "clientt_phone";
  36.     private static final String ClientRequestsCount_ColumnName = "requestcount";
  37.  
  38.  
  39.     private static final String Courier_TableName = "Couriers";
  40.     private static final String CourierID_ColumnName = "courier_id";
  41.     private static final String CourierName_ColumnName = "courier_name";
  42.     private static final String CourierIsWorking_ColumnName = "courier_status";
  43.     private static final String CourierPhone_ColumnName = "courier_phone";
  44.  
  45.  
  46.     private static final String Product_TableName = "Product";
  47.     private static final String ProductID_ColumnName = "product_id";
  48.     private static final String ProductCategory_ColumnName = "category_id";
  49.     private static final String ProductName_ColumnName = "product_name";
  50.     private static final String ProductConsist_ColumnName = "consist";
  51.     private static final String ProductPrice_ColumnName = "price";
  52.     private static final String ProductMeasure_ColumnName = "measure";
  53.  
  54.     private static final String Request_TableName = "Requests";
  55.     private static final String RequestID_ColumnName = "request_id";
  56.     private static final String RequestDate_ColumnName = "datec";
  57.     private static final String RequestClientID_ColumnName = "client_id";
  58.     private static final String RequestCourierID_ColumnName = "courier_id";
  59.     private static final String RequestDeliveryDate_ColumnName = "delivery_date";
  60.     private static final String RequestPrice_ColumnName = "summ";
  61.     private static final String RequestStatus_ColumnName = "status";
  62.  
  63.     private static final String Order_TableName = "Orders";
  64.     private static final String OrderID_ColumnName = "order_id";
  65.     private static final String OrderRequestID_ColumnName = "request_id";
  66.     private static final String OrderProductID_ColumnName = "product_id";
  67.     private static final String OrderCount_ColumnName = "quantity";
  68.  
  69.     static Map<Integer, Order> OrderMap = new HashMap<>();
  70.     static Map<Integer, Request> RequestMap = new HashMap<>();
  71.  
  72.     static Connection connection;
  73.     public static void Connect() throws SQLException {
  74.         connection = DriverManager
  75.                 .getConnection("jdbc:postgresql://" + IP + ":" + PORT + "/" + DB,
  76.                         USER, PASSWORD);
  77.         connection.setAutoCommit(false);
  78.     }
  79.  
  80.     public static List<Category> GetCategories() throws SQLException {
  81.         Statement stmt = connection.createStatement();
  82.         ResultSet rs = stmt.executeQuery( "SELECT * FROM " + Category_TableName + ";" );
  83.  
  84.         List<Category> l = new ArrayList<>();
  85.         while (rs.next()){
  86.             String name = rs.getString(CategoryName_ColumnName);
  87.             int id = rs.getInt(CategoryID_ColumnName);
  88.  
  89.             Category cat = new Category(id);
  90.             cat.setName(name);
  91.             l.add(cat);
  92.         }
  93.         rs.close();
  94.         stmt.close();
  95.         return l;
  96.     }
  97.  
  98.     public static void UpdateCategories(List<Category> categories) throws SQLException {
  99.         Statement stmt = connection.createStatement();
  100.  
  101.         for(Category car : categories){
  102.             stmt.executeUpdate("UPDATE " + Category_TableName + " SET " + CategoryName_ColumnName + " = '" + car.getName() + "' where " + CategoryID_ColumnName + " = " + car.getID());
  103.         }
  104.         connection.commit();
  105.         stmt.close();
  106.     }
  107.  
  108.     public static List<Category> AddCategories(List<Category> categories) throws SQLException {
  109.         Statement stmt = connection.createStatement();
  110.  
  111.         for(Category car : categories){
  112.             stmt.executeUpdate("INSERT INTO " + Category_TableName + " (" + CategoryName_ColumnName + ") VALUES ('" + car.getName() + "');", Statement.RETURN_GENERATED_KEYS);
  113.         }
  114.         ResultSet rs = stmt.getGeneratedKeys();
  115.         int i = 0;
  116.         if (rs != null && rs.next()) {
  117.             categories.get(i).setID(rs.getInt(1));
  118.  
  119.         }
  120.         connection.commit();
  121.         stmt.close();
  122.  
  123.         return categories;
  124.     }
  125.  
  126.     public static void RemoveCategories(List<Category> categories) throws SQLException {
  127.         Statement stmt = connection.createStatement();
  128.  
  129.         for(Category car : categories){
  130.             stmt.executeUpdate("DELETE FROM " + Category_TableName + " where " + CategoryID_ColumnName + " = " + car.getID());
  131.         }
  132.         connection.commit();
  133.         stmt.close();
  134.     }
  135.  
  136.  
  137.     public static List<Client> GetClients() throws SQLException {
  138.         Statement stmt = connection.createStatement();
  139.         ResultSet rs = stmt.executeQuery( "SELECT * FROM " + Client_TableName + ";" );
  140.  
  141.         List<Client> l = new ArrayList<>();
  142.         while (rs.next()){
  143.             String name = rs.getString(ClientName_ColumnName);
  144.             String address = rs.getString(ClientAddress_ColumnName);
  145.             String phone = rs.getString(ClientPhone_ColumnName);
  146.             int id = rs.getInt(ClientID_ColumnName);
  147.  
  148.             Client cat = new Client();
  149.             cat.setID(id);
  150.             cat.setName(name);
  151.             cat.setAddress(address);
  152.             cat.setPhone(phone);
  153.             l.add(cat);
  154.         }
  155.         rs.close();
  156.         stmt.close();
  157.         return l;
  158.     }
  159.  
  160.     public static void UpdateClients(List<Client> categories) throws SQLException {
  161.         Statement stmt = connection.createStatement();
  162.  
  163.         for(Client car : categories){
  164.             stmt.executeUpdate("UPDATE " + Client_TableName + " SET " +
  165.                     ClientRequestsCount_ColumnName + " = '" + car.getRequestsCount() + "', " +
  166.                     ClientName_ColumnName + " = '" + car.getName() + "', "+
  167.                     ClientAddress_ColumnName + "= '" + car.getAddress() + "', " +
  168.                     ClientPhone_ColumnName + "= '" + car.getPhone() + "' where " + ClientID_ColumnName + " = " + car.getID());
  169.         }
  170.         connection.commit();
  171.         stmt.close();
  172.     }
  173.  
  174.     public static List<Client> AddClients(List<Client> categories) throws SQLException {
  175.         Statement stmt = connection.createStatement();
  176.  
  177.         for(Client car : categories){
  178.             stmt.executeUpdate("INSERT INTO " + Client_TableName + " ("+ ClientRequestsCount_ColumnName + ", " + ClientName_ColumnName + ", " + ClientPhone_ColumnName + ", " + ClientAddress_ColumnName + ") VALUES ( " +
  179.                     "'" + car.getRequestsCount() + "', " +
  180.                     "'" + car.getName() + "', " +
  181.                     "'" + car.getPhone() + "',  " +
  182.                     "'" + car.getAddress() + "'" + " );", Statement.RETURN_GENERATED_KEYS);
  183.         }
  184.         ResultSet rs = stmt.getGeneratedKeys();
  185.         int i = 0;
  186.         if (rs != null && rs.next()) {
  187.             categories.get(i).setID(rs.getInt(1));
  188.  
  189.         }
  190.         connection.commit();
  191.         stmt.close();
  192.  
  193.         return categories;
  194.     }
  195.  
  196.     public static void RemoveClients(List<Client> categories) throws SQLException {
  197.         Statement stmt = connection.createStatement();
  198.  
  199.         for(Client car : categories){
  200.             stmt.executeUpdate("DELETE FROM " + Client_TableName + " where " + ClientID_ColumnName + " = " + car.getID());
  201.         }
  202.         connection.commit();
  203.         stmt.close();
  204.     }
  205.  
  206.  
  207.     public static List<Courier> GetCouriers() throws SQLException {
  208.         Statement stmt = connection.createStatement();
  209.         ResultSet rs = stmt.executeQuery( "SELECT * FROM " + Courier_TableName + ";" );
  210.  
  211.         List<Courier> l = new ArrayList<>();
  212.         while (rs.next()){
  213.             String name = rs.getString(CourierName_ColumnName);
  214.             Boolean isWorking = rs.getBoolean(CourierIsWorking_ColumnName);
  215.             String phone = rs.getString(CourierPhone_ColumnName);
  216.             int id = rs.getInt(CourierID_ColumnName);
  217.  
  218.             Courier cat = new Courier();
  219.             cat.setID(id);
  220.             cat.setName(name);
  221.             cat.isWorking = isWorking;
  222.             cat.setPhone(phone);
  223.             l.add(cat);
  224.         }
  225.         rs.close();
  226.         stmt.close();
  227.         return l;
  228.     }
  229.  
  230.     public static void UpdateCouriers(List<Courier> categories) throws SQLException {
  231.         Statement stmt = connection.createStatement();
  232.  
  233.         for(Courier car : categories){
  234.             stmt.executeUpdate("UPDATE " + Courier_TableName + " SET " +
  235.                     CourierName_ColumnName + " = '" + car.getName() + "', "+
  236.                     CourierIsWorking_ColumnName + "= '" + car.isWorking + "', " +
  237.                     CourierPhone_ColumnName + "= '" + car.getPhone() + "' where " + CourierID_ColumnName + " = " + car.getID());
  238.         }
  239.         connection.commit();
  240.         stmt.close();
  241.     }
  242.  
  243.     public static List<Courier> AddCouriers(List<Courier> categories) throws SQLException {
  244.         Statement stmt = connection.createStatement();
  245.  
  246.         for(Courier car : categories){
  247.             stmt.executeUpdate("INSERT INTO " + Courier_TableName + " (" + CourierName_ColumnName + ", " + CourierPhone_ColumnName + ", " + CourierIsWorking_ColumnName + ") VALUES ( " +
  248.                     "'" + car.getName() + "', " +
  249.                     "'" + car.getPhone() + "', " +
  250.                     "'" + car.isWorking + "'" + " );", Statement.RETURN_GENERATED_KEYS);
  251.         }
  252.         ResultSet rs = stmt.getGeneratedKeys();
  253.         int i = 0;
  254.         if (rs != null && rs.next()) {
  255.             categories.get(i).setID(rs.getInt(1));
  256.  
  257.         }
  258.         connection.commit();
  259.         stmt.close();
  260.  
  261.         return categories;
  262.     }
  263.  
  264.     public static void RemoveCouriers(List<Courier> categories) throws SQLException {
  265.         Statement stmt = connection.createStatement();
  266.  
  267.         for(Courier car : categories){
  268.             stmt.executeUpdate("DELETE FROM " + Courier_TableName + " where " + CourierID_ColumnName + " = " + car.getID());
  269.         }
  270.         connection.commit();
  271.         stmt.close();
  272.     }
  273.  
  274.     public static List<Product> GetProducts() throws SQLException {
  275.         Statement stmt = connection.createStatement();
  276.         ResultSet rs = stmt.executeQuery( "SELECT * FROM " + Product_TableName + ";" );
  277.  
  278.         List<Category> categories = GetCategories();
  279.         List<Product> l = new ArrayList<>();
  280.         while (rs.next()){
  281.             String name = rs.getString(ProductName_ColumnName);
  282.             int categoryid = rs.getInt(ProductCategory_ColumnName);
  283.             String consist = rs.getString(ProductConsist_ColumnName);
  284.             String measure = rs.getString(ProductMeasure_ColumnName);
  285.             double price = rs.getDouble(ProductPrice_ColumnName);
  286.             int id = rs.getInt(ProductID_ColumnName);
  287.  
  288.             Product cat = new Product();
  289.             cat.setID(id);
  290.             cat.setName(name);
  291.             cat.setConsist(consist);
  292.             try{
  293.                 cat.setMeasure(Measure.valueOf(measure));
  294.             }catch (Exception e) { }
  295.             cat.setPrice(price);
  296.             for (Category category : categories){
  297.                 if( category.getID() == categoryid ) {
  298.                     cat.setCategory(category);
  299.                     break;
  300.                 }
  301.             }
  302.             l.add(cat);
  303.         }
  304.         rs.close();
  305.         stmt.close();
  306.         return l;
  307.     }
  308.  
  309.     public static void UpdateProducts(List<Product> categories) throws SQLException {
  310.         Statement stmt = connection.createStatement();
  311.  
  312.         for(Product car : categories){
  313.             String additional = "";
  314.             if(car.getCategory() != null){
  315.                 additional += ProductCategory_ColumnName + "= '" + car.getCategory().getID() + "', ";
  316.             }
  317.             if(car.getMeasure() != null){
  318.                 additional += ProductMeasure_ColumnName + "= '" + car.getMeasure().name() + "', ";
  319.             }
  320.             stmt.executeUpdate("UPDATE " + Product_TableName + " SET " +
  321.                     ProductName_ColumnName + " = '" + car.getName() + "', "+
  322.                     ProductConsist_ColumnName + "= '" + car.getConsist() + "', " +
  323.                     additional +
  324.                     ProductPrice_ColumnName + "= '" + car.getPrice() + "'" +
  325.                     " where " + ProductID_ColumnName + " = " + car.getID());
  326.         }
  327.         connection.commit();
  328.         stmt.close();
  329.     }
  330.  
  331.     public static List<Product> AddProducts(List<Product> categories) throws SQLException {
  332.         Statement stmt = connection.createStatement();
  333.  
  334.         for(Product car : categories){
  335.             String additionalName = "";
  336.             String additional = "";
  337.  
  338.             if(car.getCategory() != null){
  339.                 additionalName += ProductCategory_ColumnName + ", ";
  340.                 additional += "'" + car.getCategory().getID() + "', ";
  341.             }
  342.             if(car.getMeasure() != null){
  343.                 additionalName += ProductMeasure_ColumnName + ", ";
  344.                 additional += "'" + car.getMeasure().name() + "', ";
  345.             }
  346.  
  347.             stmt.executeUpdate("INSERT INTO " + Product_TableName + " (" + ProductName_ColumnName + ", " + ProductConsist_ColumnName + ", " + additionalName + ProductPrice_ColumnName + ") VALUES ( " +
  348.                     "'" + car.getName() + "', " +
  349.                     "'" + car.getConsist() + "', " +
  350.                     additional +
  351.                     "'" + car.getPrice() + "'" + " );", Statement.RETURN_GENERATED_KEYS);
  352.         }
  353.         ResultSet rs = stmt.getGeneratedKeys();
  354.         int i = 0;
  355.         if (rs != null && rs.next()) {
  356.             categories.get(i).setID(rs.getInt(1));
  357.  
  358.         }
  359.         connection.commit();
  360.         stmt.close();
  361.  
  362.         return categories;
  363.     }
  364.  
  365.     public static void RemoveProducts(List<Product> categories) throws SQLException {
  366.         Statement stmt = connection.createStatement();
  367.  
  368.         for(Product car : categories){
  369.             stmt.executeUpdate("DELETE FROM " + Product_TableName + " where " + ProductID_ColumnName + " = " + car.getID());
  370.         }
  371.         connection.commit();
  372.         stmt.close();
  373.     }
  374.  
  375.     public static List<Request> GetRequests() throws SQLException { return GetRequests(null, null);}
  376.  
  377.     private static List<Request> GetRequests(List<Client> clientsO, List<Courier> couriersO) throws SQLException {
  378.         Statement stmt = connection.createStatement();
  379.         ResultSet rs = stmt.executeQuery( "SELECT * FROM " + Request_TableName + ";" );
  380.  
  381.         List<Client> clients;
  382.         if(clientsO == null) clients = GetClients();
  383.         else clients = clientsO;
  384.  
  385.         List<Courier> couriers;
  386.         if(couriersO == null) couriers = GetCouriers();
  387.         else couriers = couriersO;
  388.  
  389.         List<Request> l = new ArrayList<>();
  390.         while (rs.next()){
  391.             int id = rs.getInt(RequestID_ColumnName);
  392.  
  393.             Request cat;
  394.             if(!RequestMap.containsKey(id)){
  395.                 RequestMap.put(id, new Request());
  396.             }
  397.             cat = RequestMap.get(id);
  398.  
  399.             LocalDateTime date = LocalDateTime.parse(rs.getString(RequestDate_ColumnName), Format);
  400.             LocalDateTime deliveryDate = LocalDateTime.parse(rs.getString(RequestDeliveryDate_ColumnName), Format);
  401.             int clientID = rs.getInt(RequestClientID_ColumnName);
  402.             int courierID = rs.getInt(RequestCourierID_ColumnName);
  403.             double price = rs.getDouble(RequestPrice_ColumnName);
  404.             String status = rs.getString(RequestStatus_ColumnName);
  405.  
  406.             cat.setID(id);
  407.             cat.setDate(date);
  408.             cat.setDeliveryDate(deliveryDate);
  409.             try{
  410.                 cat.setStatus(RequestStatus.valueOf(status));
  411.             }catch (Exception e) { }
  412.             cat.setPrice(price);
  413.             for (Client client : clients){
  414.                 if( client.getID() == clientID ) {
  415.                     cat.setClient(client);
  416.                     break;
  417.                 }
  418.             }
  419.             for (Courier courier : couriers){
  420.                 if( courier.getID() == courierID ) {
  421.                     cat.setCourier(courier);
  422.                     break;
  423.                 }
  424.             }
  425.             l.add(cat);
  426.         }
  427.         rs.close();
  428.         stmt.close();
  429.         return l;
  430.     }
  431.  
  432.     public static void UpdateRequests(List<Request> categories) throws SQLException {
  433.         Statement stmt = connection.createStatement();
  434.  
  435.         for(Request car : categories){
  436.             String additional = "";
  437.             if(car.getClient() != null){
  438.                 additional += RequestClientID_ColumnName + "= '" + car.getClient().getID() + "', ";
  439.             }
  440.             if(car.getCourier() != null){
  441.                 additional += RequestCourierID_ColumnName + "= '" + car.getCourier().getID() + "', ";
  442.             }
  443.             if(car.getStatus() != null){
  444.                 additional += RequestStatus_ColumnName + "= '" + car.getStatus().name() + "', ";
  445.             }
  446.             stmt.executeUpdate("UPDATE " + Request_TableName + " SET " +
  447.                     RequestDeliveryDate_ColumnName + " = '" + car.getDeliveryDate().format(Format) + "', "+
  448.                     RequestDate_ColumnName + "= '" + car.getDate().format(Format) + "', " +
  449.                             additional +
  450.                     RequestPrice_ColumnName + "= '" + car.getPrice() + "' " +
  451.                      " where " + RequestID_ColumnName + " = " + car.getID());
  452.         }
  453.         connection.commit();
  454.         stmt.close();
  455.     }
  456.  
  457.     public static List<Request> AddRequests(List<Request> categories) throws SQLException {
  458.         Statement stmt = connection.createStatement();
  459.  
  460.         for(Request car : categories){
  461.             RequestMap.put(car.getID(), car);
  462.             String additionalName = "";
  463.             String additional = "";
  464.  
  465.             if(car.getClient() != null){
  466.                 additionalName += RequestClientID_ColumnName + ", ";
  467.                 additional += "'" + car.getClient().getID() + "', ";
  468.             }
  469.             if(car.getCourier() != null){
  470.                 additionalName += RequestCourierID_ColumnName + ", ";
  471.                 additional += "'" + car.getCourier().getID() + "', ";
  472.             }
  473.             if(car.getStatus() != null){
  474.                 additionalName += RequestStatus_ColumnName + ", ";
  475.                 additional += "'" + car.getStatus().name() + "', ";
  476.             }
  477.  
  478.             stmt.executeUpdate("INSERT INTO " + Request_TableName + " (" + RequestDate_ColumnName + ", " + RequestDeliveryDate_ColumnName + ", " + additionalName + RequestPrice_ColumnName + ") VALUES ( " +
  479.                     "'" + car.getDate().format(Format) + "', " +
  480.                     "'" + car.getDeliveryDate().format(Format) + "', " +
  481.                     additional +
  482.                     "'" + car.getPrice() + "'" + " );", Statement.RETURN_GENERATED_KEYS);
  483.         }
  484.         ResultSet rs = stmt.getGeneratedKeys();
  485.         int i = 0;
  486.         if (rs != null && rs.next()) {
  487.             categories.get(i).setID(rs.getInt(1));
  488.  
  489.         }
  490.         connection.commit();
  491.         stmt.close();
  492.  
  493.         return categories;
  494.     }
  495.  
  496.     public static void RemoveRequests(List<Request> categories) throws SQLException {
  497.         Statement stmt = connection.createStatement();
  498.  
  499.         for(Request car : categories){
  500.             stmt.executeUpdate("DELETE FROM " + Request_TableName + " where " + RequestID_ColumnName + " = " + car.getID());
  501.         }
  502.         connection.commit();
  503.         stmt.close();
  504.     }
  505.  
  506.  
  507.  
  508.     public static List<Order> GetOrders() throws SQLException {
  509.         Statement stmt = connection.createStatement();
  510.         ResultSet rs = stmt.executeQuery( "SELECT * FROM " + Order_TableName + ";" );
  511.  
  512.         List<Request> requests = GetRequests();
  513.         List<Product> products = GetProducts();
  514.         List<Order> l = new ArrayList<>();
  515.         while (rs.next()){
  516.             int id = rs.getInt(OrderID_ColumnName);
  517.  
  518.  
  519.             Order cat;
  520.             if(!OrderMap.containsKey(id)){
  521.                 OrderMap.put(id, new Order());
  522.             }
  523.             cat = OrderMap.get(id);
  524.  
  525.             int requestID = rs.getInt(OrderRequestID_ColumnName);
  526.             int productID = rs.getInt(OrderProductID_ColumnName);
  527.             int count = rs.getInt(OrderCount_ColumnName);
  528.  
  529.             cat.setID(id);
  530.             cat.setCount(count);
  531.  
  532.             for (Request request : requests){
  533.                 if( request.getID() == requestID ) {
  534.                     cat.setRequest(request);
  535.                     break;
  536.                 }
  537.             }
  538.             for (Product product : products){
  539.                 if( product.getID() == productID ) {
  540.                     cat.setProduct(product);
  541.                     break;
  542.                 }
  543.             }
  544.             l.add(cat);
  545.         }
  546.         rs.close();
  547.         stmt.close();
  548.         return l;
  549.     }
  550.  
  551.     public static void UpdateOrders(List<Order> categories) throws SQLException {
  552.         Statement stmt = connection.createStatement();
  553.  
  554.         for(Order car : categories){
  555.             String additional = "";
  556.             if(car.getRequest() != null){
  557.                 additional += OrderRequestID_ColumnName + "= '" + car.getRequest().getID() + "', ";
  558.             }
  559.             if(car.getProduct() != null){
  560.                 additional += OrderProductID_ColumnName + "= '" + car.getProduct().getID() + "', ";
  561.             }
  562.             stmt.executeUpdate("UPDATE " + Order_TableName + " SET " +
  563.                     additional +
  564.                     OrderCount_ColumnName + "= '" + car.getCount() + "' where " + OrderID_ColumnName + " = " + car.getID());
  565.         }
  566.         connection.commit();
  567.         stmt.close();
  568.     }
  569.  
  570.     public static List<Order> AddOrders(List<Order> categories) throws SQLException {
  571.         Statement stmt = connection.createStatement();
  572.  
  573.         for(Order car : categories){
  574.             OrderMap.put(car.getID(), car);
  575.             String additionalName = "";
  576.             String additional = "";
  577.  
  578.             if(car.getRequest() != null){
  579.                 additionalName += OrderRequestID_ColumnName + ", ";
  580.                 additional += "'" + car.getRequest().getID() + "', ";
  581.             }
  582.             if(car.getProduct() != null){
  583.                 additionalName += OrderProductID_ColumnName + ", ";
  584.                 additional += "'" + car.getProduct().getID() + "', ";
  585.             }
  586.  
  587.             stmt.executeUpdate("INSERT INTO " + Order_TableName + " (" + additionalName + OrderCount_ColumnName + ") VALUES ( " +
  588.                     additional +
  589.                     "'" + car.getCount() + "'" + " );", Statement.RETURN_GENERATED_KEYS);
  590.         }
  591.         ResultSet rs = stmt.getGeneratedKeys();
  592.         int i = 0;
  593.         if (rs != null && rs.next()) {
  594.             categories.get(i).setID(rs.getInt(1));
  595.  
  596.         }
  597.         connection.commit();
  598.         stmt.close();
  599.  
  600.         return categories;
  601.     }
  602.  
  603.     public static void RemoveOrders(List<Order> categories) throws SQLException {
  604.         Statement stmt = connection.createStatement();
  605.  
  606.         for(Order car : categories){
  607.             stmt.executeUpdate("DELETE FROM " + Order_TableName + " where " + OrderID_ColumnName + " = " + car.getID());
  608.         }
  609.         connection.commit();
  610.         stmt.close();
  611.     }
  612. }
RAW Paste Data
Top