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
| package index;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class index_import {
public static void main(String[] args) throws IOException{
hierarchie_2("lMCL.txt");
}
public static void alpha(String nom) throws IOException{
BufferedReader lecteurAvecBuffer = null;
String ligne;
String[] lg;
try{
lecteurAvecBuffer = new BufferedReader(new FileReader(nom));
}catch(FileNotFoundException exc){
System.out.println("Erreur d'ouverture");
}
while ((ligne = lecteurAvecBuffer.readLine()) != null){
lg = ligne.split("\t");
System.out.println(lg[0]);
}
lecteurAvecBuffer.close();
}
public static void hierarchie_2(String nom) throws IOException{
BufferedReader lecteurAvecBuffer = null;
String ligne;
String[] lg;
String niv_sup;
Connection connexion = connect_bdd();
try{
lecteurAvecBuffer = new BufferedReader(new FileReader(nom));
}catch(FileNotFoundException exc){
System.out.println("Erreur d'ouverture");
}
while ((ligne = lecteurAvecBuffer.readLine()) != null){
lg = ligne.split("\t");
if(lg.length<2){
System.out.println(lg[0]);
niv_sup = lg[0];
insert_bdd(connexion,nom,lg[0],"","");
}else{
System.out.println("--- "+lg[1]);
}
}
lecteurAvecBuffer.close();
}
public static Connection connect_bdd(){
Connection connection = null;
try {
try {
//Charge le driver JDBC
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Driver chargé!");
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//Paramètres de connexion à la base de données
String url = "jdbc:mysql://localhost/webcath";
String login = "root";
String password = "";
System.out.println("Paramètres de connexion chargés!");
try{
//Connexion à la base de données
connection = DriverManager.getConnection(url,login,password);
System.out.println("Connexion à la bdd ok!");
PreparedStatement pstm;
pstm = connection.prepareStatement("TRUNCATE TABLE index");
pstm.executeUpdate();
}
catch(SQLException sqle){
System.out.println("Exception SQL : ");
while (sqle != null) {
String message = sqle.getMessage();
String sqlState = sqle.getSQLState();
int errorCode = sqle.getErrorCode();
System.out.println("Message = "+message);
System.out.println("SQLState = "+sqlState);
System.out.println("ErrorCode = "+errorCode);
sqle.printStackTrace();
sqle = sqle.getNextException();
}
}
return connection;
}
/**
* Import dans la base de données
*/
public static void insert_bdd(Connection connection,String nom,String h1,String h2,String h3){
try {
//Préparation de la requète
PreparedStatement pstm = connection.prepareStatement("INSERT INTO index (nom,h1,h2,h3) VALUES(?,?,?,?)");
//Insertion dans la base de données
//Complète le PreparedStatement avec les données de l'objet en cours
pstm.setString(1,nom);
pstm.setString(2,h1);
pstm.setString(3,h2);
pstm.setString(4,h3);
//Exécute la requète
pstm.executeUpdate();
}
catch(SQLException sqle){
System.out.println("Exception SQL : ");
while (sqle != null) {
String message = sqle.getMessage();
String sqlState = sqle.getSQLState();
int errorCode = sqle.getErrorCode();
System.out.println("Message = "+message);
System.out.println("SQLState = "+sqlState);
System.out.println("ErrorCode = "+errorCode);
sqle.printStackTrace();
sqle = sqle.getNextException();
}
}
}
} |
Partager