Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. package base;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.util.ArrayList;
  8. import java.util.ResourceBundle;
  9.  
  10. import bean.Livre;
  11.  
  12. public class Base {
  13.    
  14.     static {
  15.         try{
  16.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  17.            
  18.         }
  19.         catch(Exeption e){
  20.            
  21.         }
  22.     }
  23.    
  24.    
  25.     Connection connection = null;
  26.    
  27.     public Base() {
  28.         String url = null;
  29.         String user = null;
  30.         String password = null;
  31.  
  32.         // accès au ressources (fichier base/config.properties)
  33.         try {
  34.             ResourceBundle rs =
  35.                 ResourceBundle.getBundle("base/config");
  36.             url = rs.getString("url");
  37.             user = rs.getString("user");
  38.             password = rs.getString("password");
  39.             System.out.println("url = "+url);
  40.             System.out.println("user = "+user);
  41.         }
  42.         catch (Exception e) {
  43.             System.out.println("Erreur acces ressources "+e.getMessage());
  44.         }
  45.        
  46.         // connexion BD
  47.         try  {
  48.             connection = DriverManager.getConnection(url, user, password);
  49.         }
  50.         catch (Exception e) {
  51.             System.out.println("Erreur connexion "+e.getMessage());
  52.         }
  53.     }
  54.    
  55.     public void fermer() {
  56.         try {if (connection != null) connection.close();} catch (Exception e) {}
  57.     }
  58.  
  59.     public ArrayList<Livre> listerLivres() {
  60.        
  61.         ArrayList<Livre> res = new ArrayList<>();
  62.        
  63.         String sql = "select * from t_livre";
  64.        
  65.         try {
  66.             PreparedStatement ps = connection.prepareStatement(sql);
  67.             ResultSet rs = ps.executeQuery();
  68.             while (rs.next()) {
  69.                 int idLivre = rs.getInt("idLivre");
  70.                 String titre = rs.getString("titre");
  71.                 String auteur = rs.getString("auteur");
  72.                 int annee = rs.getInt("annee");
  73.                 Livre l = new Livre(titre, auteur, annee);
  74.                 l.setIdLivre(idLivre);
  75.                 res.add(l);
  76.             }
  77.         }
  78.         catch (Exception e) {
  79.             System.out.println(
  80.                     "Erreur listerLivres "+e.getMessage());
  81.         }
  82.        
  83.         return res;
  84.     }
  85.    
  86.     public boolean enregistrerLivre(Livre l)  {
  87.         boolean res = false;
  88.         String sql = "insert into t_livre (titre, auteur, annee) values (?, ?, ? )";
  89.         try {
  90.             PreparedStatement ps = connection.prepareStatement(sql);
  91.             ps.setString(1, l.getTitre());
  92.             ps.setString(2, l.getAuteur());
  93.             ps.setInt(3, l.getAnnee());
  94.             System.out.println("sql = " +ps.toString());
  95.             int nb = ps.executeUpdate();
  96.             if (nb == 1) res = true;
  97.  
  98.  
  99.         } catch (Exception e) {
  100.             System.out.println(
  101.                     "Erreur enregistrerLivre "+e.getMessage());
  102.         }
  103.  
  104.         return res;
  105.     }
  106.  
  107.  
  108.     public static void main(String [] args) {
  109.         Base base = new Base();
  110.    
  111.  
  112.         Livre livre = new Livre
  113.                 ("titreB","auteurB",2019);
  114.         base.enregistrerLivre(livre);
  115.  
  116.         ArrayList<Livre> lst = base.listerLivres();
  117.         for (Livre l : lst) {
  118.             System.out.println("titre = "+l.getTitre());
  119.         }
  120.     }
  121.  
  122.    
  123.        
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement