Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.67 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 showTables(ArrayList<String> tableNames) {
  208.         try {
  209.             DatabaseMetaData dbmd = conn.getMetaData();
  210.             String[] types = {"TABLE"};
  211.             ResultSet rs = dbmd.getTables(null, null, "%", types);
  212.  
  213.             while (rs.next()) {
  214.                 tableNames.add(rs.getString("TABLE_NAME"));
  215.             }
  216.  
  217.             for (int i = tableNames.size(); i > 0; i--) {
  218.                 if (tableNames.get(i - 1).charAt(0) == '_') {
  219.                     tableNames.remove(i - 1);
  220.                 }
  221.             }
  222.         }
  223.         catch (Exception e) {
  224.             System.out.println(e.getMessage());
  225.         }
  226.     }
  227.  
  228.     /**
  229.      * Deletes an item from a given table.
  230.      * @param item - an item to delete.
  231.      * @param table - table with needed item.
  232.      */
  233.     void removeFromTable(String item, String table) {
  234.         try {
  235.             Statement statement = conn.createStatement();
  236.             String query = "DELETE FROM " + "'"+ table + "'" + " WHERE item = " + "'" + item + "'";
  237.             statement.executeUpdate(query);
  238.             statement.close();
  239.         }
  240.         catch (Exception e) {
  241.             System.out.println(e.getMessage());
  242.         }
  243.     }
  244.  
  245.     /**
  246.      * Deletes a table from an sqlite database.
  247.      * @param table - a table to delete.
  248.      */
  249.     void deleteTable(String table) {
  250.         try {
  251.             String query = "DROP TABLE " + table;
  252.  
  253.             Statement statement = conn.createStatement();
  254.             statement.executeUpdate(query);
  255.             statement.close();
  256.         }
  257.         catch (Exception e) {
  258.             System.out.println(e.getMessage());
  259.         }
  260.     }
  261.  
  262.     /**
  263.      * Creates a group of tables.
  264.      * @param group - name of a group.
  265.      */
  266.     void createGroup(String group) {
  267.         StringBuilder sb = new StringBuilder("_");
  268.         sb.append(group);
  269.         try {
  270.             String query = "CREATE TABLE " + "'" + sb + "'" + " (" +
  271.                     "id INTEGER PRIMARY KEY AUTOINCREMENT, \n" +
  272.                     "\ttableName VARCHAR(15), \n" +
  273.                     "\tprotein REAL, \n" +
  274.                     "\tfat REAL, \n" +
  275.                     "\tcarbohydrates REAL)";
  276.  
  277.             Statement statement = conn.createStatement();
  278.             statement.executeUpdate(query);
  279.             statement.close();
  280.         }
  281.         catch (Exception e) {
  282.             System.out.println(e.getMessage());
  283.         }
  284.     }
  285.  
  286.     /**
  287.      * Adds a table to a group.
  288.      * @param container - DietContainer object needed to buffer values of table.
  289.      * @param table - name of a table to be added to a group.
  290.      * @param group - name of a group to collect table.
  291.      */
  292.     void addToGroup(DietContainer container, String table, String group) {
  293.         container.tableName = group; //was groupName
  294.  
  295.         try {
  296.             Statement statement = conn.createStatement();
  297.             String query = "SELECT item, protein, fat, carbohydrates FROM " + table;
  298.             ResultSet rs = statement.executeQuery(query);
  299.  
  300.             while (rs.next()) {
  301.                 container.add(rs.getString("item"),
  302.                         rs.getFloat("protein"),
  303.                         rs.getFloat("fat"),
  304.                         rs.getFloat("carbohydrates"));
  305.             }
  306.  
  307.             query = "INSERT INTO " + group + "(tableName, protein, fat, carbohydrates) " +
  308.                     "VALUES ('"
  309.                     + table
  310.                     + "', '"
  311.                     + container.proteinAbsoluteTotal
  312.                     + "', '"
  313.                     + container.fatAbsoluteTotal
  314.                     + "', '"
  315.                     + container.carbohydratesAbsoluteTotal + "')";
  316.  
  317.             statement.executeUpdate(query);
  318.             statement.close();
  319.  
  320.  
  321.         } catch (Exception e) {
  322.             System.out.println(e.getMessage());
  323.         }
  324.  
  325.  
  326.     }
  327.  
  328.     /**
  329.      * Gets names of groups into the given list.
  330.      * @param tableNames - list to collect groups names.
  331.      */
  332.     void showGroups(ArrayList<String> tableNames) {
  333.         try {
  334.             DatabaseMetaData dbmd = conn.getMetaData();
  335.             String[] types = {"TABLE"};
  336.             ResultSet rs = dbmd.getTables(null, null, "%", types);
  337.  
  338.             while (rs.next()) {
  339.                 tableNames.add(rs.getString("TABLE_NAME"));
  340.             }
  341.  
  342.             for (int i = tableNames.size(); i > 0; i--) {
  343.                 if (tableNames.get(i - 1).charAt(0) != '_') {
  344.                     tableNames.remove(i - 1);
  345.                 }
  346.             }
  347.         }
  348.         catch (Exception e) {
  349.             System.out.println(e.getMessage());
  350.         }
  351.     }
  352.  
  353.     /**
  354.      * Print a console version of a given group.
  355.      * @param group - group to print.
  356.      */
  357.     void showGroup(DietContainer container, String group) {
  358.         container.tableName = group;
  359.  
  360.         try {
  361.             Statement statement = conn.createStatement();
  362.             String query = "SELECT tableName, protein, fat, carbohydrates FROM " + "'" + group + "'";
  363.             ResultSet rs = statement.executeQuery(query);
  364.  
  365.             while (rs.next()) {
  366.                 container.add(rs.getString("tableName"),
  367.                         rs.getFloat("protein"),
  368.                         rs.getFloat("fat"),
  369.                         rs.getFloat("carbohydrates"));
  370.             }
  371.  
  372.             rs.close();
  373.         }
  374.         catch (Exception e) {
  375.             System.out.println(e.getMessage());
  376.         }
  377.     }
  378.  
  379.     /**
  380.      * Deletes an table from a given group.
  381.      * @param tableName - a table name of a table to delete.
  382.      * @param group - group with needed item.
  383.      */
  384.     void removeFromGroup(String tableName, String group) {
  385.         try {
  386.             Statement statement = conn.createStatement();
  387.             String query = "DELETE FROM " + "'"+ group + "'" + " WHERE tableName = " + "'" + tableName + "'";
  388.             statement.executeUpdate(query);
  389.             statement.close();
  390.         }
  391.         catch (Exception e) {
  392.             System.out.println(e.getMessage());
  393.         }
  394.     }
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement