From ccc3c1885407e6bb09bc7f207692f23ffa89639f Mon Sep 17 00:00:00 2001 From: NicolasFabregas Date: Thu, 21 Jan 2021 11:38:27 +0100 Subject: [PATCH] =?UTF-8?q?Creation=20de=20la=20methode=20pour=20delete=20?= =?UTF-8?q?les=20valeurs=20apr=C3=A8s=202017027,=20Creation=20de=20la=20me?= =?UTF-8?q?thode=20drop=20table?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/fr/myny/database/DataBase.java | 167 +++++++++++++++++- .../java/fr/myny/database/DataBaseTest.java | 12 ++ 2 files changed, 172 insertions(+), 7 deletions(-) diff --git a/src/main/java/fr/myny/database/DataBase.java b/src/main/java/fr/myny/database/DataBase.java index 328742e..d194cf0 100644 --- a/src/main/java/fr/myny/database/DataBase.java +++ b/src/main/java/fr/myny/database/DataBase.java @@ -82,6 +82,7 @@ public class DataBase { Scanner sc = new Scanner(new File(filePath)); sc.useDelimiter(";|\\n"); //sets the delimiter pattern int i=0,j; + long val; String line=sc.nextLine(); line=""; String date, jour, mois, an; @@ -89,7 +90,8 @@ public class DataBase { j=(i% NBCOL)+1; if(j==1) { line=line+'('; - line=line+sc.next().replace(",","."); + val=Long.parseLong(sc.next().replace(",",".")); + line=line+val; line=line+';'; } if(j< NBCOL && j>1) { @@ -201,17 +203,17 @@ public class DataBase { " numero_chance TINYINT,"+ //10 " combinaison_gagnante_en_ordre_croissant varchar(20),"+//11 VARCHAR " nombre_de_gagnant_au_rang1 INTEGER,"+ //12 - " rapport_du_rang1 INTEGER,"+ //13 + " rapport_du_rang1 float(53),"+ //13 " nombre_de_gagnant_au_rang2 INTEGER,"+ //14 - " rapport_du_rang2 INTEGER,"+ //15 + " rapport_du_rang2 float(53),"+ //15 " nombre_de_gagnant_au_rang3 INTEGER,"+ //16 - " rapport_du_rang3 INTEGER,"+ //17 + " rapport_du_rang3 float(53),"+ //17 " nombre_de_gagnant_au_rang4 INTEGER,"+ //18 - " rapport_du_rang4 INTEGER,"+ //19 + " rapport_du_rang4 float(53),"+ //19 " nombre_de_gagnant_au_rang5 INTEGER,"+ //20 - " rapport_du_rang5 INTEGER,"+ //21 + " rapport_du_rang5 float(53),"+ //21 " nombre_de_gagnant_au_rang6 INTEGER,"+ //22 - " rapport_du_rang6 INTEGER,"+ //23 + " rapport_du_rang6 float(53),"+ //23 " numero_jokerplus INTEGER,"+ //24 " devise VARCHAR(10));"; //25 VARCHAR //System.out.println(sql); @@ -433,6 +435,157 @@ public class DataBase { } + public String updateDataBasev2() throws FileNotFoundException { + String sql="INSERT INTO myny.Test_Table" + //le debut de la requete, specifiant laction a effectuer, la table et la db + " (annee_numero_de_tirage, "+ + " jour_de_tirage, "+ + " date_de_tirage,"+ + " date_de_forclusion,"+ + " boule_1 ,"+ + " boule_2 ,"+ + " boule_3 ,"+ + " boule_4 ,"+ + " boule_5 ,"+ + " numero_chance ,"+ + " combinaison_gagnante_en_ordre_croissant ,"+ + " nombre_de_gagnant_au_rang1 ,"+ + " rapport_du_rang1 ,"+ + " nombre_de_gagnant_au_rang2 ,"+ + " rapport_du_rang2 ,"+ + " nombre_de_gagnant_au_rang3 ,"+ + " rapport_du_rang3 ,"+ + " nombre_de_gagnant_au_rang4 ,"+ + " rapport_du_rang4 ,"+ + " nombre_de_gagnant_au_rang5 ,"+ + " rapport_du_rang5 ,"+ + " nombre_de_gagnant_au_rang6 ,"+ + " rapport_du_rang6 ,"+ + " numero_jokerplus ,"+ + " devise ) VALUES \n"; + try { + conn = this.getConnection(); //initialisation de la connexion + if(conn!=null) { //si la co est bonne + Scanner sc = new Scanner(new File(filePath)); //lire le fichier donne + sc.useDelimiter(";|\\n"); //les delimiteurs seront ; et \n + sc.nextLine(); //on ne conserve pas la premiere ligne, contenant les metadonnees + long cpt=0; + Object[] champs; + int i=0,j=0, nbValAj=0; + String date, jour, mois, an; + String line=""; + long anEntre=Long.parseLong(sc.next()); //lecture de l id en haut du tableau(donc le plus grand, cest ce qu on a constate en lisant le csv + //System.out.println("numero en haut du fichier csv :"+anEntre); + Statement stmt = conn.createStatement(); //recuperation du nb de lignes dans la table + ResultSet rs = stmt.executeQuery("SELECT MAX(annee_numero_de_tirage) FROM myny.Test_Table;"); + rs.next(); + long maxvaldb=rs.getLong(1); + //System.out.println("numero max de la db :"+rs.getLong(1)); + /*comme les nombres de la premiere col sont decroissants, le nb le plus grand est lu en premier, + on va donc lire les nombres jusqua arriver a la plus haute valeur entree dans la table. On ne + pourrait probablement pas utiliser cette methode pour remplir la table en entier*/ + while(anEntre>maxvaldb) { + nbValAj++;//des quon rentre dans la boucle, on sait qu'on va devoir mettre a jour la table car cette valeur est>0 + for (i = 0; i < NBCOL; i++) {//on remplit la ligne en effectuant une lecture par col + + j = (i % NBCOL) + 1; + if (j == 1) { + line = line + "(?;";//debut de la ligne a jouter + //champs[cpt++]=anEntre; + //line = line + anEntre; + //line = line + ';';//on met un point virgule mais on le remplacera plus tard + //sc.next(); + } + if (j < NBCOL && j > 1) {//pour chaque colonne, on va faire en sorte de mettre le champ dans la onne mise en fore pour qu'il soit accepté par la db + if (j == 2 || j == 11) { + line = line + "\'" + sc.next().replace(",", ".") + "\',"; + } else if (j == 3 || j == 4) {//pour les col 3 et 4, il s'agit d'une date, on va donc passer dela forme jj-mm-aaaa a la forme aaaa-mm-jj + date = sc.next().replace(",", "."); + jour = date.substring(0, 2); + mois = date.substring(3, 5); + an = date.substring(6, 10); + line = line + "\'" + an + "-" + mois + "-" + jour + "\',"; + } else {//pour les autres col, on va simplement les remplir + line = line + sc.next().replace(",", "."); + line = line + ';'; + } + } + if (j == NBCOL) {//pour la derniere col + line = line + "\'" + sc.next().replace(",", ".") + "\'"; + line = line + ')';//on la remplit + line = line.replaceAll("\\s", "");//on remplace les caracteres qui nous derangent + line = line.replaceAll(";", ","); + anEntre = Long.parseLong(sc.next());//on lit le prochain id de l'entree + if (anEntre > maxvaldb) {//on defini si on doit encore ajouter des lignes a la requete + line = line + ","; + } else {//ou si on doit cloturer la requete + line = line + ";"; + } + //System.out.println(line); + sql = sql + line + "\n";//on ajoute la ligne a la requete + line = ""; + } + } + } + System.out.println(sql); + if (nbValAj>0) {//on effectue la requete si on a des maj a faire + try { + conn = this.getConnection(); + if (conn != null) { + stmt = conn.createStatement(); + stmt.executeQuery(sql); + conn.commit(); + //conn.close(); + System.out.println("update validee"); + } + } catch (SQLException e) { + System.out.println("oskour into update"); + System.out.println(e.getMessage()); + } + } + } + } catch (SQLException e) { + System.out.println("oskour update"); + System.out.println(e.getMessage()); + } + return sql; + } + + public void removeLines() throws FileNotFoundException{ + String sql="delete from myny.Test_Table where annee_numero_de_tirage >2017027"; + try { + conn = this.getConnection(); + if(conn!=null) { + Statement ps = conn.createStatement(); + ps.executeQuery(sql); + conn.commit(); + //conn.close(); + System.out.println("statement cree"); + } + } catch (SQLException e) { + System.out.println("oskour"); + System.out.println(e.getMessage()); + } + + } + + public void dropTable() throws FileNotFoundException{ + String sql="drop table myny.Test_Table"; + try { + conn = this.getConnection(); + if(conn!=null) { + Statement ps = conn.createStatement(); + ps.executeQuery(sql); + conn.commit(); + //conn.close(); + System.out.println("statement cree"); + } + } catch (SQLException e) { + System.out.println("oskour"); + System.out.println(e.getMessage()); + } + + } + diff --git a/src/test/java/fr/myny/database/DataBaseTest.java b/src/test/java/fr/myny/database/DataBaseTest.java index 541e6c7..2c74bd3 100644 --- a/src/test/java/fr/myny/database/DataBaseTest.java +++ b/src/test/java/fr/myny/database/DataBaseTest.java @@ -58,4 +58,16 @@ class DataBaseTest { Connection maCo=maDB.getConnection(); assertNotNull(maCo); } + + @Test + public void removeLines() throws FileNotFoundException{ + DataBase maDB=new DataBase(); + maDB.removeLines(); + } + + @Test + public void dropTable() throws FileNotFoundException{ + DataBase maDB=new DataBase(); + maDB.dropTable(); + } } \ No newline at end of file