connexion base de donnÉes mini-projets java-bd 2011-2012 wael mallek imed amri
TRANSCRIPT
![Page 1: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/1.jpg)
CONNEXION BASE DE DONNÉES
Mini-projets Java-BD 2011-2012Wael MallekImed Amri
![Page 2: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/2.jpg)
JDBC API ?
API d’interaction avec un SGBD contenant : un ensemble de classes et d’interfaces ne fournit pas les classes qui implantent les
interfaces Permet de:
Établir une connexion avec un SGBD Envoyer des requêtes SQL Récupérer des résultats de requêtes
![Page 3: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/3.jpg)
Drivers ?
Drivers chaque SGBD utilise un pilote (driver) qui
lui est propre et qui permet de convertir les requêtes JDBC dans le langage natif du SGBD
le driver est un ensemble de classes qui implantent les interfaces de JDBC
les drivers sont le lien entre le programme Java et le SGBD
![Page 4: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/4.jpg)
Base MySQL
Créer une base mysql ‘esprit’
![Page 5: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/5.jpg)
Projet Java
Créer un nouveau Projet : Java Application
Ajouter la Librairie MySQL JDBC Driver
![Page 6: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/6.jpg)
Méthode Main
Les Attribues de la classes : static String url =
"jdbc:mysql://localhost/esprit"; static String user = "root"; static String pwd = ""; static Connection conn; static Statement ste;
![Page 7: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/7.jpg)
Méthode Main
Charger le Driver Class.forName("com.mysql.jdbc.Driver");
Etablir une connexion conn = DriverManager.getConnection(url,
user, pwd); Traiter les exceptions
ClassNotFoundException SQLException
![Page 8: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/8.jpg)
Méthode Main
Les Attribues de la classes : static String url =
"jdbc:mysql://localhost/esprit"; static String user = "root"; static String pwd = ""; static Connection conn; static Statement ste;
![Page 9: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/9.jpg)
Méthode Main
Créer un STATEMENT ste = conn.createStatement();
Requete SQL d’ajout String req = "Insert into personne
values("+null+",'A2','3info')"; Executer la Requete
ste.executeUpdate(req) (insert, update, delete)
ste.executeQuery(req) select
![Page 10: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/10.jpg)
Méthodes CRUD
Créer 4 méthodes static : static void Ajouter(String Nom, String
Prenom){ } static void UpdatePrenom(int id, String
Prenom){ } static void Delete(int id){ } static void AfficherAll(){ }
Utiliser le ResultSet pour récupérer le résultat de executeQuery
ajouter throws SQLException pour chaque Methode
![Page 11: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/11.jpg)
Correspondance Java / BD
Type JDBC/SQL Méthode Java CHAR, VARCHAR getString() BINARY, VARBINARY getBytes() BIT getBoolean() INTEGER getInt() BIGINT getLong() SMALLINT getShort() REAL getFloat() DOUBLE, FLOAT getDouble() DATE getDate() TIME getTime() TIME STAMP getTimeStamp()
![Page 12: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/12.jpg)
Design Pattern
Singleton : permet d’instancier l’objet qu’une seule fois.
![Page 13: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/13.jpg)
Design Pattern
Singleton : (dans notre cas) un attribut statique
private static Connection conn;
un constructeur déclaré en privé private MyConnection() { … }
une méthode statique servant de pseudo-constructeur public static Connection getInstance() { … }
![Page 14: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/14.jpg)
Design Pattern
DAO : Permet de regrouper les accès aux données persistantes dans des classes à part.
Séparation entre la couche Métier et les données.
![Page 15: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/15.jpg)
Les Packages
Créer 4 packages Entite
Classe Personne DAO
Classe PersonneDAO Metier
Classe MyConnection Test
Classe Main
![Page 16: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/16.jpg)
Metier
private static Connection conn; private MyConnection() { } public static Connection getInstance(){
if(conn==null){
MyConnection aa = new MyConnection();}return conn;}
![Page 17: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/17.jpg)
Entite
Créer une classe Personne private int id; private String nom; private String prenom;
Générer les constructeurs Générer les Getters et les Setters
![Page 18: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/18.jpg)
DAO
Créer une Classe PersonneDAO Connection
conn=MyConnection.getInstance(); Statement ste;
Méthodes CRUD public int Ajouter (Personne p) public Vector<Personne> readAll() public boolean update (Personne p) public boolean delete (int id) public Personne findById (int id)
![Page 19: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/19.jpg)
Ajouter
public int Ajouter(Personne p) throws SQLException {int cle=0;
ste=conn.createStatement(); String reqinsert ="INSERT INTO `personne` ( `nom` ,
`prenom` ) " + "VALUES ('"+p.getNom()+"', '"+p.getPrenom()+"');"; ste.executeUpdate(reqinsert); ResultSet rs=ste.getGeneratedKeys(); while (rs.next()) { cle=rs.getInt(1); }return cle;}
![Page 20: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/20.jpg)
DAO
Créer une Classe PersonneDAO Connection
conn=MyConnection.getInstance(); Statement ste;
Méthodes CRUD public int Ajouter (Personne p) public Vector<Personne> readAll() public boolean update (Personne p) public boolean delete (int id) public Personne findById (int id)
![Page 21: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/21.jpg)
readAll()
public Vector<Personne> readAll() throws SQLException{Vector<Personne> v=new Vector<Personne>();Statement ste=conn.createStatement();String req="Select * from personne;";ResultSet rs=ste.executeQuery(req);
while (rs.next()) { Personne p=new Personne(rs.getInt(1),
rs.getString(2), rs.getString(3)); v.add(p); }return v;}
![Page 22: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/22.jpg)
DAO
Créer une Classe PersonneDAO Connection
conn=MyConnection.getInstance(); Statement ste;
Méthodes CRUD public int Ajouter (Personne p) public Vector<Personne> readAll() public boolean update (Personne p) public boolean delete (int id) public Personne findById (int id)
![Page 23: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/23.jpg)
update
public boolean update(Personne p) throws SQLException {boolean test=false;Statement ste=conn.createStatement();
String req="UPDATE `j2me`.`personne` SET `nom` ='"+p.getNom()+"' WHERE `personne`.`id` ="+p.getId()+";";int res=ste.executeUpdate(req);
if(res!=0) test=true;return test;}
![Page 24: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/24.jpg)
DAO
Créer une Classe PersonneDAO Connection
conn=MyConnection.getInstance(); Statement ste;
Méthodes CRUD public int Ajouter (Personne p) public Vector<Personne> readAll() public boolean update (Personne p) public boolean delete (int id) public Personne findById (int id)
![Page 25: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/25.jpg)
delete
public boolean delete(int id) throws SQLException {boolean test=false; Statement ste=conn.createStatement(); String req="DELETE FROM `j2me`.`personne`
WHERE `personne`.`id` ='"+id+"';";int res=ste.executeUpdate(req);if(res!=0) test=true; return test;}
![Page 26: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/26.jpg)
DAO
Créer une Classe PersonneDAO Connection
conn=MyConnection.getInstance(); Statement ste;
Méthodes CRUD public int Ajouter (Personne p) public Vector<Personne> readAll() public boolean update (Personne p) public boolean delete (int id) public Personne findById (int id)
![Page 27: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/27.jpg)
FindByID
public Personne findById(int id){
Personne p = null;
try {
ste = conn.createStatement();
String req="select * from personne where id="+id;
ResultSet rs=ste.executeQuery(req);
rs.next();
p=new Personne(rs.getInt(1),rs.getString(2),rs.getString(3));
} catch (SQLException ex) {
System.out.println("id n'existe pas");
}
return p;
}
![Page 28: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/28.jpg)
Test
Instancier une PersonneDAO PersonneDAO per=new PersonneDAO();
Instancier une Personne Personne p=new Personne(1,"esprit",
"ecole"); Ajouter une personne Lire toutes les informations des
personnes per.Ajouter(p); System.out.println(per.readAll());
![Page 29: CONNEXION BASE DE DONNÉES Mini-projets Java-BD 2011-2012 Wael Mallek Imed Amri](https://reader038.vdocuments.site/reader038/viewer/2022110117/551d9d90497959293b8c6a95/html5/thumbnails/29.jpg)
Void Main
public static void main(String[] args) { PersonneDAO per=new PersonneDAO(); Personne p=new Personne(1,"ffghf", "gfghg"); try { per.Ajouter(p); System.out.println(per.readAll()); } catch (SQLException ex) {
} }