Remplissage de la DB avec l'excel
This commit is contained in:
parent
bcb2adb7fc
commit
177283a7d9
2 changed files with 196 additions and 39 deletions
|
|
@ -3,20 +3,16 @@ package fr.myny.database;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.concurrent.*;
|
|
||||||
import com.mysql.jdbc.Driver;
|
|
||||||
import org.mariadb.jdbc.internal.com.read.dao.Results;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
|
import com.opencsv.CSVReader;
|
||||||
/**
|
/**
|
||||||
* La classe DataBase qui soccupera de la base de donnees
|
* La classe DataBase qui soccupera de la base de donnees
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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 String url="jdbc:mariadb://phpmyadmin.vachot.fr:3306?db=myny&user=mynynicolas&password=Bw0po64*";
|
//public static String url="jdbc:mariadb://phpmyadmin.vachot.fr:3306?db=myny&user=mynynicolas&password=Bw0po64*";
|
||||||
Connection conn;
|
Connection conn;
|
||||||
/**
|
/**
|
||||||
* Le constructeur de DataBase
|
* Le constructeur de DataBase
|
||||||
|
|
@ -28,11 +24,161 @@ public class DataBase {
|
||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* La methode de remplissage de la base de donnees
|
* La methode de remplissage de la base de donnees
|
||||||
*/
|
*/
|
||||||
public void fillDataBase(){
|
public void fillDataBase(){
|
||||||
|
/*String sql="INSERT INTO 'myny.Test_Table'" +
|
||||||
|
" ('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";*/
|
||||||
|
String sql="INSERT INTO myny.Test_Table" +
|
||||||
|
" (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 {
|
||||||
|
Scanner sc = new Scanner(new File("c:/Users/cocof/Bureau/nouveau_loto.csv"));
|
||||||
|
sc.useDelimiter(";|\\n"); //sets the delimiter pattern
|
||||||
|
int i=0,j;
|
||||||
|
String line=sc.nextLine();
|
||||||
|
line="";
|
||||||
|
String date, jour, mois, an;
|
||||||
|
while (sc.hasNext()) //returns a boolean value
|
||||||
|
{
|
||||||
|
j=(i%25)+1;
|
||||||
|
if(j==1) {
|
||||||
|
line=line+'(';
|
||||||
|
line=line+sc.next().replace(",",".");
|
||||||
|
line=line+';';
|
||||||
|
}
|
||||||
|
if(j<25 && j>1) {
|
||||||
|
if (j==2 ||j==11){
|
||||||
|
line=line+"\'"+sc.next().replace(",",".")+"\',";
|
||||||
|
}
|
||||||
|
else if (j==3 ||j==4){
|
||||||
|
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{
|
||||||
|
line=line+sc.next().replace(",",".");
|
||||||
|
line=line+';';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(j==25) {
|
||||||
|
line=line+"\'"+sc.next().replace(",",".")+"\'";
|
||||||
|
line=line+')';
|
||||||
|
line=line.replaceAll("\\s","");
|
||||||
|
line = line.replaceAll(";", ",");
|
||||||
|
if(sc.hasNext()) {
|
||||||
|
line = line+ ",";
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
line = line+ ";";
|
||||||
|
}
|
||||||
|
//System.out.println(line);
|
||||||
|
sql=sql+line;
|
||||||
|
line="";
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
//System.out.println(sc.nextLine());
|
||||||
|
//System.out.print(sc.next()); //find and returns the next complete token from this scanner
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(sql);
|
||||||
|
//System.out.println(sql);
|
||||||
|
/*CSVReader reader = new CSVReader(new FileReader("c:/Users/cocof/Bureau/nouveau_loto.csv"));
|
||||||
|
String[] nextLine=reader.readNext();
|
||||||
|
while ((nextLine = reader.readNext()) != null) {
|
||||||
|
// nextLine[] is an array of values from the line
|
||||||
|
for (int j = 0; j < nextLine.length - 1; j++) {
|
||||||
|
if (j==0){
|
||||||
|
sql=sql+"(";
|
||||||
|
}
|
||||||
|
sql=sql+nextLine[j];
|
||||||
|
if (j==nextLine.length-1){
|
||||||
|
sql=sql+"),";
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
sql=sql+",";
|
||||||
|
}
|
||||||
|
//System.out.println(nextLine[j]);
|
||||||
|
}
|
||||||
|
sql=sql+";";
|
||||||
|
}
|
||||||
|
System.out.println(sql);
|
||||||
|
|
||||||
|
conn = DriverManager.getConnection(url);
|
||||||
|
if(conn!=null) {
|
||||||
|
Statement stmt = conn.createStatement();
|
||||||
|
stmt.executeQuery(sql);
|
||||||
|
conn.commit();
|
||||||
|
System.out.println("statement cree");
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
catch (Exception e){
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
conn = DriverManager.getConnection(url);
|
||||||
|
if(conn!=null) {
|
||||||
|
Statement stmt = conn.createStatement();
|
||||||
|
stmt.executeQuery(sql);
|
||||||
|
conn.commit();
|
||||||
|
//conn.close();
|
||||||
|
System.out.println("statement cree");
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println("oskour");
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -47,36 +193,39 @@ public class DataBase {
|
||||||
*/
|
*/
|
||||||
public void createDataBase() {
|
public void createDataBase() {
|
||||||
String sql="CREATE TABLE IF NOT EXISTS myny.Test_Table ("+
|
String sql="CREATE TABLE IF NOT EXISTS myny.Test_Table ("+
|
||||||
" annee_numero_de_tirage INTEGER,"+
|
" annee_numero_de_tirage INTEGER,"+ //1
|
||||||
" jour_de_tirage VARCHAR(10),"+
|
" jour_de_tirage VARCHAR(10),"+ //2 VARCHAR
|
||||||
" date_de_tirage DATE,"+
|
" date_de_tirage DATE,"+ //3 DATE
|
||||||
" date_de_forclusion DATE,"+
|
" date_de_forclusion DATE,"+ //4 DATE
|
||||||
" boule_1 TINYINT,"+
|
" boule_1 TINYINT,"+ //5
|
||||||
" boule_2 TINYINT,"+
|
" boule_2 TINYINT,"+ //6
|
||||||
" boule_3 TINYINT,"+
|
" boule_3 TINYINT,"+ //7
|
||||||
" boule_4 TINYINT,"+
|
" boule_4 TINYINT,"+ //8
|
||||||
" boule_5 TINYINT,"+
|
" boule_5 TINYINT,"+ //9
|
||||||
" numero_chance TINYINT,"+
|
" numero_chance TINYINT,"+ //10
|
||||||
" combinaison_gagnante_en_ordre_croissant varchar(20),"+
|
" combinaison_gagnante_en_ordre_croissant varchar(20),"+//11 VARCHAR
|
||||||
" nombre_de_gagnant_au_rang1 INTEGER,"+
|
" nombre_de_gagnant_au_rang1 INTEGER,"+ //12
|
||||||
" rapport_du_rang1 INTEGER,"+
|
" rapport_du_rang1 INTEGER,"+ //13
|
||||||
" nombre_de_gagnant_au_rang2 INTEGER,"+
|
" nombre_de_gagnant_au_rang2 INTEGER,"+ //14
|
||||||
" rapport_du_rang2 INTEGER,"+
|
" rapport_du_rang2 INTEGER,"+ //15
|
||||||
" nombre_de_gagnant_au_rang3 INTEGER,"+
|
" nombre_de_gagnant_au_rang3 INTEGER,"+ //16
|
||||||
" rapport_du_rang3 INTEGER,"+
|
" rapport_du_rang3 INTEGER,"+ //17
|
||||||
" nombre_de_gagnant_au_rang4 INTEGER,"+
|
" nombre_de_gagnant_au_rang4 INTEGER,"+ //18
|
||||||
" rapport_du_rang4 INTEGER,"+
|
" rapport_du_rang4 INTEGER,"+ //19
|
||||||
" nombre_de_gagnant_au_rang5 INTEGER,"+
|
" nombre_de_gagnant_au_rang5 INTEGER,"+ //20
|
||||||
" rapport_du_rang5 INTEGER,"+
|
" rapport_du_rang5 INTEGER,"+ //21
|
||||||
" nombre_de_gagnant_au_rang6 INTEGER,"+
|
" nombre_de_gagnant_au_rang6 INTEGER,"+ //22
|
||||||
" rapport_du_rang6 INTEGER,"+
|
" rapport_du_rang6 INTEGER,"+ //23
|
||||||
" numero_jokerplus INTEGER,"+
|
" numero_jokerplus INTEGER,"+ //24
|
||||||
" devise VARCHAR(10));";
|
" devise VARCHAR(10));"; //25 VARCHAR
|
||||||
//System.out.println(sql);
|
//System.out.println(sql);
|
||||||
try {
|
try {
|
||||||
conn = DriverManager.getConnection(url);
|
conn = DriverManager.getConnection(url);
|
||||||
if(conn!=null) {
|
if(conn!=null) {
|
||||||
Statement stmt = conn.createStatement();
|
Statement stmt = conn.createStatement();
|
||||||
|
stmt.executeQuery(sql);
|
||||||
|
conn.commit();
|
||||||
|
//conn.close();
|
||||||
System.out.println("statement cree");
|
System.out.println("statement cree");
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
|
|
@ -175,7 +324,10 @@ public class DataBase {
|
||||||
//conn = DriverManager.getConnection("jdbc:mariadb://vachot.fr:3306?user=mynynicolas&password=Bw0po64*");
|
//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://phpmyadmin.vachot.fr:3306/myny?user=mynynicolas&password=Bw0po64*");
|
||||||
//conn = DriverManager.getConnection("jdbc:mariadb:../../../../../../test.db");
|
//conn = DriverManager.getConnection("jdbc:mariadb:../../../../../../test.db");
|
||||||
System.out.println("Connected to database");
|
if(conn!=null) {
|
||||||
|
System.out.println("Connected to database");
|
||||||
|
System.out.println(conn);
|
||||||
|
}
|
||||||
}catch(SQLException e) {
|
}catch(SQLException e) {
|
||||||
System.out.println("on a des pb:" + e.getMessage());
|
System.out.println("on a des pb:" + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,17 +5,22 @@ import org.junit.jupiter.api.Test;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.concurrent.*;
|
|
||||||
import com.mysql.jdbc.Driver;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
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 connexion;
|
||||||
@Test
|
@Test
|
||||||
void fillDataBase() {
|
void fillDataBase() throws SQLException {
|
||||||
|
|
||||||
|
DataBase maDB=new DataBase();
|
||||||
|
Connection maCo=maDB.getConnection();
|
||||||
|
assertNotNull(maCo);
|
||||||
|
//maDB.fillDataBase();
|
||||||
|
Statement stmt= maDB.conn.createStatement();
|
||||||
|
ResultSet rs=stmt.executeQuery("SELECT Count(*) As total From myny.Test_Table Where 1=1");
|
||||||
|
System.out.println("nb lignes (changer la ligne juste en dessous si on modifie le nb de col): "+rs.getInt("total"));
|
||||||
|
assertEquals(1317,rs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Reference in a new issue