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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
|
public class ConnexionMangaLu {
private static Connection InstanceConnection = null;
/* Création de l'objet gérant les requêtes */
static Statement statement = null;
static ResultSet resultat = null;
private static PreparedStatement prepStat = null;
private static String bdd = "C:/Users/user/Otaku No Shinbun/Base de données/Journal.db";
// static InfosMangafox info;
FormulaireMiseAJour fm;
static FormulaireLu form;
static SauvegardeBDD sv = new SauvegardeBDD();
static ConnexionMangaALire connexionMangaALire;
GestionAffichageLu frame;
/* formatage de la date (jour Mois Année) */
static SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy");
/* utiliser le calendrier par défaut */
static Calendar calendar = Calendar.getInstance();
static String date = format.format(calendar.getTime());
static String user = System.getProperty("user.home");
public static File repBdd = new File(user
+ "/Otaku No Shinbun/Base de données/");
static File repFichier = new File(user + "/Otaku No Shinbun/Fichier");
static File fichierInfos = new File(repFichier + "/Infos Tableau.txt");
static Path logFile = Paths.get(fichierInfos + "");
static File sauvMangasLu = new File(user
+ "/Otaku No Shinbun/Base de données/Sauvegarde.csv");
static File modifBDD = new File(repFichier + "/Modification BDD");
static Path chemin = Paths.get(sauvMangasLu + "");
static int compte = 0;
public static int refSite = 1;
static String titre = null;
static String genre;
static String auteur;
static int annee;
static String dernierChap;
static String dateDernierChap;
static String synopsie;
static String statut;
static String urlImg;
/*
* test de la la presence d'un mangas sur un site et changement de site en
* cas d'abscence
*/
static public boolean titreValide(String titre) {
switch (compte) {
case 0:
InfosMangafox info = new InfosMangafox(titre);
if (!info.titreValide()) {
compte++;
refSite++;
titreValide(titre);
} else {
return true;
}
break;
case 1:
InfosMangareader info1 = new InfosMangareader(titre);
if (!info1.titreValide()) {
compte++;
refSite++;
titreValide(titre);
} else {
return true;
}
break;
case 2:
InfosMangapark info2 = new InfosMangapark(titre);
if (!info2.titreValide()) {
compte++;
refSite++;
titreValide(titre);
} else {
return true;
}
break;
default:
break;
}
compte = 0;
return false;
}
/********************************* Connection à la base de donnée ***********************************/
public static Connection connection() {
/*** Gestion des connections par autorisation d'une connection à la fois ***/
if (InstanceConnection == null) {
System.out
.println("************Nouvelle connection a la base de donnée ************");
try {
// Chargement du driver
Class.forName("org.sqlite.JDBC");
try {
/* connection */
InstanceConnection = DriverManager
.getConnection("jdbc:sqlite:" + bdd);
System.out.println("Connecter");
statement = InstanceConnection.createStatement();
if (!existe(InstanceConnection, "Mangas")) {
String sql = "CREATE TABLE Mangas "
+ "(Titre VARCHAR(40) PRIMARY KEY NOT NULL,"
+ "Genre VARCHAR(200), "
+ "Auteur VARCHAR(50), " + "Annee INTEGER, "
+ "Dernier_Chapitre VARCHAR(5),"
+ "DATE_DERNIER_CHAP VARCHAR(20),"
+ "Dernier_Chapitre_Lu VARCHAR(5) NOT NULL, "
+ "Date_Derniere_Lecture VARCHAR(20), "
+ "Synopsie VARCHAR(600), "
+ "Status VARCHAR(15) )";
statement.executeUpdate(sql);
}
statement.close();
System.out.println("statement fermer");
} catch (SQLException e) {
System.err.println(e.getClass().getName() + ": "
+ e.getMessage());
System.exit(0);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
} else {
// System.out.println("****** Connection déja existante ********* ");
}
return InstanceConnection;
}
/********************************************************************************************************************/
public static String getDate() {
return date;
}
public ResultSet query(String requet) {
try {
resultat = statement.executeQuery(requet);
} catch (SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.out.println("Erreur dans la requete : " + requet);
}
return resultat;
}
/****************************** Ajout d'un Manga ************************************/
@SuppressWarnings("static-access")
public static boolean addManga(String tit, String DernierChapLu) {
/* test de l'existance du manga dans la base de donnée Manga Lu */
if (!existance(tit.toUpperCase())) {
if (titreValide(tit)) {
enregistrement(tit.toUpperCase(), DernierChapLu, date);
connexionMangaALire.connection();
/*
* test de l'existance du manga dans la base de donnée Manga a
* Lire
*/
if (connexionMangaALire.existance(tit.toUpperCase())) {
connexionMangaALire.connection();
// connexionMangaALire.delManga(tit.toUpperCase())
/*
* si le manga existe dans la BDD Manga a Lire on le
* supprime
*/
}
}
} else {
JOptionPane
.showMessageDialog(
null,
"Le Manga est déjà dans le journal.\n Tu seras redirigé vers la mise a jour",
"Attention", JOptionPane.ERROR_MESSAGE);
form.dispose();
return false;
}
return true;
}
/*** Test de l'existance du manga dans la base ***/
public static boolean existance(String titre) {
boolean existe = false;
try {
PreparedStatement preparedStatement = InstanceConnection
.prepareStatement("SELECT * FROM Mangas WHERE Titre = ?");
preparedStatement.setString(1, titre.toUpperCase());
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
existe = true;
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
return existe;
}
/** Recuperation du dernier chapitre pour completer la checkbox ***/
public String infoCheckbox(String tit) {
String etat = null;
switch (refSite) {
case 1:
etat = InfosMangafox.getDernierChap();
break;
case 2:
etat = InfosMangareader.getDernierChap();
break;
case 3:
etat = InfosMangapark.getDernierChap();
break;
default:
break;
}
if (existance(tit.toUpperCase())) {
etat = "invalide";
JOptionPane
.showMessageDialog(
null,
"Le Manga est déjà dans le journal.\n Vous serez redirigé vers la mise a jour",
"Erreur ", JOptionPane.ERROR_MESSAGE);
}
return etat;
}
/** Enregistrement des information du manga **/
public static boolean enregistrement(String Titre, String chap, String date) {
if (chap == null)
JOptionPane
.showMessageDialog(
null,
"La case \" Dernier Scan Lu \" ne peut etre vide ou = à 0 ",
"Erreur", JOptionPane.ERROR_MESSAGE);
else {
try {
prepStat = InstanceConnection
.prepareStatement("INSERT INTO Mangas "
+ "(Titre,Genre,Auteur,Annee,Dernier_Chapitre,Date_Dernier_Chap,"
+ "Dernier_Chapitre_Lu,Date_Derniere_Lecture,Synopsie,Status)"
+ "VALUES(?,?,?,?,?,?,?,?,?,?)");
enreg();
prepStat.setString(1, Titre);
prepStat.setString(2, genre);
prepStat.setString(3, auteur);
prepStat.setInt(4, annee);
prepStat.setString(5, dernierChap);
prepStat.setString(6, dateDernierChap);
prepStat.setString(7, chap);
prepStat.setString(8, date);
prepStat.setString(9, synopsie);
prepStat.setString(10, statut);
prepStat.executeUpdate();
// System.out.println("ConnexionMangaLu :enregistrement: Insertion effectuée Avec Succès");
} catch (SQLException e) {
System.err.println(e.getClass().getName() + ": "
+ e.getMessage());
return false;
}
}
return true;
}
/**************** Permet de determiner sur quel site les informations ont été récupérées *******************/
private static void enreg() {
switch (refSite) {
case 1:
genre = InfosMangafox.getGenre();
auteur = InfosMangafox.getAuteur();
annee = InfosMangafox.getAnnee();
dernierChap = InfosMangafox.getDernierChap();
dateDernierChap = InfosMangafox.getDate();
synopsie = InfosMangafox.getSynopsie();
statut = InfosMangafox.getStatut();
urlImg = InfosMangafox.getUrlImg();
break;
case 2:
genre = InfosMangareader.getGenre();
auteur = InfosMangareader.getAuteur();
annee = InfosMangareader.getAnnee();
dernierChap = InfosMangareader.getDernierChap();
dateDernierChap = InfosMangareader.getDate();
synopsie = InfosMangareader.getSynopsie();
statut = InfosMangareader.getStatut();
break;
case 3:
genre = InfosMangapark.getGenre();
auteur = InfosMangapark.getAuteur();
annee = InfosMangapark.getAnnee();
dernierChap = InfosMangapark.getDernierChap();
dateDernierChap = InfosMangapark.getDate();
synopsie = InfosMangapark.getSynopsie();
statut = InfosMangapark.getStatut();
break;
default:
break;
}
}
/*********************************** Enregistrement du dernier chapitre lu *****************************/
public boolean majManga(String titre, String chap) {
try {
prepStat = InstanceConnection
.prepareStatement("UPDATE Mangas SET Date_Derniere_Lecture = ?,"
+ " Dernier_Chapitre_Lu = ? WHERE Titre= ? ");
prepStat.setString(1, date);
prepStat.setString(2, chap);
prepStat.setString(3, titre);
prepStat.executeUpdate();
} catch (SQLException e) {
System.out
.println("ConnexionMangaLu :enregistrementMAJ :: Syntaxe incorecte");
e.printStackTrace();
return false;
}
return true;
}
public String majMangaCheckBox(String titre) {
String dernierChap = null;
switch (refSite) {
case 1:
dernierChap = InfosMangafox.getDernierChap();
urlImg = InfosMangafox.getUrlImg();
break;
case 2:
dernierChap = InfosMangareader.getDernierChap();
break;
case 3:
dernierChap = InfosMangapark.getDernierChap();
break;
default:
break;
}
return dernierChap;
}
/*************************************** Suppression d'un Manga ********************************/
public boolean delManga(String titre) {
try {
PreparedStatement preparedStatement = InstanceConnection
.prepareStatement("DELETE from Mangas where Titre = ? ");
preparedStatement.setString(1, titre.toUpperCase());
preparedStatement.executeUpdate();
System.out.println("ConnexionMangaLu : delMAnga :: Données supprimer");
} catch (SQLException e) {
System.out.println("ConnexionMangaLu : delManga :: Syntaxe incorecte");
e.printStackTrace();
return false;
}
return true;
}
/*************** Récuperation des données *****************/
public ArrayList<Manga> getAllMangas() {
ArrayList<Manga> result = new ArrayList<>();
try {
PreparedStatement preparedStatement = InstanceConnection
.prepareStatement("SELECT * FROM Mangas order by Titre");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Manga mang = new Manga(resultSet.getString(1),
resultSet.getString(2), resultSet.getString(3),
resultSet.getString(4), resultSet.getString(5),
resultSet.getString(6), resultSet.getString(7),
resultSet.getString(8), resultSet.getString(9),
resultSet.getString(10));
result.add(mang);
}
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public Manga getManga(String titre){
Manga mang = null;
try {
PreparedStatement preparedStatement = InstanceConnection
.prepareStatement("SELECT * FROM Mangas where Titre = ?");
preparedStatement.setString(1, titre);
ResultSet resultSet = preparedStatement.executeQuery();
mang = new Manga(resultSet.getString(1),
resultSet.getString(2), resultSet.getString(3),
resultSet.getString(4), resultSet.getString(5),
resultSet.getString(6), resultSet.getString(7),
resultSet.getString(8), resultSet.getString(9),
resultSet.getString(10));
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
return mang;
}
/*************** Récuperation des données sauvegarder *****************/
public ArrayList<String> recupInfoSauv() {
ArrayList<String> result = new ArrayList<>();
try {
PreparedStatement preparedStatement = InstanceConnection
.prepareStatement("SELECT Titre,Dernier_Chapitre_Lu FROM Mangas order by Titre");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
String ma = resultSet.getString(1) + ";"
+ resultSet.getDouble(2);
String mang = new String(ma);
result.add(mang);
}
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/*** Test de l'existance de la table dans la base de donne **/
private static boolean existe(Connection connection, String nomTable)
throws SQLException {
boolean existe;
DatabaseMetaData dmd = connection.getMetaData();
ResultSet tables = dmd.getTables(connection.getCatalog(), null,
nomTable, null);
existe = tables.next();
tables.close();
return existe;
}
/** Sauvegarde des derniers chapitres sortits **/
public void ecritureInfosMAJ(String titre) {
switch (refSite) {
case 1:
dernierChap = InfosMangafox.getDernierChap();
dateDernierChap = InfosMangafox.getDate();
statut = InfosMangafox.getStatut();
urlImg = InfosMangafox.getUrlImg();
break;
case 2:
dernierChap = InfosMangareader.getDernierChap();
dateDernierChap = InfosMangareader.getDate();
statut = InfosMangareader.getStatut();
break;
case 3:
dernierChap = InfosMangapark.getDernierChap();
dateDernierChap = InfosMangapark.getDate();
statut = InfosMangapark.getStatut();
break;
default:
break;
}
try {
prepStat = InstanceConnection
.prepareStatement("UPDATE Mangas SET DATE_DERNIER_CHAP = ?,"
+ " Dernier_Chapitre = ?,Status= ? WHERE Titre= ? ");
prepStat.setString(1, dateDernierChap);
prepStat.setString(2, dernierChap);
prepStat.setString(3, statut);
prepStat.setString(4, titre);
prepStat.executeUpdate();
System.out.println("connnnexion :ecritureInfosMAJ: Mise à jour de "
+ titre + " Réussite");
} catch (SQLException e) {
System.out
.println("ConnexionMangaLu :ecritureInfosMAJ : Syntaxe incorecte");
e.printStackTrace();
// System.exit(0);
}
try {
new ImageDownloader(urlImg, titre, 0);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/********* Récupération des titres pour mises à jour des scans *****************/
public ArrayList<String> lectureTitre() {
ArrayList<String> result = new ArrayList<>();
try {
PreparedStatement preparedStatement = InstanceConnection
.prepareStatement("SELECT Titre FROM Mangas");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
result.add(resultSet.getString(1));
}
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/***
* Modification de la structure de la base de donnée * public void
* modifBDD() throws IOException {
*
* try {
*
* sv.sauvegardeDeLaBase(1); con = DriverManager .getConnection(
* "jdbc:sqlite:C:/Users/user/Otaku No Shinbun/Base de données/test.db");
*
* statement = con.createStatement();
*
* String sql = "DROP TABLE Mangas"; statement.executeUpdate(sql);
*
* connection(); } catch (SQLException e) { e.printStackTrace(); } catch
* (Exception e) { e.printStackTrace(); }
*
* restauration();
*
* modifBDD.createNewFile();
*
* }
*
* /** Restauration des données sauvegarder sur fichier
**/
public static boolean restauration() {
/* on lit toutes les lignes du fichier */
try {
final List<String> dataList1 = Files.readAllLines(chemin);
for (String string : dataList1) {
String[] mang = string.split(";");
String tit = null;
String chap = null;
for (int i = 0; i < mang.length; i++) {
tit = mang[0];
chap = mang[1];
}
if (titreValide(tit)) {
connection();
addManga(tit, chap);
System.out.println("Réinsertion de :" + tit + " réussi");
}
}
InstanceConnection.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
public Manga infos(String titre) {
Manga mg = null;
try {
PreparedStatement preparedStatement = InstanceConnection
.prepareStatement("SELECT Genre,Auteur,Annee,Dernier_Chapitre,Synopsie, Status FROM Mangas WHERE Titre = ?");
/*** Requete SQL a modifié **/
preparedStatement.setString(1, titre);
ResultSet resultSet = preparedStatement.executeQuery();
mg = new Manga(resultSet.getString(1), resultSet.getString(2),
resultSet.getString(3), resultSet.getString(4),
resultSet.getString(5), resultSet.getString(6));
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
return mg;
}
/********* Récupération du statut *****************/
public String recupStatut(String titre) {
String result = null;
try {
PreparedStatement preparedStatement = InstanceConnection
.prepareStatement("SELECT Status FROM Mangas WHERE Titre = ? ");
preparedStatement.setString(1, titre);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
result = resultSet.getString(1);
}
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
} |