Advertisement
Guest User

DAO Voorstelling

a guest
Nov 27th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.37 KB | None | 0 0
  1. package voorstellingen;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8.  
  9. import dbconnectie.Connector;
  10.  
  11. public abstract class VoorstellingDAO {
  12.     public static Voorstelling getVoorstellingByVoorstellingId(int id){
  13.         Connection con = null;
  14.         ResultSet rs = null;
  15.         PreparedStatement ps = null;
  16.         Voorstelling vo = null;
  17.        
  18.         try{
  19.             con = Connector.getInstance();
  20.             ps = con.prepareStatement("SELECT * FROM Voorstellingen WHERE idVoorstellingen = ?");
  21.             ps.setInt(1, id);
  22.            
  23.             rs = ps.executeQuery();
  24.            
  25.             while(rs.next()){
  26.                 vo = new Voorstelling(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4),rs.getInt(5),rs.getInt(6),rs.getInt(7),rs.getInt(8),rs.getInt(9),rs.getInt(10));
  27.             }
  28.            
  29.             if(ps != null){
  30.                 ps.close();
  31.             }
  32.             if(rs != null){
  33.                 rs.close();
  34.             }
  35.             if(con != null){
  36.                 con.close();
  37.             }
  38.         } catch(SQLException e) {
  39.             return null;
  40.         }
  41.         return vo;
  42.     }
  43.    
  44.    
  45. /*
  46.  * GET ALL FROM VOORSTELLINGEN
  47.  */
  48.     public static ArrayList<Voorstelling> getAllVoorstellingen(){
  49.         ArrayList<Voorstelling> resultaten = new ArrayList<Voorstelling>();
  50.         Connection con = null;
  51.         ResultSet rs = null;
  52.         PreparedStatement ps = null;
  53.         Voorstelling vo = null;
  54.        
  55.         try{
  56.             con = Connector.getInstance();
  57.            
  58.             ps = con.prepareStatement("SELECT * FROM Voorstelling");
  59.            
  60.            
  61.             rs = ps.executeQuery();
  62.  
  63.            
  64.             while(rs.next()){
  65.                 vo = new Voorstelling(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4),rs.getInt(5),rs.getInt(6),rs.getInt(7),rs.getInt(8),rs.getInt(9),rs.getInt(10));
  66.                 resultaten.add(vo);
  67.             }
  68.            
  69.             if(ps != null){
  70.                 ps.close();
  71.             }
  72.             if(rs != null){
  73.                 rs.close();
  74.             }
  75.             if(con != null){
  76.                 con.close();
  77.             }
  78.         } catch(SQLException e) {
  79.             return resultaten;
  80.         }
  81.         return resultaten;
  82.     }
  83.    
  84.     /*
  85.      * GET VOORSTELLING FROM VOORSTELLINGEN BY VOORSTELLING NAME
  86.      */
  87.     public static ArrayList<Voorstelling> getVoorstellingByVoorstellingDate(int dag, int maand, int jaar){
  88.             Connection con = null;
  89.             ResultSet rs = null;
  90.             PreparedStatement ps = null;
  91.             ArrayList<Voorstelling> resultaten = new ArrayList<Voorstelling>();
  92.             //Voorstelling vo = null;
  93.             Voorstelling vo;
  94.             try{
  95.                 con = Connector.getInstance();
  96.                
  97.                 ps = con.prepareStatement("SELECT * FROM Voorstellingen where dag = ? AND maand = ? AND jaar = ?");
  98.                 ps.setInt(1, dag);
  99.                 ps.setInt(2, maand);
  100.                 ps.setInt(3, jaar);
  101.                
  102.                 rs = ps.executeQuery();
  103.                
  104.  
  105.                 while(rs.next()){
  106.                     vo = new Voorstelling(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4),rs.getInt(5),rs.getInt(6),rs.getInt(7),rs.getInt(8),rs.getInt(9),rs.getInt(10));
  107.                     resultaten.add(vo);
  108.                 }
  109.                
  110.                 if(ps != null){
  111.                     ps.close();
  112.                 }
  113.                 if(rs != null){
  114.                     rs.close();
  115.                 }
  116.                 if(con != null){
  117.                     con.close();
  118.                 }
  119.             } catch(SQLException e) {
  120.                 return null;
  121.             }
  122.             return resultaten;
  123.         }
  124.    
  125.    
  126.    
  127.    
  128.     public static boolean checkIfVoorstellingExists(int id){
  129.         Voorstelling vo = getVoorstellingByVoorstellingId(id);
  130.        
  131.         if( vo == null){
  132.             return false;
  133.         }
  134.        
  135.         return true;
  136.     }
  137.    
  138.     public static boolean addOrReplace(Voorstelling vo){
  139.         if(vo == null){
  140.             return false;
  141.         }
  142.        
  143.         Connection con = null;
  144.         int resultaat;
  145.         PreparedStatement ps = null;
  146.         boolean bestaat;
  147.         boolean available=true;
  148.         Voorstelling result = getVoorstellingByVoorstellingId(vo.getIdVoorstelling());
  149.         if(result == null){
  150.             bestaat = false;
  151.         }
  152.         else{
  153.             bestaat = true;
  154.         }
  155.        
  156.         /*
  157.          * Controleer of er een voorstelling in een bepaalde zaal nog kan
  158.          */
  159.         ArrayList<Voorstelling> voorstellingen = getVoorstellingByVoorstellingDate(result.getDag(), result.getMaand(), result.getJaar());
  160.         for(int i=0;i<voorstellingen.size();i++){
  161.             if(voorstellingen.get(i).getBeginUur()>23 || voorstellingen.get(i).getEindUur()>23)
  162.                 available=false;
  163.             if(vo.getIdZalen()==voorstellingen.get(i).getIdZalen()){
  164.                 if(vo.getEindUur()>=voorstellingen.get(i).getBeginUur() && vo.getEindUur()<=voorstellingen.get(i).getEindUur()){
  165.                     if(vo.getEindMin()>=voorstellingen.get(i).getBeginMin() && vo.getEindMin()<=voorstellingen.get(i).getEindMin()){
  166.                         available=false;
  167.                     }
  168.                 }
  169.             }
  170.         }
  171.        
  172.         try {
  173.                 con = Connector.getInstance();
  174.                
  175.                 if(bestaat){
  176.                     ps = con.prepareStatement("UPDATE Voorstellingen SET idVoorstellingen = ?, dag = ?, maand = ?, jaar = ?, beginUur = ?, beginMin = ?, eindUur = ?, eindMin = ?, idZalen = ?, idEvenementen = ?");
  177.                     ps.setInt(1, result.getIdVoorstelling());
  178.                     ps.setInt(2, result.getDag());
  179.                     ps.setInt(3, result.getMaand());
  180.                     ps.setInt(4, result.getJaar());
  181.                     ps.setInt(5, result.getBeginUur());
  182.                     ps.setInt(6, result.getBeginMin());
  183.                     ps.setInt(7, result.getEindUur());
  184.                     ps.setInt(8, result.getEindMin());
  185.                     ps.setInt(9, result.getIdZalen());
  186.                     ps.setInt(10, result.getIdEvenementen());
  187.                 }
  188.                 else if(available){
  189.                     ps = con.prepareStatement("INSERT INTO Voorstellingen VALUES (null,?,?,?,?,?,?,?,?,?)");
  190.                     ps.setInt(1, result.getIdVoorstelling());
  191.                     ps.setInt(2, result.getDag());
  192.                     ps.setInt(3, result.getMaand());
  193.                     ps.setInt(4, result.getJaar());
  194.                     ps.setInt(5, result.getBeginUur());
  195.                     ps.setInt(6, result.getBeginMin());
  196.                     ps.setInt(7, result.getEindUur());
  197.                     ps.setInt(8, result.getEindMin());
  198.                     ps.setInt(9, result.getIdZalen());
  199.                     ps.setInt(10, result.getIdEvenementen());
  200.                 }
  201.                 else{
  202.                     return false;
  203.                 }
  204.                
  205.                 resultaat = ps.executeUpdate();
  206.                
  207.        
  208.                
  209.         if(ps != null){
  210.             ps.close();
  211.         }
  212.                
  213.         if(con != null){
  214.             con.close();
  215.         }
  216.    
  217.                
  218.         }
  219.         catch (SQLException e) {
  220.                 return false;
  221.            
  222.         }
  223.        
  224.         if(resultaat > -1){
  225.             return true;
  226.         }
  227.        
  228.         return false;  
  229.     }
  230.    
  231.     public static Voorstelling getVoorstellingByZaalId(int id){
  232.         Connection con = null;
  233.         ResultSet rs = null;
  234.         PreparedStatement ps = null;
  235.         Voorstelling vo = null;
  236.        
  237.         try{
  238.             con = Connector.getInstance();
  239.            
  240.             ps = con.prepareStatement("SELECT * FROM Voorstellingen WHERE idZalen = ?");
  241.             ps.setInt(1, id);
  242.            
  243.             rs = ps.executeQuery();
  244.            
  245.             while(rs.next()){
  246.                 vo = new Voorstelling(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4),rs.getInt(5),rs.getInt(6),rs.getInt(7),rs.getInt(8),rs.getInt(9),rs.getInt(10));
  247.             }
  248.            
  249.             if(ps != null){
  250.                 ps.close();
  251.             }
  252.             if(rs != null){
  253.                 rs.close();
  254.             }
  255.             if(con != null){
  256.                 con.close();
  257.             }
  258.         } catch(SQLException e) {
  259.             return null;
  260.         }
  261.         return vo;
  262.     }
  263.    
  264.     public static ArrayList<Voorstelling> getVoorstellingByEvenementenId(int id){
  265.         Connection con = null;
  266.         ResultSet rs = null;
  267.         PreparedStatement ps = null;
  268.         ArrayList<Voorstelling> results = new ArrayList<Voorstelling>();
  269.         Voorstelling vo = null;
  270.        
  271.         try{
  272.             con = Connector.getInstance();
  273.            
  274.             ps = con.prepareStatement("SELECT * FROM Voorstellingen WHERE idEvenementen = ?");
  275.             ps.setInt(1, id);
  276.            
  277.             rs = ps.executeQuery();
  278.            
  279.             while(rs.next()){
  280.                 vo = new Voorstelling(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4),rs.getInt(5),rs.getInt(6),rs.getInt(7),rs.getInt(8),rs.getInt(9),rs.getInt(10));
  281.                 results.add(vo);
  282.             }
  283.            
  284.             if(ps != null){
  285.                 ps.close();
  286.             }
  287.             if(rs != null){
  288.                 rs.close();
  289.             }
  290.             if(con != null){
  291.                 con.close();
  292.             }
  293.         } catch(SQLException e) {
  294.             return null;
  295.         }
  296.         return results;
  297.     }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement