Advertisement
Nickpips

Database.java

Apr 20th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. package icecream;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.text.DecimalFormat;
  8. import java.util.Arrays;
  9. import java.util.HashMap;
  10.  
  11. public class Database {
  12.     Connection con;
  13.     Statement st;
  14.  
  15.     public Database(Connection con) throws SQLException {
  16.         this.con = con;
  17.         st = con.createStatement();
  18.  
  19.         // st.execute("INSERT INTO toppings VALUES (1,'Chocolate Syup',2.5);");
  20.         // st.execute("INSERT INTO flavors VALUES (1,'Vanilla',3.5);");
  21.         // st.execute("INSERT INTO orders VALUES (1,1, 'N','P','Cone');");
  22.         // st.execute("INSERT INTO ordertoppings VALUES (1,1);");
  23.     }
  24.  
  25.     public void makeTables() throws SQLException {
  26.         try {
  27.             st.executeQuery("SELECT * FROM flavors WHERE 1=0;");
  28.         } catch (SQLException e) {
  29.             st.executeUpdate("CREATE TABLE flavors (id INT PRIMARY KEY, flavor VARCHAR(64), price MONEY);");
  30.         }
  31.  
  32.         try {
  33.             st.executeQuery("SELECT * FROM toppings WHERE 1=0;");
  34.         } catch (SQLException e) {
  35.             st.executeUpdate("CREATE TABLE toppings (id INT PRIMARY KEY, topping VARCHAR(64), price MONEY);");
  36.         }
  37.  
  38.         try {
  39.             //st.execute("INSERT INTO orders VALUES ('Nicholas','Pipitone',1,1,'Cone',GETDATE())");
  40.             st.executeQuery("SELECT * FROM orders WHERE 1=0;");
  41.         } catch (SQLException e) {
  42.             System.out.println(e.getMessage());
  43.             st.executeUpdate(
  44.                     "CREATE TABLE orders (id INT IDENTITY(1,1) PRIMARY KEY, fname VARCHAR(64), lname VARCHAR(64), flavorid INT FOREIGN KEY REFERENCES flavors(id), toppingid INT FOREIGN KEY REFERENCES toppings(id), container VARCHAR(64), date DATE);");
  45.         }
  46.     }
  47.  
  48.     final String[] cols = new String[] { "First name", "Last name", "Flavor", "Topping", "Container", "Price" };
  49.  
  50.     public String[][] getorders() throws SQLException {
  51.         ResultSet rs;
  52.  
  53.         rs = st.executeQuery(
  54.                 "SELECT *, toppings.price AS toppingPrice, flavors.price AS flavorPrice FROM orders INNER JOIN flavors ON orders.flavorid = flavors.id INNER JOIN toppings ON orders.toppingid = toppings.id;");
  55.         HashMap<Integer, String[]> rows = new HashMap<Integer, String[]>();
  56.  
  57.         while (rs.next()) {
  58.             int key = rs.getInt("id");
  59.             DecimalFormat df = new DecimalFormat("$0.00");
  60.             rows.put(key,
  61.                     new String[] { rs.getString("fname"), rs.getString("lname"), rs.getString("flavor"),
  62.                             rs.getString("topping"), rs.getString("container"),
  63.                             df.format(rs.getFloat("flavorPrice") + rs.getFloat("flavorPrice")) });
  64.         }
  65.  
  66.         if (rows.size() != 0) {
  67.             String[][] output = new String[rows.size()][];
  68.  
  69.             int index = 0;
  70.  
  71.             for (Integer key : rows.keySet()) {
  72.                 output[index] = rows.get(key);
  73.                 index++;
  74.             }
  75.  
  76.             return output;
  77.         } else {
  78.             return null;
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement