1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| package com.classrooms.dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import com.classrooms.beans.BeanException;
import com.classrooms.beans.Video;
import com.classrooms.dao.*;
public class VideoDaoImpl implements VideoDao {
private DaoFactory daoFactory;
VideoDaoImpl(DaoFactory daoFactory) {
this.daoFactory = daoFactory;
}
@Override
public void ajouter(Video video) throws DaoException {
Connection connexion = null;
PreparedStatement preparedStatement = null;
try {
connexion = daoFactory.getConnection(); // erreur sur getConnection
preparedStatement = connexion.prepareStatement("INSERT INTO video(fichier, titre, commentaire) VALUES(?, ?, ?, ?);");
preparedStatement.setString(1, video.getFichier());
preparedStatement.setString(3, video.getTitre());
preparedStatement.setString(4, video.getCommentaire());
preparedStatement.executeUpdate();
connexion.commit();
} catch (SQLException e) {
try {
if (connexion != null) {
connexion.rollback();
}
} catch (SQLException e2) {
}
throw new DaoException("Impossible de communiquer avec la base de données SQLException");
}
finally {
try {
if (connexion != null) {
connexion.close();
}
} catch (SQLException e) {
throw new DaoException("Impossible de communiquer avec la base de données SQLException");
}
}
}
@Override
public List<Video> lister() throws DaoException {
List<Video> videos = new ArrayList<Video>();
Connection connexion = null;
Statement statement = null;
ResultSet resultat = null;
try {
connexion = DaoFactory.getConnection(); // erreur sur getConnction
statement = connexion.createStatement();
resultat = statement.executeQuery("SELECT fichier, titre, commentaire FROM video;");
while (resultat.next()) {
String fichier = resultat.getString("fichier");
String titre = resultat.getString("titre");
String commentaire = resultat.getString("commentaire");
Video video = new Video();
video.setFichier(fichier);
video.setTitre(titre);
video.setCommentaire(commentaire);
videos.add(video);
}
} catch (SQLException e) {
throw new DaoException("Impossible de communiquer avec la base de données; SQLException");
} catch (BeanException e) {
throw new DaoException("Les données de la base sont invalides: bean");
}
finally {
try {
if (connexion != null) {
connexion.close();
}
} catch (SQLException e) {
throw new DaoException("Impossible de communiquer avec la base de données : fermeture");
}
}
return videos;
}
} |
Partager