Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package voorstellingen;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import dbconnectie.Connector;
- public abstract class VoorstellingDAO {
- public static Voorstelling getVoorstellingByVoorstellingId(int id){
- Connection con = null;
- ResultSet rs = null;
- PreparedStatement ps = null;
- Voorstelling vo = null;
- try{
- con = Connector.getInstance();
- ps = con.prepareStatement("SELECT * FROM Voorstellingen WHERE idVoorstellingen = ?");
- ps.setInt(1, id);
- rs = ps.executeQuery();
- while(rs.next()){
- 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));
- }
- if(ps != null){
- ps.close();
- }
- if(rs != null){
- rs.close();
- }
- if(con != null){
- con.close();
- }
- } catch(SQLException e) {
- return null;
- }
- return vo;
- }
- /*
- * GET ALL FROM VOORSTELLINGEN
- */
- public static ArrayList<Voorstelling> getAllVoorstellingen(){
- ArrayList<Voorstelling> resultaten = new ArrayList<Voorstelling>();
- Connection con = null;
- ResultSet rs = null;
- PreparedStatement ps = null;
- Voorstelling vo = null;
- try{
- con = Connector.getInstance();
- ps = con.prepareStatement("SELECT * FROM Voorstelling");
- rs = ps.executeQuery();
- while(rs.next()){
- 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));
- resultaten.add(vo);
- }
- if(ps != null){
- ps.close();
- }
- if(rs != null){
- rs.close();
- }
- if(con != null){
- con.close();
- }
- } catch(SQLException e) {
- return resultaten;
- }
- return resultaten;
- }
- /*
- * GET VOORSTELLING FROM VOORSTELLINGEN BY VOORSTELLING NAME
- */
- public static ArrayList<Voorstelling> getVoorstellingByVoorstellingDate(int dag, int maand, int jaar){
- Connection con = null;
- ResultSet rs = null;
- PreparedStatement ps = null;
- ArrayList<Voorstelling> resultaten = new ArrayList<Voorstelling>();
- //Voorstelling vo = null;
- Voorstelling vo;
- try{
- con = Connector.getInstance();
- ps = con.prepareStatement("SELECT * FROM Voorstellingen where dag = ? AND maand = ? AND jaar = ?");
- ps.setInt(1, dag);
- ps.setInt(2, maand);
- ps.setInt(3, jaar);
- rs = ps.executeQuery();
- while(rs.next()){
- 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));
- resultaten.add(vo);
- }
- if(ps != null){
- ps.close();
- }
- if(rs != null){
- rs.close();
- }
- if(con != null){
- con.close();
- }
- } catch(SQLException e) {
- return null;
- }
- return resultaten;
- }
- public static boolean checkIfVoorstellingExists(int id){
- Voorstelling vo = getVoorstellingByVoorstellingId(id);
- if( vo == null){
- return false;
- }
- return true;
- }
- public static boolean addOrReplace(Voorstelling vo){
- if(vo == null){
- return false;
- }
- Connection con = null;
- int resultaat;
- PreparedStatement ps = null;
- boolean bestaat;
- boolean available=true;
- Voorstelling result = getVoorstellingByVoorstellingId(vo.getIdVoorstelling());
- if(result == null){
- bestaat = false;
- }
- else{
- bestaat = true;
- }
- /*
- * Controleer of er een voorstelling in een bepaalde zaal nog kan
- */
- ArrayList<Voorstelling> voorstellingen = getVoorstellingByVoorstellingDate(result.getDag(), result.getMaand(), result.getJaar());
- for(int i=0;i<voorstellingen.size();i++){
- if(voorstellingen.get(i).getBeginUur()>23 || voorstellingen.get(i).getEindUur()>23)
- available=false;
- if(vo.getIdZalen()==voorstellingen.get(i).getIdZalen()){
- if(vo.getEindUur()>=voorstellingen.get(i).getBeginUur() && vo.getEindUur()<=voorstellingen.get(i).getEindUur()){
- if(vo.getEindMin()>=voorstellingen.get(i).getBeginMin() && vo.getEindMin()<=voorstellingen.get(i).getEindMin()){
- available=false;
- }
- }
- }
- }
- try {
- con = Connector.getInstance();
- if(bestaat){
- ps = con.prepareStatement("UPDATE Voorstellingen SET idVoorstellingen = ?, dag = ?, maand = ?, jaar = ?, beginUur = ?, beginMin = ?, eindUur = ?, eindMin = ?, idZalen = ?, idEvenementen = ?");
- ps.setInt(1, result.getIdVoorstelling());
- ps.setInt(2, result.getDag());
- ps.setInt(3, result.getMaand());
- ps.setInt(4, result.getJaar());
- ps.setInt(5, result.getBeginUur());
- ps.setInt(6, result.getBeginMin());
- ps.setInt(7, result.getEindUur());
- ps.setInt(8, result.getEindMin());
- ps.setInt(9, result.getIdZalen());
- ps.setInt(10, result.getIdEvenementen());
- }
- else if(available){
- ps = con.prepareStatement("INSERT INTO Voorstellingen VALUES (null,?,?,?,?,?,?,?,?,?)");
- ps.setInt(1, result.getIdVoorstelling());
- ps.setInt(2, result.getDag());
- ps.setInt(3, result.getMaand());
- ps.setInt(4, result.getJaar());
- ps.setInt(5, result.getBeginUur());
- ps.setInt(6, result.getBeginMin());
- ps.setInt(7, result.getEindUur());
- ps.setInt(8, result.getEindMin());
- ps.setInt(9, result.getIdZalen());
- ps.setInt(10, result.getIdEvenementen());
- }
- else{
- return false;
- }
- resultaat = ps.executeUpdate();
- if(ps != null){
- ps.close();
- }
- if(con != null){
- con.close();
- }
- }
- catch (SQLException e) {
- return false;
- }
- if(resultaat > -1){
- return true;
- }
- return false;
- }
- public static Voorstelling getVoorstellingByZaalId(int id){
- Connection con = null;
- ResultSet rs = null;
- PreparedStatement ps = null;
- Voorstelling vo = null;
- try{
- con = Connector.getInstance();
- ps = con.prepareStatement("SELECT * FROM Voorstellingen WHERE idZalen = ?");
- ps.setInt(1, id);
- rs = ps.executeQuery();
- while(rs.next()){
- 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));
- }
- if(ps != null){
- ps.close();
- }
- if(rs != null){
- rs.close();
- }
- if(con != null){
- con.close();
- }
- } catch(SQLException e) {
- return null;
- }
- return vo;
- }
- public static ArrayList<Voorstelling> getVoorstellingByEvenementenId(int id){
- Connection con = null;
- ResultSet rs = null;
- PreparedStatement ps = null;
- ArrayList<Voorstelling> results = new ArrayList<Voorstelling>();
- Voorstelling vo = null;
- try{
- con = Connector.getInstance();
- ps = con.prepareStatement("SELECT * FROM Voorstellingen WHERE idEvenementen = ?");
- ps.setInt(1, id);
- rs = ps.executeQuery();
- while(rs.next()){
- 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));
- results.add(vo);
- }
- if(ps != null){
- ps.close();
- }
- if(rs != null){
- rs.close();
- }
- if(con != null){
- con.close();
- }
- } catch(SQLException e) {
- return null;
- }
- return results;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement