Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.90 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.ArrayList;
  3.  
  4. /**
  5.  * Creates sqlite database connection. Consists methods to work with database fields.
  6.  */
  7. class DBContainer {
  8.     private Connection conn = null;
  9.  
  10.     /**
  11.      * Opens a database connection.
  12.      */
  13.     void open() {
  14.         try {
  15.             Class.forName("org.sqlite.JDBC");
  16.             conn = DriverManager.getConnection("jdbc:sqlite:./src/db/user.db");
  17.             System.out.println("Connected");
  18.         }
  19.         catch (Exception e) {
  20.             System.out.println(e.getMessage());
  21.         }
  22.     }
  23.  
  24.     /**
  25.      * Closes a database connection.
  26.      */
  27.     void close() {
  28.         try {
  29.             conn.close();
  30.             System.out.println("Disconnected");
  31.         }
  32.         catch (Exception e) {
  33.             System.out.println(e.getMessage());
  34.         }
  35.     }
  36.  
  37.     /**
  38.      * Creates a library.
  39.      */
  40.     void createLib() {
  41.         try {
  42.             DatabaseMetaData dbmd = conn.getMetaData();
  43.             String[] types = {"TABLE"};
  44.             ResultSet rs = dbmd.getTables(null, null, "%", types);
  45.             while (rs.next()) {
  46.                 if (rs.getString("TABLE_NAME").equals("library")) return;
  47.             }
  48.  
  49.             String query = "CREATE TABLE library (" +
  50.                     "id INTEGER PRIMARY KEY AUTOINCREMENT, \n" +
  51.                     "\titem VARCHAR(15), \n" +
  52.                     "\tprotein REAL, \n" +
  53.                     "\tfat REAL, \n" +
  54.                     "\tcarbohydrates REAL)";
  55.  
  56.             Statement statement = conn.createStatement();
  57.             statement.executeUpdate(query);
  58.             statement.close();
  59.         }
  60.         catch (Exception e) {
  61.             System.out.println(e.getMessage());
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * Adds an item to a library (a table in sqlite database).
  67.      * @param item - name of a item.
  68.      * @param protein - protein content of an item.
  69.      * @param fat - fat content of an item.
  70.      * @param carbohydrates - carbohydrates content of an item.
  71.      */
  72.     void addToLib(String item, float protein, float fat, float carbohydrates) {
  73.         try {
  74.             String query = "INSERT INTO library (item, protein, fat, carbohydrates) " +
  75.                     "VALUES ('" + item + "', '" + protein + "', '" + fat + "', '" + carbohydrates + "')";
  76.             Statement statement = conn.createStatement();
  77.             statement.executeUpdate(query);
  78.             statement.close();
  79.         }
  80.         catch (Exception e) {
  81.             System.out.println(e.getMessage());
  82.         }
  83.     }
  84.  
  85.     /**
  86.      * Creates a DietContainer object and fills it with library's values.
  87.      * @param container - a DietContainer object to collect values of library.
  88.      */
  89.     void showLib(DietContainer container) {
  90.         try {
  91.             Statement statement = conn.createStatement();
  92.             String query = "SELECT item, protein, fat, carbohydrates FROM library";
  93.             ResultSet rs = statement.executeQuery(query);
  94.  
  95.             while (rs.next()) {
  96.                 container.add(rs.getString("item"),
  97.                         rs.getFloat("protein"),
  98.                         rs.getFloat("fat"),
  99.                         rs.getFloat("carbohydrates"));
  100.             }
  101.  
  102.             rs.close();
  103.         }
  104.         catch (Exception e) {
  105.             System.out.println(e.getMessage());
  106.         }
  107.     }
  108.  
  109.     /**
  110.      * Deletes an item from a library.
  111.      * @param item - an item of a library to delete.
  112.      */
  113.     void deleteLibItem(String item) {
  114.         try {
  115.             Statement statement = conn.createStatement();
  116.             String query = "DELETE FROM 'library' WHERE item = " + "'" + item + "'";
  117.             statement.executeUpdate(query);
  118.             statement.close();
  119.         }
  120.         catch (Exception e) {
  121.             System.out.println(e.getMessage());
  122.         }
  123.     }
  124.  
  125.     /**
  126.      * Creates a table in an sqlite database.
  127.      * @param table - name of a table.
  128.      */
  129.     void createTable(String table) {
  130.         try {
  131.             String query = "CREATE TABLE " + table + "(" +
  132.                     "id INTEGER PRIMARY KEY AUTOINCREMENT, \n" +
  133.                     "\titem VARCHAR(15), \n" +
  134.                     "\tprotein REAL, \n" +
  135.                     "\tfat REAL, \n" +
  136.                     "\tcarbohydrates REAL)";
  137.  
  138.             Statement statement = conn.createStatement();
  139.             statement.executeUpdate(query);
  140.             statement.close();
  141.         }
  142.         catch (Exception e) {
  143.             System.out.println(e.getMessage());
  144.         }
  145.     }
  146.  
  147.     /**
  148.      * Adds an item to an sqlite database's table.
  149.      * @param item - name of an item.
  150.      * @param table - name of a table.
  151.      */
  152.     void addToTable(String item, String table) {
  153.         float protein;
  154.         float fat;
  155.         float carbohydrates;
  156.  
  157.         try {
  158.             Statement statement = conn.createStatement();
  159.             String query = "SELECT item, protein, fat, carbohydrates FROM "
  160.                     + "\'library\'" + " WHERE item = " + "'" + item + "'";
  161.             ResultSet rs = statement.executeQuery(query);
  162.             protein = rs.getFloat("protein");
  163.             fat = rs.getFloat("fat");
  164.             carbohydrates = rs.getFloat("carbohydrates");
  165.  
  166.             query = "INSERT INTO " + table + " (item, protein, fat, carbohydrates) " +
  167.                     "VALUES ('" + item + "', '" + protein + "', '" + fat + "', '" + carbohydrates + "')";
  168.             statement.executeUpdate(query);
  169.             statement.close();
  170.         }
  171.         catch (Exception e) {
  172.             System.out.println(e.getMessage());
  173.         }
  174.     }
  175.  
  176.     /**
  177.      * Fills a DietContainer object with an sqlite database table's values.
  178.      * @param container - a DietContainer object to collect table's values.
  179.      * @param table - a table with needed values.
  180.      */
  181.     void showTable(DietContainer container, String table) {
  182.         container.tableName = table;
  183.  
  184.         try {
  185.             Statement statement = conn.createStatement();
  186.             String query = "SELECT item, protein, fat, carbohydrates FROM " + table;
  187.             ResultSet rs = statement.executeQuery(query);
  188.  
  189.             while (rs.next()) {
  190.                 container.add(rs.getString("item"),
  191.                         rs.getFloat("protein"),
  192.                         rs.getFloat("fat"),
  193.                         rs.getFloat("carbohydrates"));
  194.             }
  195.  
  196.             rs.close();
  197.         }
  198.         catch (Exception e) {
  199.             System.out.println(e.getMessage());
  200.         }
  201.     }
  202.  
  203.     /**
  204.      * Gets names of tables into the given list.
  205.      * @param tableNames - list to collect table names.
  206.      */
  207.     void getTables(ArrayList<String> tableNames) {
  208.         try {
  209.             DatabaseMetaData dbmd = conn.getMetaData();
  210.             String[] types = {"TABLE"};
  211.             ResultSet rs = dbmd.getTables(null, null, "%", types);
  212.             while (rs.next()) {
  213.                 tableNames.add(rs.getString("TABLE_NAME"));
  214.             }
  215.         }
  216.         catch (Exception e) {
  217.             System.out.println(e.getMessage());
  218.         }
  219.     }
  220.  
  221.     /**
  222.      * Deletes an item from a given table.
  223.      * @param item - an item to delete.
  224.      * @param table - table with needed item.
  225.      */
  226.     void removeFromTable(String item, String table) {
  227.         try {
  228.             Statement statement = conn.createStatement();
  229.             String query = "DELETE FROM " + "'"+ table + "'" + " WHERE item = " + "'" + item + "'";
  230.             statement.executeUpdate(query);
  231.             statement.close();
  232.         }
  233.         catch (Exception e) {
  234.             System.out.println(e.getMessage());
  235.         }
  236.     }
  237.  
  238.     /**
  239.      * Deletes a table from an sqlite database.
  240.      * @param table - a table to delete.
  241.      */
  242.     void deleteTable(String table) {
  243.         try {
  244.             String query = "DROP TABLE " + table;
  245.  
  246.             Statement statement = conn.createStatement();
  247.             statement.executeUpdate(query);
  248.             statement.close();
  249.         }
  250.         catch (Exception e) {
  251.             System.out.println(e.getMessage());
  252.         }
  253.     }
  254.  
  255.     //TODO
  256.     void createGroup(String group) {
  257.         StringBuilder sb = new StringBuilder("_");
  258.         sb.append(group);
  259.         try {
  260.             String query = "CREATE TABLE " + "'" + sb + "'" + " (" +
  261.                     "id INTEGER PRIMARY KEY AUTOINCREMENT, \n" +
  262.                     "\ttableName VARCHAR(15), \n" +
  263.                     "\tprotein REAL, \n" +
  264.                     "\tfat REAL, \n" +
  265.                     "\tcarbohydrates REAL)";
  266.  
  267.             Statement statement = conn.createStatement();
  268.             statement.executeUpdate(query);
  269.             statement.close();
  270.         }
  271.         catch (Exception e) {
  272.             System.out.println(e.getMessage());
  273.         }
  274.     }
  275.  
  276.     //TODO
  277.     void addToGroup(DietContainer container, String table, String group) {
  278.         //container.tableName = table;
  279.  
  280.         try {
  281.             Statement statement = conn.createStatement();
  282.             String query = "SELECT item, protein, fat, carbohydrates FROM " + table;
  283.             ResultSet rs = statement.executeQuery(query);
  284.  
  285.             while (rs.next()) {
  286.                 container.add(rs.getString("item"),
  287.                         rs.getFloat("protein"),
  288.                         rs.getFloat("fat"),
  289.                         rs.getFloat("carbohydrates"));
  290.             }
  291.  
  292.             rs.close();
  293.  
  294.             query = "INSERT INTO " + group + "(tableName, protein, fat, carbohydrates) " +
  295.                     "VALUES ('"
  296.                     + table
  297.                     + "', '"
  298.                     + container.proteinAbsoluteTotal
  299.                     + "', '"
  300.                     + container.fatAbsoluteTotal
  301.                     + "', '"
  302.                     + container.carbohydratesAbsoluteTotal + "')";
  303.  
  304.             statement.executeUpdate(query);
  305.             statement.close();
  306.  
  307.  
  308.         } catch (Exception e) {
  309.             System.out.println(e.getMessage());
  310.         }
  311.  
  312.  
  313.     }
  314.  
  315.     //TODO
  316.     void showGroups() {
  317.  
  318.     }
  319.  
  320.     //TODO
  321.     void showGroup(DietContainer container, String group) {
  322.         container.groupName = group;
  323.  
  324.         try {
  325.             Statement statement = conn.createStatement();
  326.             String query = "SELECT tableName, protein, fat, carbohydrates FROM " + "'" + group + "'";
  327.             ResultSet rs = statement.executeQuery(query);
  328.  
  329.             while (rs.next()) {
  330.                 container.add(rs.getString("tableName"),
  331.                         rs.getFloat("protein"),
  332.                         rs.getFloat("fat"),
  333.                         rs.getFloat("carbohydrates"));
  334.             }
  335.  
  336.             rs.close();
  337.         }
  338.         catch (Exception e) {
  339.             System.out.println(e.getMessage());
  340.         }
  341.     }
  342.  
  343.     //TODO
  344.     void removeFromGroup(String table, String group) {
  345.  
  346.     }
  347.  
  348.     //TODO
  349.     void deleteGroup(String group) {
  350.  
  351.     }
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement