Quipex

db

Jun 5th, 2017
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package classes;
  7.  
  8. import java.sql.*;
  9.  
  10. /**
  11.  *
  12.  * @author User
  13.  */
  14. public class Database {
  15.     static Connection conn;
  16.     static Statement statmt;
  17.     static ResultSet resSet;
  18.     static final String DBNAME = "pharmacy.db";
  19.  
  20.     // --------ПОДКЛЮЧЕНИЕ К БАЗЕ ДАННЫХ--------
  21.  
  22.     /**
  23.      * Connects to a fixed database.
  24.      * @throws ClassNotFoundException
  25.      * @throws SQLException
  26.      */
  27.     public static void conn() throws ClassNotFoundException, SQLException
  28.     {
  29.         conn = null;
  30.         Class.forName("org.sqlite.JDBC");
  31.         conn = DriverManager.getConnection("jdbc:sqlite:"+DBNAME);
  32.         statmt = conn.createStatement();
  33.     }
  34.    
  35.     public static Object getRowFromTable(String table, String column, int index) throws ClassNotFoundException, SQLException{
  36.         resSet = statmt.executeQuery("SELECT "+column+" FROM "+table+";");
  37.         while(index-- > 0)
  38.             resSet.next();
  39.         Object obj = resSet.getObject(column);
  40.         return obj;
  41.     }
  42.    
  43.     public static Object getByParameter(String table, String column, String parameter, String value)throws ClassNotFoundException,SQLException{
  44.         resSet = statmt.executeQuery("SELECT "+column+" FROM "+table+" WHERE "+parameter+"="+value+";");
  45.         resSet.next();
  46.         Object obj = resSet.getObject(column);
  47.         return obj;
  48.     }
  49.    
  50.     public static void closeDB() throws ClassNotFoundException, SQLException
  51.     {
  52.         conn.close();
  53.         statmt.close();
  54.         resSet.close();
  55.     }
  56.  
  57.     static int getNumSales(int id_goods) throws SQLException {
  58.         resSet = statmt.executeQuery("select sum(sale_goods.quantity) as A from sale_goods join goods on sale_goods.id_goods=goods.id_goods " +
  59.                                      "where sale_goods.id_goods = "+id_goods+";");
  60.         resSet.next();
  61.         return resSet.getInt("A");
  62.     }
  63.  
  64.     static int getNumWarehouse(int id_goods) throws SQLException {
  65.         resSet = statmt.executeQuery("select sum(purchase.quantity) as A from purchase join goods on purchase.id_purchase=goods.id_purchase " +
  66.                                      "where goods.id_goods = "+id_goods+";");
  67.         resSet.next();
  68.         return resSet.getInt("A");
  69.     }
  70.    
  71.     static String getDate(int id_goods) throws SQLException {
  72.         resSet = statmt.executeQuery("select purchase.date as A from purchase join goods on purchase.id_purchase=goods.id_purchase " +
  73.                                      "where goods.id_goods = "+id_goods+";");
  74.         resSet.next();
  75.         return resSet.getString("A");
  76.     }
  77.    
  78.     static Object getSaleInfo(String parameter, int id_goods, String dateStart, String dateEnd, int index) throws SQLException{
  79.         Object obj;
  80.         resSet = statmt.executeQuery("select "+parameter+" as A from " +
  81.                                      "sale_goods join sale join goods join employee on "
  82.                                    + "sale.id_employee=employee.id_employee and sale_goods.id_goods=goods.id_goods and sale_goods.id_sale=sale.id_sale "
  83.                                    + "where goods.id_goods="+id_goods+" and sale.date between date('"+dateStart+"') and date('"+dateEnd+"') order by sale.date desc;");
  84.         while(index-- > 0)
  85.             resSet.next();
  86.         obj = resSet.getObject("A");
  87.         return obj;
  88.     }
  89.    
  90.     public static int getSaleInfoNum(int id_goods, String dateStart, String dateEnd, int index) throws SQLException{
  91.         int num;
  92.         resSet = statmt.executeQuery("select count() from " +
  93.                                      "sale_goods join sale join goods join employee on "
  94.                                    + "sale.id_employee=employee.id_employee and sale_goods.id_goods=goods.id_goods and sale_goods.id_sale=sale.id_sale "
  95.                                    + "where goods.id_goods="+id_goods+" and sale.date between date('"+dateStart+"') and date('"+dateEnd+"');");
  96.         resSet.next();
  97.         num = resSet.getInt("count()");
  98.         return num;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment