daily pastebin goal
58%
SHARE
TWEET

Untitled

a guest May 27th, 2018 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import com.mysql.fabric.jdbc.FabricMySQLDriver;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.StringTokenizer;
  7.  
  8. public class OrderDAOImpl implements OrderDAO {
  9.     private static final String URL = "jdbc:mysql://localhost:3306/mydb";
  10.     private static final String USERNAME = "root";
  11.     private static final String PASSWORD = "root";
  12.  
  13.     private static final String INSERT_ORDER =
  14.             "insert into Patients (amount,price,name) values(?,?,?);";
  15.     private static final String DELETE_ALL = "delete from Orders";
  16.     private static final String PRINT_ALL = "select * from Orders;";
  17.  
  18.     public OrderDAOImpl() {
  19.         super();
  20.         try {
  21.             Driver driver = new FabricMySQLDriver();
  22.             DriverManager.registerDriver(driver);
  23.         } catch (SQLException e) {
  24.  
  25.         }
  26.     }
  27.  
  28.     @Override
  29.     public List<Order> getOrderList() {
  30.         ArrayList<Order> orders = new ArrayList<>();
  31.         try {
  32.             Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
  33.             try {
  34.                 PreparedStatement preparedStatement = connection.prepareStatement(PRINT_ALL);
  35.                 ResultSet resultSet = preparedStatement.executeQuery();
  36.                 while (resultSet.next()) {
  37.                     String amountList = resultSet.getString(2);
  38.                     String priceList = resultSet.getString(3);
  39.                     String nameList = resultSet.getString(4);
  40.                     List<Product> productList = getProductList(amountList, priceList, nameList);
  41.                     orders.add(new Order(productList));
  42.                 }
  43.                 return orders;
  44.             } catch (SQLException e) {
  45.                 connection.close();
  46.             }
  47.         } catch (SQLException e) {
  48.         }
  49.         return orders;
  50.     }
  51.  
  52.     private List<Product> getProductList(String amountList, String priceList, String nameList) {
  53.         StringTokenizer aTok = new StringTokenizer(amountList, ",");
  54.         StringTokenizer pTok = new StringTokenizer(priceList, ",");
  55.         StringTokenizer nTok = new StringTokenizer(nameList, ",");
  56.         List<Product> productList = new ArrayList<>();
  57.         while (aTok.hasMoreTokens()) {
  58.             int amount = Integer.parseInt(aTok.nextToken());
  59.             int price = Integer.parseInt(pTok.nextToken());
  60.             String name = nTok.nextToken();
  61.             productList.add(new Product(amount, price, name));
  62.         }
  63.         return productList;
  64.     }
  65.  
  66.     @Override
  67.     public void setOrderList(List<Order> orderList) {
  68.         try {
  69.             Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
  70.             try {
  71.                 PreparedStatement preparedStatement = connection.prepareStatement(DELETE_ALL);
  72.                 preparedStatement.executeUpdate();
  73.                 for(Order order: orderList) {
  74.                     List<String> productStrings = convertProductList(order.getProductList());
  75.                     preparedStatement = connection.prepareStatement(INSERT_ORDER);
  76.                     preparedStatement.setString(1,productStrings.get(0));
  77.                     preparedStatement.setString(2,productStrings.get(1));
  78.                     preparedStatement.setString(3,productStrings.get(2));
  79.                     preparedStatement.execute();
  80.                 }
  81.             } catch (SQLException e) {
  82.                 connection.close();
  83.             }
  84.         } catch (SQLException e) {
  85.         }
  86.  
  87.     }
  88.  
  89.     private List<String> convertProductList(List<Product> productList) {
  90.         StringBuilder aBuild = new StringBuilder();
  91.         StringBuilder pBuild = new StringBuilder();
  92.         StringBuilder nBuild = new StringBuilder();
  93.         for (Product product : productList) {
  94.             aBuild.append(product.getAmount() + ",");
  95.             pBuild.append(product.getPrice() + ",");
  96.             nBuild.append(product.getName() + ",");
  97.         }
  98.         List<String> productStrings = new ArrayList<>();
  99.         productStrings.add(aBuild.toString());
  100.         productStrings.add(pBuild.toString());
  101.         productStrings.add(nBuild.toString());
  102.         return productStrings;
  103.     }
  104. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top