comment extraire un champ de la base de donnée?
si on veut par exemple extraire une somme des enregistrements d 'un champ ou le nombre des enregistrements voila la fonction
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
public String get_Word(String value){
ResultSet rs;
try {
rs=statm.executeQuery(value );
System.out.println(value);
rs.next();
return rs.getString("M");
} catch (Exception e) { e.printStackTrace();
System.out.println("Clsgestion.getWord() :" +
" impossible de touver: " + value );
return null;}
}
....
// il faut indiquer le nom du champ par l'aliace 'as M'
String s=get_Word("select sum(n_etudiant) AS M from etudiant"); |
Export & Import base mysql
:salut: je vous propose deux codes pour exporter et importer votre base mysql en java :
Exportation:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
try{
Runtime.getRuntime().exec(
// 1 - La commande a exécuter (le shell), en séparant les paramètres :
new String[] {
"cmd.exe", // ou command.com sous Windows 9x
"/C",
"mysqldump.exe --user=farid --password=farid mabase > C:\\base.sql" },
// 2 - Les variables d'environnements (null = hérité du parent)
null,
// 3 - Le répertoire de travail
new File("C:\\Program Files\\EasyPHP1-8\\mysql\\bin\\")
);
}catch(IOException e){ System.out.println("erreur"); } |
et pour importer une base mysql depuis un fichier sql voici le code
Importation:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
try{
Runtime.getRuntime().exec(
new String[] {
"cmd.exe",
"/C",
// il faut remplacer mysqldump par mysql dans le cas d' importation
"mysql.exe --user=farid --password=farid mabase < C:\\base.sql" },
null,
new File("C:\\Program Files\\EasyPHP1-8\\mysql\\bin\\")
);
}catch(IOException e){ System.out.println("erreur"); } |
Remarque:
avant d'importer le fichier sql, il faut d'abord créer une base de donnée vide;)
Comment savoir si un champ existe dans la base ?
pour tester si un champ existe dans la base, j ai développé cette fonction qui retourne un boolean .
Code:
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
|
Statement statm;
...
public boolean is_existe(String Sql)
{
ResultSet rs;
try {
rs=statm.executeQuery(Sql);
rs.next();
if(rs.getRow()==0)
return false;
else
return true;
} catch (Exception e)
{
e.printStackTrace();
System.out.println("Cls_base.is_existe() :" +
" impossible de touver: " +Sql);
return false;
}
}
// notre requête sql peut etre sous la forme :
// sql= "select "+champ+" from "+table+" where "+filtre+"='"+data+"'"; |
Sélectionner les lignes d'une table en utilisant PreparedStatement
Bonjour à tous,
il est conseillé d'utiliser PreparedStatement dans les interrogations et les insertions dans des tables des bases de données. Certes, l'interrogation a souvent compliquer. Ci dessous, vous trouverez une petite méthode qui l'utilise sans trop de peines. On exécute la requête à l'aide d'une variable PreparedStatement, et on parcourt le résultat avec un ResultSet.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
public static void selectionner(Connection cn, String table){
PreparedStatement ps;
ResultSet rs;
//Préparer une requête
ps = cn.prepareStatement("select * from "+table);
//Exécuter cette dernière
ps.executeQuery();
//on récupère le ResultSet du ps et on le passe au rs
rs = ps.getResultSet();
//on parcourt rs
while (rs.next()) {
/*Traitement des lignes récupérées
...........
*/
}
//Fermetures
rs.close();
ps.close();
} |
Bonne pratique 8-)