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
| package sql;
import java.sql.*;
class testsql
{
public static void main(String[] args)
{
//Nom de mon pilote
String pilote = "com.mysql.jdbc.Driver";//"sun.jdbc.odbc.JdbcOdbcDriver";
try
{
//Chargement de mon pilote
Class.forName(pilote);
//Connexion à ma base mysql avec mon login et mot de passe(ici mot de passe vide)
Connection connexion = DriverManager.getConnection("jdbc:mysql://localhost/tableau","root","");
//Création de mon statement qui va me permettre d'executer mes requetes
Statement instruction = connexion.createStatement();
//Ma table s'appelle dynes et tout ce qui reste dépend d'elle
ResultSet resultat = instruction.executeQuery("SELECT * FROM dynes");
//voici maintenant mes colonnes
while(resultat.next())
{
System.out.println("---------------------------");
//System.out.println("n°: "+resultat.getInt("n°"));
System.out.println("fields: "+resultat.getString("fields"));
System.out.println("IP: "+resultat.getString("IP adress"));
System.out.println("MAC adress: "+resultat.getString("MAC adress"));
System.out.println("OS running: "+resultat.getString("OS running"));
System.out.println("product type: "+resultat.getString("product type"));
System.out.println("service pack: "+resultat.getString("service pack"));
System.out.println("processors: "+resultat.getString("processors"));
System.out.println("processor type: "+resultat.getString("processor type"));
System.out.println("processor speed: "+resultat.getString("processor speed"));
System.out.println("physical memory: "+resultat.getString("physical memory"));
}
// On exécute une seconde transaction qui consiste à ajouter un élément
// dans la base grâce à INSERT.
instruction.executeUpdate ("insert into dynes( fields, IP_adress, MAC_adress, OS_running, product_type, service_pack, processors, processor_type, processor_speed, physical_memory) values('win','199.236.25.236','00.32.23.23','2','salut''salut','salut','rerzerz','rezazera','lala')");
// Pour vérifier que l'insertion a marché, on recommence la première
// transaction de sélection.
resultat = instruction.executeQuery("SELECT * FROM dynes");
System.out.println("\n"+"Après l'insertion !");
while(resultat.next())
{
System.out.println("---------------------------");
System.out.println("fields: "+resultat.getString("fields"));
System.out.println("IP: "+resultat.getString("IP adress"));
System.out.println("MAC adress: "+resultat.getString("MAC adress"));
System.out.println("OS running: "+resultat.getString("OS running"));
System.out.println("product type: "+resultat.getString("product type"));
System.out.println("service pack: "+resultat.getString("service pack"));
System.out.println("processors: "+resultat.getString("processors"));
System.out.println("processor type: "+resultat.getString("processor type"));
System.out.println("processor speed: "+resultat.getString("processor speed"));
System.out.println("physical memory: "+resultat.getString("physical memory"));
}
connexion.close();
}
catch (Exception e)
{
System.out.println("echec pilote : "+e);
}
}
} |
Partager