Advertisement
bebesurf

TD6 Prog BD

Apr 18th, 2018
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. ------------------------------------------- Application.java ----------------------------------
  2.  
  3. package appli;
  4. import bd.*;
  5. import bean.*;
  6. import java.sql.*;
  7. import dao.*;
  8.  
  9. public class Application {
  10.  
  11.     public static void main(String[] args) throws SQLException {
  12.         // TODO Auto-generated method stub
  13.         SessionOracle connection = new SessionOracle("VOTRE ID DE CO", "MOT DE PASSE");
  14.         connection.getConnection();
  15.        
  16.         DAOEmploye test = new DAOEmploye(connection);
  17.         Employe testE1 = new Employe("Jean", 2500, 55, 3, 32);
  18.        
  19.         test.delete(testE1);
  20.        
  21.     }
  22.  
  23. }
  24.  
  25.  
  26.  
  27. ---------------------------------------------- Employe.java -------------------------------------
  28.  
  29. package bean;
  30.  
  31. public class Employe {
  32.  
  33.  
  34.     private String employe;
  35.     private int salaire;
  36.     private int nuempl;
  37.     private int affect;
  38.     private int hebdo;
  39.    
  40.     public Employe(String employe, int salaire, int nuempl, int affect, int hebdo) {
  41.         super();
  42.         this.employe = employe;
  43.         this.salaire = salaire;
  44.         this.nuempl = nuempl;
  45.         this.affect = affect;
  46.         this.hebdo = hebdo;
  47.     }
  48.    
  49.     public String getEmploye() {
  50.         return employe;
  51.     }
  52.     public void setEmploye(String employe) {
  53.         this.employe = employe;
  54.     }
  55.     public int getSalaire() {
  56.         return salaire;
  57.     }
  58.     public void setSalaire(int salaire) {
  59.         this.salaire = salaire;
  60.     }
  61.     public int getNuempl() {
  62.         return nuempl;
  63.     }
  64.     public void setNuempl(int nuempl) {
  65.         this.nuempl = nuempl;
  66.     }
  67.     public int getAffect() {
  68.         return affect;
  69.     }
  70.     public void setAffect(int affect) {
  71.         this.affect = affect;
  72.     }
  73.     public int getHebdo() {
  74.         return hebdo;
  75.     }
  76.     public void setHebdo(int hebdo) {
  77.         this.hebdo = hebdo;
  78.     }
  79.    
  80.    
  81.    
  82.    
  83. }
  84.  
  85. ------------------------------------- DAOEmploye.java -------------------------------------------------
  86.  
  87. package dao;
  88. import bd.*;
  89. import bean.*;
  90. import java.sql.*;
  91.  
  92. public class DAOEmploye {
  93.    
  94.     private SessionOracle s;
  95.     private Statement stmt;
  96.    
  97.     public DAOEmploye(SessionOracle ss) throws SQLException
  98.     {
  99.         this.s = ss;
  100.         this.stmt = s.getConnection().createStatement();
  101.     }
  102.    
  103.    
  104.     public void create(Employe e)
  105.     {
  106.         System.out.println("Insertion de la ligne dans la table ...");
  107.         try {
  108.             stmt.executeUpdate("INSERT INTO EMPLOYE2 VALUES(" + e.getNuempl() + ",'" + e.getEmploye() + "'," + e.getHebdo() + "," + e.getAffect() + "," + e.getSalaire() + ")");
  109.             System.out.println("Insertion réussie !");
  110.         } catch (SQLException e1) {
  111.             System.out.println(e1);
  112.         }
  113.     }
  114.     public void delete(Employe e)
  115.     {
  116.         System.out.println("Supression de la ligne de la table ...");
  117.         try {
  118.             String deleteTableEmploye = new String("DELETE FROM EMPLOYE2 WHERE nuempl=" + e.getNuempl());
  119.             stmt.executeUpdate(deleteTableEmploye);
  120.             System.out.println(deleteTableEmploye);
  121.             System.out.println("Supression réussie !");
  122.         } catch (SQLException e1) {
  123.             System.out.println(e1);
  124.         }
  125.     }
  126.    
  127.     public void update(Employe e)
  128.     {
  129.         System.out.println("Mise à jour de l'employé ...");
  130.         try {
  131.             String updateTableEmploye = new String("UPDATE EMPLOYE2 SET nomempl=" + "'" + e.getEmploye() + "'" + ", hebdo=" + e.getHebdo() +", affect=" + e.getAffect() + ", salaire=" + e.getSalaire() + " WHERE nuempl=" + e.getNuempl());
  132.             System.out.println(updateTableEmploye);
  133.             stmt.executeUpdate(updateTableEmploye);
  134.             System.out.println("Mise à jour réussie !");
  135.         } catch (SQLException e1) {
  136.             System.out.println(e1);
  137.         }
  138.     }
  139.  
  140.     public void read()
  141.     {
  142.         System.out.println("Lecture de la table EMPLOYE2 ...");
  143.         try {          
  144.             String selectTableEmploye = new String("SELECT * FROM EMPLOYE2 ORDER BY 1");
  145.             ResultSet res = stmt.executeQuery(selectTableEmploye);
  146.             System.out.println("Lecture terminée !");
  147.             while(res.next()) {
  148.                 int numEmpl = res.getInt(1);
  149.                 String nomEmpl = res.getString(2);
  150.                 int hebdoEmpl = res.getInt(3);
  151.                 int affectEmpl = res.getInt(4);
  152.                 int salaireEmpl = res.getInt(5);
  153.                 System.out.println(" nuempl" + "        " + "nomempl" + "            " + "hebdo" + "      " + "affect" + "      " + "salaire");
  154.                 System.out.println("   " + numEmpl + "          " + nomEmpl +  hebdoEmpl + "          " + affectEmpl + "         " + salaireEmpl);
  155.                 System.out.println("");
  156.             }
  157.         }
  158.        
  159.         catch (SQLException e) {
  160.             System.out.println(e.getMessage());
  161.             }  
  162.     }
  163. }
  164.  
  165. --------------------------------------------------------- SessionOracle.java --------------------------------------
  166.  
  167. package bd;
  168. import java.sql.*;
  169.  
  170.  
  171. public class SessionOracle  {
  172.    
  173.     public Connection conn;
  174.     public SessionOracle(String user, String passwd) { 
  175.        
  176.         try {
  177.             DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
  178.             conn = DriverManager.getConnection("jdbc:oracle:thin:@soracle3.iut-nantes.univ-nantes.prive:1521:DB1",user , passwd);          
  179.             System.out.println("connexion réussi");           
  180.         }      
  181.         catch(SQLException e){
  182.             System.out.println("connexion échouée");
  183.         }
  184.        
  185.     }
  186.    
  187.     public Connection getConnection()
  188.     {
  189.         return conn;
  190.     }  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement