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
|
public static ResultSet resultat;
public static Connection connexion;
static {
FunctionMySQL.resultat = null;
FunctionMySQL.connexion = null;
String driver = "com.mysql.jdbc.Driver";
try {
Class.forName(driver).newInstance();
connexion = DriverManager.getConnection("jdbc:mysql://localhost/maBase","root","");
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.err.println("pb lors du chargement "+e);
e.printStackTrace();
} catch (SQLException e) {
throw new RuntimeException("Impossible de charger cette classe");
}
}
public void preparedStatementWithList(){
ResultSet select;
try {
select = FunctionPgSQL.select("SELECT * FROM log;");
Date d= new Date();
long debut = d.getTime();
List<List> list = new ArrayList<List>();
while(select.next()){
List<String> ligne = new ArrayList<String>();
ligne.add(select.getString(2));
ligne.add(select.getString(3));
list.add(ligne);
}
FunctionMySQL.insertWithPrepareStatement("INSERT INTO test(nom,prenom) VALUES( ?,?)", list);
Date d2= new Date();
long fin = d2.getTime();
System.out.println("preparedStatementWithList durée :" + (fin - debut));
} catch (SQLException e3) {
System.err.println("Probleme SQL " + e3);
}
}
public static void insertWithPrepareStatement(String request,List<List> list) {
try {
PreparedStatement preparedStatement = connexion.prepareStatement(request);
for (ListIterator it = list.listIterator(); it.hasNext();){
List<String> ligne = (List<String>) it.next();
for (ListIterator ligneListe = ligne.listIterator(); ligneListe.hasNext();){
preparedStatement.setString(1,(String) ligneListe.next());
preparedStatement.setString(2,(String) ligneListe.next());
preparedStatement.executeUpdate();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
} |