Toutes les méthodes finies fonctionnelles, commentees, avec la javadoc, vérifiées une à une, et les tests valides (pour le cas utilisé)
This commit is contained in:
parent
47c6215386
commit
7af2688494
2 changed files with 252 additions and 223 deletions
|
|
@ -10,162 +10,125 @@ import java.io.*;
|
||||||
|
|
||||||
public class DataBase {
|
public class DataBase {
|
||||||
//public static String url="jdbc:mariadb://vachot.fr:3306?user=mynynicolas&password=Bw0po64*";
|
//public static String url="jdbc:mariadb://vachot.fr:3306?user=mynynicolas&password=Bw0po64*";
|
||||||
|
public static final int NBCOL =25;//le nombre de coldu fichier csv
|
||||||
public static final int NBCOL =25;
|
public static final String url="jdbc:mariadb://phpmyadmin.vachot.fr:3306?db=myny&user=mynynicolas&password=Bw0po64*";//l'acces à la db
|
||||||
public static final String url="jdbc:mariadb://phpmyadmin.vachot.fr:3306?db=myny&user=mynynicolas&password=Bw0po64*";
|
Connection conn;//la connexion a la db
|
||||||
public static final String filePath="c:/Users/cocof/Bureau/nouveau_loto.csv";
|
|
||||||
Connection conn;
|
|
||||||
/**
|
/**
|
||||||
* Le constructeur de DataBase
|
* Le constructeur de DataBase
|
||||||
*/
|
*/
|
||||||
public DataBase(){
|
public DataBase(){
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* La methode de connection
|
||||||
|
* @return conn la connection a la db
|
||||||
|
*/
|
||||||
|
public Connection getConnection() {
|
||||||
|
conn = null;
|
||||||
|
try {
|
||||||
|
conn = DriverManager.getConnection(url);
|
||||||
|
//conn = DriverManager.getConnection("jdbc:mariadb://vachot.fr:3306?user=mynynicolas&password=Bw0po64*");
|
||||||
|
//conn = DriverManager.getConnection("jdbc:mariadb://phpmyadmin.vachot.fr:3306/myny?user=mynynicolas&password=Bw0po64*");
|
||||||
|
//conn = DriverManager.getConnection("jdbc:mariadb:../../../../../../test.db");
|
||||||
|
if(conn!=null) {
|
||||||
|
System.out.println("Connecte a la db");
|
||||||
|
System.out.println(conn);
|
||||||
|
}
|
||||||
|
}catch(SQLException e) {
|
||||||
|
System.out.println("getConnection on a des pb:" + e.getMessage());
|
||||||
|
}
|
||||||
|
return conn;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* La methode dimport de donnees depuis la base de donnees
|
* La methode d import de donnees depuis la base de donnees
|
||||||
*/
|
*/
|
||||||
public void importDataBase(){
|
public void importDataBase(){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* La methode de creation de la base de donnees
|
* La methode de creation de la table
|
||||||
|
* stmt Statement la variable pour créer la declaration
|
||||||
|
* sql String la requete
|
||||||
|
* @return res, int le resultat de la requete (nb de ligne affectees)
|
||||||
*/
|
*/
|
||||||
public void createDataBase() {
|
public int createTable() {
|
||||||
String sql="CREATE TABLE IF NOT EXISTS myny.Test_Table ("+
|
int res=-1;
|
||||||
" annee_numero_de_tirage INTEGER,"+ //1
|
Statement stmt;
|
||||||
" jour_de_tirage VARCHAR(10),"+ //2 VARCHAR
|
String sql="CREATE TABLE IF NOT EXISTS myny.Test_Table ("+ //declaration de la table, de ses colonnes et de leur type et leur taille (on aurai pu ajouter des param supplementaires)
|
||||||
" date_de_tirage DATE,"+ //3 DATE
|
" annee_numero_de_tirage INTEGER,"+ //1 long
|
||||||
" date_de_forclusion DATE,"+ //4 DATE
|
" jour_de_tirage VARCHAR(10),"+ //2 String
|
||||||
" boule_1 TINYINT,"+ //5
|
" date_de_tirage DATE,"+ //3 String
|
||||||
" boule_2 TINYINT,"+ //6
|
" date_de_forclusion DATE,"+ //4 String
|
||||||
" boule_3 TINYINT,"+ //7
|
" boule_1 TINYINT,"+ //5 long
|
||||||
" boule_4 TINYINT,"+ //8
|
" boule_2 TINYINT,"+ //6 long
|
||||||
" boule_5 TINYINT,"+ //9
|
" boule_3 TINYINT,"+ //7 long
|
||||||
" numero_chance TINYINT,"+ //10
|
" boule_4 TINYINT,"+ //8 long
|
||||||
" combinaison_gagnante_en_ordre_croissant varchar(20),"+//11 VARCHAR
|
" boule_5 TINYINT,"+ //9 long
|
||||||
" nombre_de_gagnant_au_rang1 INTEGER,"+ //12
|
" numero_chance TINYINT,"+ //10 long
|
||||||
" rapport_du_rang1 float(53),"+ //13 flo
|
" combinaison_gagnante_en_ordre_croissant varchar(20),"+//11 String
|
||||||
" nombre_de_gagnant_au_rang2 INTEGER,"+ //14
|
" nombre_de_gagnant_au_rang1 INTEGER,"+ //12 long
|
||||||
" rapport_du_rang2 float(53),"+ //15 flo
|
" rapport_du_rang1 float(53),"+ //13 double
|
||||||
" nombre_de_gagnant_au_rang3 INTEGER,"+ //16
|
" nombre_de_gagnant_au_rang2 INTEGER,"+ //14 long
|
||||||
" rapport_du_rang3 float(53),"+ //17 flo
|
" rapport_du_rang2 float(53),"+ //15 double
|
||||||
" nombre_de_gagnant_au_rang4 INTEGER,"+ //18
|
" nombre_de_gagnant_au_rang3 INTEGER,"+ //16 long
|
||||||
" rapport_du_rang4 float(53),"+ //19 flo
|
" rapport_du_rang3 float(53),"+ //17 double
|
||||||
" nombre_de_gagnant_au_rang5 INTEGER,"+ //20
|
" nombre_de_gagnant_au_rang4 INTEGER,"+ //18 long
|
||||||
" rapport_du_rang5 float(53),"+ //21 flo
|
" rapport_du_rang4 float(53),"+ //19 double
|
||||||
" nombre_de_gagnant_au_rang6 INTEGER,"+ //22
|
" nombre_de_gagnant_au_rang5 INTEGER,"+ //20 long
|
||||||
" rapport_du_rang6 float(53),"+ //23 flo
|
" rapport_du_rang5 float(53),"+ //21 double
|
||||||
" numero_jokerplus INTEGER,"+ //24
|
" nombre_de_gagnant_au_rang6 INTEGER,"+ //22 long
|
||||||
" devise VARCHAR(10));"; //25 VARCHAR
|
" rapport_du_rang6 float(53),"+ //23 double
|
||||||
|
" numero_jokerplus INTEGER,"+ //24 long
|
||||||
|
" devise VARCHAR(10));"; //25 String
|
||||||
//System.out.println(sql);
|
//System.out.println(sql);
|
||||||
try {
|
try {
|
||||||
conn = DriverManager.getConnection(url);
|
conn = DriverManager.getConnection(url); //connection a la db
|
||||||
if(conn!=null) {
|
if(conn!=null) {
|
||||||
Statement stmt = conn.createStatement();
|
stmt = conn.createStatement(); //creation de la declaration
|
||||||
stmt.executeQuery(sql);
|
res=stmt.executeUpdate(sql); //execution de la declaration
|
||||||
|
//stmt.executeQuery(sql);
|
||||||
conn.commit();
|
conn.commit();
|
||||||
//conn.close();
|
//conn.close();
|
||||||
System.out.println("statement cree");
|
System.out.println("statement cree");
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.out.println("oskour");
|
System.out.println("create table probleme, verifier si le pb ne vient pas de executeupdate au lieu executequery");
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
/*try(Statement mySt = conn.createStatement()){
|
return res;
|
||||||
mySt.execute(//"CREATE SCHEMA IF NOT EXISTS Connection;"+
|
|
||||||
"CREATE TABLE IF NOT EXISTS myny.Test_Table ("+
|
|
||||||
"annee_numero_de_tirage INTEGER,"+
|
|
||||||
"jour_de_tirage CHAR(10),"+
|
|
||||||
"date_de_tirage DATE,"+
|
|
||||||
"date_de_forclusion DATE,"+
|
|
||||||
"boule_1 TINYINT,"+
|
|
||||||
"boule_2 TINYINT,"+
|
|
||||||
"boule_3 TINYINT,"+
|
|
||||||
"boule_4 TINYINT,"+
|
|
||||||
"boule_5 TINYINT,"+
|
|
||||||
"numero_chance TINYINT,"+
|
|
||||||
"combinaison_gagnante_en_ordre_croissantchar(20),"+
|
|
||||||
"nombre_de_gagnant_au_rang1 INTEGER,"+
|
|
||||||
"rapport_du_rang1 INTEGER,"+
|
|
||||||
"nombre_de_gagnant_au_rang2 INTEGER,"+
|
|
||||||
"rapport_du_rang2 INTEGER,"+
|
|
||||||
"nombre_de_gagnant_au_rang3 INTEGER,"+
|
|
||||||
"rapport_du_rang3 INTEGER,"+
|
|
||||||
"nombre_de_gagnant_au_rang4 INTEGER,"+
|
|
||||||
"rapport_du_rang4 INTEGER,"+
|
|
||||||
"nombre_de_gagnant_au_rang5 INTEGER,"+
|
|
||||||
"rapport_du_rang5 INTEGER,"+
|
|
||||||
"nombre_de_gagnant_au_rang6 INTEGER,"+
|
|
||||||
"rapport_du_rang6 INTEGER,"+
|
|
||||||
"numero_jokerplus INTEGER,"+
|
|
||||||
"deviseCHAR(10));");
|
|
||||||
conn.commit();
|
|
||||||
//mySt.close();
|
|
||||||
//conn.close();
|
|
||||||
}
|
|
||||||
catch(Exception e){
|
|
||||||
System.out.println(e);
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
}*/
|
|
||||||
/*
|
|
||||||
try {
|
|
||||||
PreparedStatement preparedStatement = connexion.prepareStatement("INSERT INTO noms(nom, prenom) VALUES(?, ?);");
|
|
||||||
preparedStatement.setString(1, utilisateur.getNom());
|
|
||||||
preparedStatement.setString(2, utilisateur.getPrenom());
|
|
||||||
|
|
||||||
preparedStatement.executeUpdate();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}*/
|
|
||||||
// try {
|
|
||||||
// Class.forName("com.mysql.jdbc.Driver");
|
|
||||||
// }
|
|
||||||
// catch (ClassNotFoundException e) {
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// try {
|
|
||||||
// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306");
|
|
||||||
// } catch (SQLException e) {
|
|
||||||
// System.out.println("oskour " +e.getStackTrace());
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Statement statement = null;
|
|
||||||
// ResultSet resultat = null;
|
|
||||||
// try {
|
|
||||||
// statement = conn.createStatement();
|
|
||||||
//
|
|
||||||
// // Exécution de la requête
|
|
||||||
// resultat = statement.executeQuery("SELECT nom, prenom FROM noms;");
|
|
||||||
//
|
|
||||||
// // Récupération des données
|
|
||||||
// while (resultat.next()) {
|
|
||||||
// String nom = resultat.getString("nom");
|
|
||||||
// String prenom = resultat.getString("prenom");
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// catch (SQLException e) {
|
|
||||||
// System.out.println(e);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* La methode de remplissage de la base de donnees
|
* La methode de remplissage de la base de donnees
|
||||||
|
* line String la ligne a ajouter
|
||||||
|
* i int, un compteur de valeurs lues,
|
||||||
|
* j int, qui suit i%NBCOL
|
||||||
|
* nbValAj int qui augmente si on doit ajouter des lignes
|
||||||
|
* date String, la valeur de la date lue
|
||||||
|
* jour String, le jour lu
|
||||||
|
* mois String, le mois lu
|
||||||
|
* an String, l annee lue
|
||||||
|
* mesL tableau dans lequel seront stockés tous les long
|
||||||
|
* mesS tableau dans lequel seront stockés tous les String
|
||||||
|
* mesD tableau dans lequel seront stockés tous les Double
|
||||||
|
* sc Scanner, l objet qui servira a lire le fichier csv
|
||||||
|
* @param filePath String le fichier et son chemin
|
||||||
|
* @return res int le resultat de la requete(nb de ligne affectees)
|
||||||
*/
|
*/
|
||||||
public void fillDataBase(){
|
public int fillTable(String filePath){
|
||||||
|
int res=-1;
|
||||||
|
Scanner sc;
|
||||||
int i=0,j, nbValAj=0;
|
int i=0,j, nbValAj=0;
|
||||||
long val;
|
|
||||||
String line="";
|
String line="";
|
||||||
String date, jour, mois, an;
|
String date, jour, mois, an;
|
||||||
long[][] mesL=new long[4000][NBCOL+1];
|
long[][] mesL=new long[4000][NBCOL+1];
|
||||||
String[][]mesS=new String[4000][NBCOL+1];
|
String[][]mesS=new String[4000][NBCOL+1];
|
||||||
Double[][]mesD=new Double[4000][NBCOL+1];
|
Double[][]mesD=new Double[4000][NBCOL+1];
|
||||||
String sql="INSERT INTO myny.Test_Table" +
|
String sql="INSERT INTO myny.Test_Table" +//definit la requete
|
||||||
" (annee_numero_de_tirage, "+
|
" (annee_numero_de_tirage, "+
|
||||||
" jour_de_tirage, "+
|
" jour_de_tirage, "+
|
||||||
" date_de_tirage,"+
|
" date_de_tirage,"+
|
||||||
|
|
@ -192,11 +155,10 @@ public class DataBase {
|
||||||
" numero_jokerplus ,"+
|
" numero_jokerplus ,"+
|
||||||
" devise ) VALUES \n";
|
" devise ) VALUES \n";
|
||||||
try {
|
try {
|
||||||
Scanner sc = new Scanner(new File(filePath));
|
sc = new Scanner(new File(filePath));//lire le fichier csv
|
||||||
sc.useDelimiter(";|\\n"); //sets the delimiter pattern
|
sc.useDelimiter(";|\\n"); //definit les separateurs utilisés
|
||||||
line=sc.nextLine();
|
sc.nextLine();//passe la premiere ligne, les metadonnees
|
||||||
line="";
|
while (sc.hasNext()){//tant qu'on a des champs non-vides, on va remplir la ligne et les cases specifiques des tableaux a 2 dim correspondant au champ lu
|
||||||
while (sc.hasNext()){
|
|
||||||
j=(i% NBCOL)+1;
|
j=(i% NBCOL)+1;
|
||||||
if(j==1) {
|
if(j==1) {
|
||||||
line=line+"(?,";
|
line=line+"(?,";
|
||||||
|
|
@ -246,7 +208,7 @@ public class DataBase {
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
if (nbValAj>0) {//si on a des modifications
|
if (nbValAj>0) {//si on a des modifications
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps;
|
||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
if (conn != null) {
|
if (conn != null) {
|
||||||
|
|
@ -256,7 +218,6 @@ public class DataBase {
|
||||||
if((b>1&&b<5)||b==11||b==25){
|
if((b>1&&b<5)||b==11||b==25){
|
||||||
System.out.println("a: "+a+" et b: "+b);
|
System.out.println("a: "+a+" et b: "+b);
|
||||||
ps.setString((a)*25+b, mesS[a][b]);
|
ps.setString((a)*25+b, mesS[a][b]);
|
||||||
|
|
||||||
}
|
}
|
||||||
else if(b > 12 && b < 24 && b % 2 == 1){
|
else if(b > 12 && b < 24 && b % 2 == 1){
|
||||||
System.out.println("a: "+a+" et b: "+b);
|
System.out.println("a: "+a+" et b: "+b);
|
||||||
|
|
@ -271,31 +232,43 @@ public class DataBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ps.executeQuery();//puis on execute la requete
|
res=ps.executeUpdate();
|
||||||
|
//ps.executeQuery();//puis on execute la requete
|
||||||
System.out.println("update validee");
|
System.out.println("update validee");
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.out.println("oskour into update");
|
System.out.println("filldatebase probleme, verifier si le pb ne viens pas de executeupdate au lieu executequery");
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* La methode de mise a jour de la base de donnees
|
* La methode de mise a jour de la base de donnees
|
||||||
* @return sql String, contenant la requete SQL
|
* @return res int, le resultat de la requete(nb de ligne affectees)
|
||||||
* @var sc Scanner, l objet qui servira a lire le fichier csv
|
* sc Scanner, l objet qui servira a lire le fichier csv
|
||||||
* @var line String, ligne contenant l'ajout a effectuer
|
* line String, ligne contenant l'ajout a effectuer
|
||||||
* @var i int, un compteur de valeurs lues,
|
* i int, un compteur de valeurs lues,
|
||||||
* @var j int, qui suit i%NBCOL
|
* j int, qui suit i%NBCOL
|
||||||
* @var nbValAj int qui augmente si on doit ajouter des lignes
|
* nbValAj int qui augmente si on doit ajouter des lignes
|
||||||
* @var date String, la valeur de la date lue
|
* date String, la valeur de la date lue
|
||||||
* @var jour String, le jour lu
|
* jour String, le jour lu
|
||||||
* @var mois String, le mois lu
|
* mois String, le mois lu
|
||||||
* @var an String, l annee lue
|
* an String, l annee lue
|
||||||
* @var anEntre long, valeur lue pour l annee et le numero de tirage. sert a definir si la valeur est a ajouter dans la table
|
* anEntre long, valeur lue pour l annee et le numero de tirage. sert a definir si la valeur est a ajouter dans la tabl
|
||||||
|
* @deprecated
|
||||||
*/
|
*/
|
||||||
public String updateDataBase() throws FileNotFoundException {
|
public int updateTable(String filePath) throws FileNotFoundException {
|
||||||
|
ResultSet rs=null;
|
||||||
|
Scanner sc;
|
||||||
|
int res=-1;
|
||||||
|
int i,j, nbValAj=0;
|
||||||
|
String date, jour, mois, an;
|
||||||
|
String line="";
|
||||||
|
long anEntre;
|
||||||
|
Statement stmt;
|
||||||
|
long maxvaldb;
|
||||||
String sql="INSERT INTO myny.Test_Table" + //le debut de la requete, specifiant laction a effectuer, la table et la db
|
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, "+
|
" (annee_numero_de_tirage, "+
|
||||||
" jour_de_tirage, "+
|
" jour_de_tirage, "+
|
||||||
|
|
@ -325,18 +298,15 @@ public class DataBase {
|
||||||
try {
|
try {
|
||||||
conn = this.getConnection(); //initialisation de la connexion
|
conn = this.getConnection(); //initialisation de la connexion
|
||||||
if(conn!=null) { //si la co est bonne
|
if(conn!=null) { //si la co est bonne
|
||||||
Scanner sc = new Scanner(new File(filePath)); //lire le fichier donne
|
sc = new Scanner(new File(filePath)); //lire le fichier donne
|
||||||
sc.useDelimiter(";|\\n"); //les delimiteurs seront ; et \n
|
sc.useDelimiter(";|\\n"); //les delimiteurs seront ; et \n
|
||||||
sc.nextLine(); //on ne conserve pas la premiere ligne, contenant les metadonnees
|
sc.nextLine(); //on ne conserve pas la premiere ligne, contenant les metadonnees
|
||||||
int i=0,j=0, nbValAj=0;
|
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
|
||||||
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);
|
//System.out.println("numero en haut du fichier csv :"+anEntre);
|
||||||
Statement stmt = conn.createStatement(); //recuperation du nb de lignes dans la table
|
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 = stmt.executeQuery("SELECT MAX(annee_numero_de_tirage) FROM myny.Test_Table;");
|
||||||
rs.next();
|
rs.next();
|
||||||
long maxvaldb=rs.getLong(1);
|
maxvaldb=rs.getLong(1);
|
||||||
//System.out.println("numero max de la db :"+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,
|
/*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
|
on va donc lire les nombres jusqua arriver a la plus haute valeur entree dans la table. On ne
|
||||||
|
|
@ -353,20 +323,20 @@ public class DataBase {
|
||||||
}
|
}
|
||||||
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 < 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) {
|
if (j == 2 || j == 11) {
|
||||||
line = line + "\'" + sc.next().replace(",", ".") + "\',";
|
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
|
} 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(",", ".");
|
date = sc.next().replace(",", ".");
|
||||||
jour = date.substring(0, 2);
|
jour = date.substring(0, 2);
|
||||||
mois = date.substring(3, 5);
|
mois = date.substring(3, 5);
|
||||||
an = date.substring(6, 10);
|
an = date.substring(6, 10);
|
||||||
line = line + "\'" + an + "-" + mois + "-" + jour + "\',";
|
line = line + "'" + an + "-" + mois + "-" + jour + "',";
|
||||||
} else {//pour les autres col, on va simplement les remplir
|
} else {//pour les autres col, on va simplement les remplir
|
||||||
line = line + sc.next().replace(",", ".");
|
line = line + sc.next().replace(",", ".");
|
||||||
line = line + ';';
|
line = line + ';';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (j == NBCOL) {//pour la derniere col
|
if (j == NBCOL) {//pour la derniere col
|
||||||
line = line + "\'" + sc.next().replace(",", ".") + "\'";
|
line = line + "'" + sc.next().replace(",", ".") + "'";
|
||||||
line = line + ')';//on la remplit
|
line = line + ')';//on la remplit
|
||||||
line = line.replaceAll("\\s", "");//on remplace les caracteres qui nous derangent
|
line = line.replaceAll("\\s", "");//on remplace les caracteres qui nous derangent
|
||||||
line = line.replaceAll(";", ",");
|
line = line.replaceAll(";", ",");
|
||||||
|
|
@ -388,53 +358,55 @@ public class DataBase {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
if (conn != null) {
|
if (conn != null) {
|
||||||
stmt = conn.createStatement();
|
stmt = conn.createStatement();
|
||||||
stmt.executeQuery(sql);
|
res=stmt.executeUpdate(sql);
|
||||||
|
//stmt.executeQuery(sql);
|
||||||
conn.commit();
|
conn.commit();
|
||||||
//conn.close();
|
//conn.close();
|
||||||
System.out.println("update validee");
|
System.out.println("update validee");
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.out.println("oskour into update");
|
System.out.println("updateDataBase probleme");
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.out.println("oskour update");
|
System.out.println("updateDataBase probleme");
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
return sql;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* La methode de mise a jour de la base de donnees 2 qui evite les injections sql
|
* La methode de mise a jour de la base de donnees 2 qui evite les injections sql
|
||||||
* @return sql String, contenant la requete SQL
|
* @return rs, int, le resultat de la requete (nb de lignes affectees)
|
||||||
* @var mesL tableau dans lequel seront stockés tous les long
|
* mesL tableau dans lequel seront stockés tous les long
|
||||||
* @var mesS tableau dans lequel seront stockés tous les String
|
* mesS tableau dans lequel seront stockés tous les String
|
||||||
* @var mesD tableau dans lequel seront stockés tous les Double
|
* mesD tableau dans lequel seront stockés tous les Double
|
||||||
* @var sc Scanner, l objet qui servira a lire le fichier csv
|
* sc Scanner, l objet qui servira a lire le fichier csv
|
||||||
* @var line String, ligne contenant l'ajout a effectuer
|
* line String, ligne contenant l'ajout a effectuer
|
||||||
* @var i int, un compteur de valeurs lues,
|
* i int, un compteur de valeurs lues,
|
||||||
* @var j int, qui suit i%NBCOL
|
* j int, qui suit i%NBCOL
|
||||||
* @var a int, un compteur de lignes modifiees
|
* a int, un compteur de lignes modifiees
|
||||||
* @var b int, un compteur de champs modifiees
|
* b int, un compteur de champs modifiees
|
||||||
* @var nbValAj int qui augmente si on doit ajouter des lignes
|
* nbValAj int qui augmente si on doit ajouter des lignes
|
||||||
* @var date String, la valeur de la date lue
|
* date String, la valeur de la date lue
|
||||||
* @var jour String, le jour lu
|
* jour String, le jour lu
|
||||||
* @var mois String, le mois lu
|
* mois String, le mois lu
|
||||||
* @var an String, l annee lue
|
* an String, l annee lue
|
||||||
* @var anEntre long, valeur lue pour l annee et le numero de tirage. sert a definir si la valeur est a ajouter dans la table
|
* anEntre long, valeur lue pour l annee et le numero de tirage. sert a definir si la valeur est a ajouter dans la table
|
||||||
* @var ps PreparedStatement pour executer la requete sans injection sql
|
* ps PreparedStatement pour executer la requete sans injection sql
|
||||||
* @var stmt Statement pour executer la requete de depart
|
* stmt Statement pour executer la requete de depart
|
||||||
*/
|
*/
|
||||||
public String updateDataBasev2() throws FileNotFoundException {
|
public int updateTablev2(String filePath) throws FileNotFoundException {
|
||||||
//Ligne maL=new Ligne();
|
//Ligne maL=new Ligne();
|
||||||
|
ResultSet rs=null;
|
||||||
|
int res=-1;
|
||||||
Statement stmt;
|
Statement stmt;
|
||||||
long[][] mesL=new long[4000][NBCOL+1];
|
long[][] mesL=new long[4000][NBCOL+1];
|
||||||
String[][]mesS=new String[4000][NBCOL+1];
|
String[][]mesS=new String[4000][NBCOL+1];
|
||||||
Double[][]mesD=new Double[4000][NBCOL+1];
|
Double[][]mesD=new Double[4000][NBCOL+1];
|
||||||
Scanner sc;
|
Scanner sc;
|
||||||
ResultSet rs;
|
|
||||||
long anEntre;
|
long anEntre;
|
||||||
int i,j, nbValAj=0;
|
int i,j, nbValAj=0;
|
||||||
String date, jour, mois, an;
|
String date, jour, mois, an;
|
||||||
|
|
@ -525,7 +497,7 @@ public class DataBase {
|
||||||
}
|
}
|
||||||
System.out.println(sql);
|
System.out.println(sql);
|
||||||
if (nbValAj>0) {//si on a des modifications
|
if (nbValAj>0) {//si on a des modifications
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps;
|
||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
if (conn != null) {
|
if (conn != null) {
|
||||||
|
|
@ -535,7 +507,6 @@ public class DataBase {
|
||||||
if((b>1&&b<5)||b==11||b==25){
|
if((b>1&&b<5)||b==11||b==25){
|
||||||
System.out.println("a: "+a+" et b: "+b);
|
System.out.println("a: "+a+" et b: "+b);
|
||||||
ps.setString((a-1)*25+b, mesS[a][b]);
|
ps.setString((a-1)*25+b, mesS[a][b]);
|
||||||
|
|
||||||
}
|
}
|
||||||
else if(b > 12 && b < 24 && b % 2 == 1){
|
else if(b > 12 && b < 24 && b % 2 == 1){
|
||||||
System.out.println("a: "+a+" et b: "+b);
|
System.out.println("a: "+a+" et b: "+b);
|
||||||
|
|
@ -547,78 +518,119 @@ public class DataBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ps.executeQuery();//puis on execute la requete
|
res=ps.executeUpdate();//puis on execute la requete
|
||||||
|
//res=ps.executeQuery();//puis on execute la requete
|
||||||
System.out.println("update validee");
|
System.out.println("update validee");
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.out.println("oskour into update");
|
System.out.println("updateDataBasev2 probleme");
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.out.println("oskour update");
|
System.out.println("updateDataBasev2 probleme, verifier si le pb ne vient pas de executeupdate au lie de executequery");
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
return sql;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeLines(int i) throws FileNotFoundException{
|
/**
|
||||||
|
* la methode de suppression des lignes strictement supérieures a la valeur i dans annee_numero_de_tirage
|
||||||
|
* @param i la valeur de reference
|
||||||
|
* @return res, int le resultat de la requete (nb de lignes affectees)
|
||||||
|
*/
|
||||||
|
public int removeMultiplesLines(int i){
|
||||||
|
int res=-1;
|
||||||
String sql="delete from myny.Test_Table where annee_numero_de_tirage >"+i;
|
String sql="delete from myny.Test_Table where annee_numero_de_tirage >"+i;
|
||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
if(conn!=null) {
|
if(conn!=null) {
|
||||||
Statement ps = conn.createStatement();
|
Statement ps = conn.createStatement();
|
||||||
ps.executeQuery(sql);
|
res=ps.executeUpdate(sql);
|
||||||
|
//ps.executeQuery(sql);
|
||||||
conn.commit();
|
conn.commit();
|
||||||
//conn.close();
|
//conn.close();
|
||||||
System.out.println("statement cree");
|
System.out.println("statement cree");
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.out.println("oskour");
|
System.out.println("removeLines probleme");
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dropTable() throws FileNotFoundException{
|
/**
|
||||||
String sql="drop table myny.Test_Table";
|
* la methode de selection de la ligne ayant la valeur i dans annee_numero_de_tirage
|
||||||
|
* @param i la valeur de reference
|
||||||
|
* @return rs, ResultSet le resultat de la requete
|
||||||
|
*/
|
||||||
|
public ResultSet showLine(int i){
|
||||||
|
ResultSet rs=null;
|
||||||
|
String sql="select * from myny.Test_Table where annee_numero_de_tirage ="+i;
|
||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
if(conn!=null) {
|
if(conn!=null) {
|
||||||
Statement ps = conn.createStatement();
|
Statement ps = conn.createStatement();
|
||||||
ps.executeQuery(sql);
|
rs=ps.executeQuery(sql);
|
||||||
conn.commit();
|
conn.commit();
|
||||||
//conn.close();
|
//conn.close();
|
||||||
System.out.println("statement cree");
|
System.out.println("statement cree");
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.out.println("oskour");
|
System.out.println("removeLine probleme");
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
|
return rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* La methode de connection
|
* la methode de suppression de la ligne ayant la valeur i dans annee_numero_de_tirage
|
||||||
|
* @param i la valeur de reference
|
||||||
|
* @return res, int le resultat de la requete(nb de lignes affectees)
|
||||||
*/
|
*/
|
||||||
public Connection getConnection() {
|
public int removeLine(int i){
|
||||||
conn = null;
|
int res=-1;
|
||||||
|
String sql="delete from myny.Test_Table where annee_numero_de_tirage ="+i;
|
||||||
try {
|
try {
|
||||||
conn = DriverManager.getConnection(url);
|
conn = this.getConnection();
|
||||||
//conn = DriverManager.getConnection("jdbc:mariadb://vachot.fr:3306?user=mynynicolas&password=Bw0po64*");
|
|
||||||
//conn = DriverManager.getConnection("jdbc:mariadb://phpmyadmin.vachot.fr:3306/myny?user=mynynicolas&password=Bw0po64*");
|
|
||||||
//conn = DriverManager.getConnection("jdbc:mariadb:../../../../../../test.db");
|
|
||||||
if(conn!=null) {
|
if(conn!=null) {
|
||||||
System.out.println("Connected to database");
|
Statement ps = conn.createStatement();
|
||||||
System.out.println(conn);
|
res=ps.executeUpdate(sql);
|
||||||
|
//res=ps.executeQuery(sql);
|
||||||
|
conn.commit();
|
||||||
|
//conn.close();
|
||||||
|
System.out.println("statement cree");
|
||||||
}
|
}
|
||||||
}catch(SQLException e) {
|
} catch (SQLException e) {
|
||||||
System.out.println("on a des pb:" + e.getMessage());
|
System.out.println("removeLine probleme, voir si le pb ne vient pas d'execute query et executeupdate");
|
||||||
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
return conn;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* La methode pour supprimer la table entiere
|
||||||
|
* @return res int le resultat de la requete(nb de lignes affectees)
|
||||||
|
*/
|
||||||
|
public int dropTable(){
|
||||||
|
String sql="drop table myny.Test_Table";
|
||||||
|
int res=-1;
|
||||||
|
try {
|
||||||
|
conn = this.getConnection();
|
||||||
|
if(conn!=null) {
|
||||||
|
Statement ps = conn.createStatement();
|
||||||
|
//rs=ps.executeQuery(sql);
|
||||||
|
res=ps.executeUpdate(sql);
|
||||||
|
conn.commit();
|
||||||
|
//conn.close();
|
||||||
|
System.out.println("statement cree");
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println("dropTable probleme, voir si le pb ne vient pas d'execute query et executeupdate");
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,20 +3,20 @@ package fr.myny.database;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import fr.myny.database.*;
|
|
||||||
|
|
||||||
class DataBaseTest {
|
class DataBaseTest {
|
||||||
Connection connexion;
|
Connection maCo;
|
||||||
|
public static final String FILEPATH="c:/Users/cocof/Bureau/nouveau_loto.csv";
|
||||||
@Test
|
@Test
|
||||||
void fillDataBase() throws SQLException {
|
void fillDataBase() throws SQLException {
|
||||||
|
|
||||||
DataBase maDB=new DataBase();
|
DataBase maDB=new DataBase();
|
||||||
Connection maCo=maDB.getConnection();
|
maCo=maDB.getConnection();
|
||||||
assertNotNull(maCo);
|
assertNotNull(maCo);
|
||||||
maDB.fillDataBase();
|
int res= maDB.fillTable(FILEPATH);
|
||||||
|
System.out.println(res);
|
||||||
Statement stmt= maDB.conn.createStatement();
|
Statement stmt= maDB.conn.createStatement();
|
||||||
ResultSet rs=stmt.executeQuery("SELECT Count(*) From myny.Test_Table");
|
ResultSet rs=stmt.executeQuery("SELECT Count(*) From myny.Test_Table");
|
||||||
rs.next();
|
rs.next();
|
||||||
|
|
@ -31,9 +31,9 @@ class DataBaseTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createDataBase() throws SQLException {
|
void createTable() throws SQLException {
|
||||||
DataBase maDB=new DataBase();
|
DataBase maDB=new DataBase();
|
||||||
maDB.createDataBase();
|
maDB.createTable();
|
||||||
Statement stmt= maDB.conn.createStatement();
|
Statement stmt= maDB.conn.createStatement();
|
||||||
ResultSet rs=stmt.executeQuery("Select * From myny.Test_Table");
|
ResultSet rs=stmt.executeQuery("Select * From myny.Test_Table");
|
||||||
ResultSetMetaData rsmd =rs.getMetaData();
|
ResultSetMetaData rsmd =rs.getMetaData();
|
||||||
|
|
@ -43,21 +43,21 @@ class DataBaseTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void updateDataBase() throws FileNotFoundException {
|
void updateTable() throws FileNotFoundException, SQLException {
|
||||||
DataBase maDB=new DataBase();
|
DataBase maDB=new DataBase();
|
||||||
Connection maCo=maDB.getConnection();
|
maCo=maDB.getConnection();
|
||||||
String requete = maDB.updateDataBase();
|
int res = maDB.updateTable(FILEPATH);
|
||||||
System.out.println(requete);
|
System.out.println(res);
|
||||||
assertNotNull(maCo);
|
assertNotNull(maCo);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void updateDataBasev2() throws FileNotFoundException {
|
void updateTablev2() throws FileNotFoundException, SQLException {
|
||||||
DataBase maDB=new DataBase();
|
DataBase maDB=new DataBase();
|
||||||
Connection maCo=maDB.getConnection();
|
maCo=maDB.getConnection();
|
||||||
String requete = maDB.updateDataBasev2();
|
int res = maDB.updateTablev2(FILEPATH);
|
||||||
System.out.println(requete);
|
System.out.println(res);
|
||||||
assertNotNull(maCo);
|
assertNotNull(maCo);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -65,14 +65,31 @@ class DataBaseTest {
|
||||||
@Test
|
@Test
|
||||||
void getConnection() {
|
void getConnection() {
|
||||||
DataBase maDB=new DataBase();
|
DataBase maDB=new DataBase();
|
||||||
Connection maCo=maDB.getConnection();
|
maCo=maDB.getConnection();
|
||||||
assertNotNull(maCo);
|
assertNotNull(maCo);
|
||||||
}
|
}
|
||||||
|
@Test
|
||||||
|
public void showLine() throws SQLException {
|
||||||
|
DataBase maDB=new DataBase();
|
||||||
|
ResultSet rs=maDB.showLine(2017001);
|
||||||
|
rs.next();
|
||||||
|
for (int i=1;i<26;i++) {
|
||||||
|
System.out.print(rs.getObject(i)+", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void removeLines() throws FileNotFoundException{
|
public void removeMultiplesLines() throws FileNotFoundException{
|
||||||
DataBase maDB=new DataBase();
|
DataBase maDB=new DataBase();
|
||||||
maDB.removeLines(0);
|
int res= maDB.removeMultiplesLines(2017004);
|
||||||
|
System.out.print(res);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void removeLine() throws FileNotFoundException{
|
||||||
|
DataBase maDB=new DataBase();
|
||||||
|
int res= maDB.removeLine(2017014);
|
||||||
|
System.out.print(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Reference in a new issue