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
| import java.sql.*;
public class MaBdd
{
String user="root";
String mdp="mdp";
String url ="jdbc:mysql://localhost:3306/mabasa";
Connection conn;
ResultSet result;
Statement state;
ResultSetMetaData resultMeta;
public MaBdd()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn =DriverManager.getConnection(url,user,mdp);
} catch (Exception e)
{
e.printStackTrace();
}
}
public void Affichage()
{
try {
state=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
result=state.executeQuery("select id_etudiant,sexe_etudiant,nom_etudiant,prenom_etudiant from etudiant limit 5");
resultMeta =result.getMetaData();
System.out.println(" "+resultMeta.getColumnCount()+"colonnes");
System.out.println("\n****************************************************************");
for(int i=1;i<=resultMeta.getColumnCount();i++){
System.out.print(resultMeta.getColumnName(i)+" ");
}
System.out.println("\n ***************************************************************");
while (result.next()){
for(int i=1;i<=resultMeta.getColumnCount();i++){
System.out.print(result.getObject(i).toString()+"\t\t");
}
System.out.println("\n------------------------------------------------------------");
}
} catch (Exception e){
e.printStackTrace();
}}
public void Insertion()
{
try{
state.executeUpdate("insert into etudiant(id_etudiant,sexe_etudiant,nom_etudiant,prenom_etudiant,ville_etudiant,pays_etudiant,datenais_etu) values ('291234199','m','lenom','leprenom','ville','lepays','2013-05-05')");
}catch(Exception e){
e.printStackTrace();
}
}
public void Modification(){
try{
result=state.executeQuery("select * from etudiant where id_etudiant='29123457'");
result.updateString("nom_etudiant","lenom2");
result.updateRow();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
MaBdd laconection =new MaBdd();
laconection.Affichage();
laconection.Insertion();
laconection.Modification();
}
} |
Partager