Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.40 KB | None | 0 0
  1. package frames;
  2. import java.awt.Color;
  3. import java.sql.*;
  4.  
  5. import javax.swing.DefaultComboBoxModel;
  6. import javax.swing.JComboBox;
  7. import javax.swing.JLabel;
  8. import javax.swing.JLayeredPane;
  9. import javax.swing.JPanel;
  10. import javax.swing.JTable;
  11. import javax.swing.table.DefaultTableModel;
  12.  
  13. import net.proteanit.sql.DbUtils;
  14.  
  15. public class MainClass
  16. {
  17.     static Connection con = DBConnection.doConnection();
  18.     static Statement stmt = null;
  19.     static ResultSet res = null;
  20.    
  21.     public static ResultSet select (String query)
  22.     {
  23.         try
  24.         {
  25.             stmt = con.createStatement();
  26.             res = stmt.executeQuery(query);
  27.            
  28.             return res;
  29.         }
  30.         catch (Exception ex)
  31.         {
  32.             System.out.println(ex.getMessage());
  33.            
  34.             return null;
  35.         }      
  36.     }
  37.    
  38.     //na vyn String per arsye se clickedID osht string, edhe nuk kemi nevoje me e kompliku shume
  39.     public static String getHeadAdminID ()
  40.     {
  41.         ResultSet test = MainClass.select("select S.id from Staff s where s.priviledge=1");
  42.        
  43.         try
  44.         {
  45.             while (test.next())
  46.             {
  47.                 return test.getString(1);
  48.             }
  49.         }
  50.         catch (Exception ex)
  51.         {
  52.         }
  53.        
  54.         return null;
  55.     }
  56.    
  57.     public static boolean exec (String query)
  58.     {
  59.         try
  60.         {
  61.             stmt = con.createStatement();
  62.             stmt.executeUpdate(query);
  63.        
  64.             return true;
  65.         }
  66.         catch (Exception ex)
  67.         {
  68.             System.out.println(ex.getMessage());
  69.            
  70.             return false;
  71.         }
  72.     }
  73.    
  74.     public static String getCIDfromUsername(String username)
  75.     {
  76.         ResultSet queried = MainClass.select("select C.id from Clients C where C.login.username='"+username+"'");
  77.        
  78.         try
  79.         {
  80.             while (queried.next())
  81.             {
  82.                 String id = queried.getString("id");
  83.                
  84.                 return id;
  85.             }
  86.         }
  87.         catch (Exception ex)
  88.         {
  89.             ex.getMessage();
  90.         }
  91.        
  92.         return null;
  93.     }
  94.    
  95.     public static int specialSelect (String query)
  96.     {
  97.         try
  98.         {
  99.             PreparedStatement statement = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
  100.                 statement.execute();
  101.                 PreparedStatement ps = con.prepareStatement("select sells_table_seq.currval from dual");
  102.                
  103.                 ResultSet rs = ps.executeQuery();
  104.                 int generatedKey;
  105.                 if (rs.next())
  106.                 {
  107.                     generatedKey = (int) rs.getLong(1);
  108.                    
  109.                     return generatedKey;
  110.                 }
  111.     }
  112.         catch (Exception ex)
  113.         {
  114.             ex.getMessage();
  115.         }
  116.        
  117.         return -1;
  118.     }
  119.    
  120.     public static int[] verifyLogin (String query, String username, String password, JLabel label, int isStaff)
  121.     {
  122.         ResultSet partTable = select (query);
  123.         int[] arr = new int[10];
  124.         arr[0] = -1;
  125.         arr[1] = -1;
  126.        
  127.         try
  128.         {
  129.             while (partTable.next())
  130.             {
  131.                 int id = Integer.parseInt(partTable.getString("id"));
  132.                 String user = partTable.getString("login.username");
  133.                 String pass = partTable.getString("login.password");
  134.                
  135.                 if (username.equals(user) && password.equals(pass))
  136.                 {  
  137.                     if (isStaff == 1)
  138.                     {
  139.                         int priv = Integer.parseInt(partTable.getString("priviledge"));
  140.                        
  141.                         arr[0] = id;
  142.                         arr[1] = priv;
  143.                     }
  144.                     else
  145.                     {  
  146.                         arr[0] = id;
  147.                     }
  148.                 }
  149.                 else
  150.                 {
  151.                     label.setForeground(Color.red);
  152.                     label.setText("Invalid login information");
  153.                 }
  154.             }
  155.         }
  156.         catch(Exception ex)
  157.         {
  158.             return arr;
  159.         }
  160.        
  161.         return arr;
  162.     }
  163.    
  164.     public static void ClearLayers (JLayeredPane layeredPane, JPanel panel)
  165.     {
  166.         layeredPane.removeAll();
  167.         layeredPane.add(panel);
  168.         layeredPane.repaint();
  169.         layeredPane.revalidate();
  170.     }  
  171.  
  172.     public static void viewIt (String query, JTable table)
  173.     {
  174.         ResultSet tryIt = select (query);
  175.        
  176.         table.setModel(DbUtils.resultSetToTableModel(tryIt));
  177.     }
  178.  
  179.     public static void fetchCities (JComboBox box)
  180.     {
  181.         ResultSet query = select ("select * from Cities");
  182.        
  183.         try
  184.         {
  185.             while (query.next())
  186.             {
  187.                 String emri = query.getString("name");
  188.                
  189.                 box.addItem(emri);
  190.             }
  191.         }
  192.         catch (Exception ex)
  193.         {
  194.             System.out.println(ex.getMessage());
  195.         }
  196.     }
  197.    
  198.     public static void clearModel (DefaultTableModel model)
  199.     {
  200.         for (int i = model.getRowCount() - 1; i >= 0; i--)
  201.         {
  202.             model.removeRow(i);
  203.         }
  204.     }
  205.    
  206.     public static void fetchReturned (JComboBox box)
  207.     {
  208.         ResultSet query = select ("select * from ReturnedType");
  209.        
  210.         try
  211.         {
  212.             while (query.next())
  213.             {
  214.                 String emri = query.getString("type");
  215.                
  216.                 box.addItem(emri);
  217.             }
  218.         }
  219.         catch (Exception ex)
  220.         {
  221.             System.out.println(ex.getMessage());
  222.         }
  223.     }
  224.    
  225.     public static void fetchPayments (JComboBox box)
  226.     {
  227.         ResultSet query = select ("select * from PaymentType");
  228.        
  229.         try
  230.         {
  231.             while (query.next())
  232.             {
  233.                 String emri = query.getString("type");
  234.                
  235.                 box.addItem(emri);
  236.             }
  237.         }
  238.         catch (Exception ex)
  239.         {
  240.             System.out.println(ex.getMessage());
  241.         }
  242.     }
  243.  
  244.     public static int getBookComponent (String component, int id) //amount, price
  245.     {
  246.         try
  247.         {
  248.             CallableStatement cstmt = con.prepareCall("{? = call getBookComponent(?, ?)}");
  249.             cstmt.registerOutParameter(1, Types.INTEGER);
  250.             cstmt.setString(2, component);
  251.             cstmt.setInt(3, id);
  252.             cstmt.executeUpdate();
  253.        
  254.             return cstmt.getInt(1);
  255.         }
  256.         catch (Exception ex)
  257.         {
  258.             System.out.println(ex.getMessage());
  259.            
  260.             return -1;
  261.         }
  262.        
  263.     }
  264.    
  265.     public static String getBookSells (String id)
  266.     {
  267.         ResultSet queried = MainClass.select("select count(*) \"Nr\" from Sells where bookid='"+id+"'");
  268.        
  269.         try
  270.         {
  271.             while (queried.next())
  272.             {
  273.                 String times = queried.getString("Nr");
  274.                
  275.                 return times;
  276.             }
  277.         }
  278.         catch (Exception ex)
  279.         {
  280.         }
  281.        
  282.         return null;
  283.     }
  284.  
  285.     public static String getBookBorrows (String id)
  286.     {
  287.         ResultSet queried = MainClass.select("select count(*) \"Nr\" from forRent where bookid='"+id+"'");
  288.        
  289.         try
  290.         {
  291.             while (queried.next())
  292.             {
  293.                 String times = queried.getString("Nr");
  294.                
  295.                 return times;
  296.             }
  297.         }
  298.         catch (Exception ex)
  299.         {  
  300.         }
  301.        
  302.         return null;
  303.     }
  304.    
  305.     public static int getPenaltyTimes (int clientID)
  306.     {
  307.         try
  308.         {
  309.             CallableStatement cstmt = con.prepareCall("{? = call getClientPenaltyTimes(?)}");
  310.            
  311.             cstmt.registerOutParameter(1, Types.INTEGER);
  312.             cstmt.setInt(2, clientID);
  313.             cstmt.executeUpdate();
  314.             int times = cstmt.getInt(1);
  315.            
  316.             return times;
  317.  
  318.         }
  319.         catch (Exception ex)
  320.         {
  321.             System.out.println(ex.getMessage());
  322.            
  323.             return -1;
  324.         }
  325.     }
  326.  
  327.     public static int getClientComponent(String component, int id) //currentlyRenting, penaltyTimes
  328.     {
  329.         try
  330.         {
  331.             CallableStatement cstmt = con.prepareCall("{? = call getClientComponent(?, ?)}");
  332.             cstmt.registerOutParameter(1, Types.INTEGER);
  333.             cstmt.setString(2, component);
  334.             cstmt.setInt(3, id);
  335.             cstmt.executeUpdate();
  336.            
  337.             return cstmt.getInt(1);
  338.         }
  339.         catch (Exception ex)
  340.         {
  341.             System.out.println(ex.getMessage());
  342.            
  343.             return -1;
  344.         }
  345.     }
  346.  
  347.     public static String getClientBuyCount(String id)
  348.     {
  349.         ResultSet query = MainClass.select("select count(*) \"Nr\" from Sells S where S.clientid="+id+" and S.approved=1");
  350.        
  351.         try
  352.         {
  353.             while (query.next())
  354.             {
  355.                 String times = query.getString("Nr");
  356.                
  357.                 return times;
  358.             }
  359.         }
  360.         catch (Exception ex)
  361.         {
  362.             System.out.println(ex.getMessage());
  363.         }
  364.        
  365.         return null;
  366.     }
  367.    
  368.     public static String getClientRentCount(String id)
  369.     {
  370.         ResultSet query = MainClass.select("select count(*) \"Nr\" from forRent R where R.clientid="+id);
  371.        
  372.         try
  373.         {
  374.             while (query.next())
  375.             {
  376.                 String times = query.getString("Nr");
  377.                
  378.                 return times;
  379.             }
  380.         }
  381.         catch (Exception ex)
  382.         {
  383.             System.out.println(ex.getMessage());
  384.         }
  385.        
  386.         return null;
  387.     }
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement