Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.47 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package graphic;
  7.  
  8.  
  9. import java.awt.*;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.WindowAdapter;
  13. import java.awt.event.WindowEvent;
  14. import java.sql.Statement;
  15. import java.io.IOException;
  16. import java.sql.Connection;
  17. import java.sql.DriverManager;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javax.swing.JOptionPane;
  23.  
  24.  
  25. /**
  26.  *
  27.  * @author rachid
  28.  */
  29. public class Atlas_db  extends Frame{
  30.  Button btnAjouter,btnModifier,btnValider,btnSupprimer,btnFirst,btnPrevious,btnNext,btnLast;
  31.     Label  lblNom,lblCap,lblPop,lblCon;
  32.     TextField txtNom,txtPop,txtCon,txtCap;
  33.     Panel pAction,pNavigation,pEtiquette,ptexte;
  34.     String Mode;
  35.     //Vector vPays=new Vector();
  36.     //int Indice=0;
  37.     ResultSet RS;
  38.     Connection connect;
  39.     Statement S;
  40.    
  41.  
  42.  
  43. public Atlas_db() throws SQLException{
  44.         super("Atlas");
  45.        
  46.         setSize(400,250);
  47.         this.setLayout(new FlowLayout());
  48.         lblNom=new Label("Nom       :");
  49.         lblCap=new Label("Capitale  :");
  50.         lblPop=new Label("Population:");
  51.         lblCon=new Label("Continent:");
  52.         txtNom=new TextField("",10);
  53.         txtPop=new TextField("",10);
  54.         txtCon=new TextField("",10);
  55.         txtCap=new TextField("",10);
  56.         btnAjouter=new Button("Ajouter");
  57.         btnModifier =new Button("Modifier");
  58.         btnValider =new Button("Valider");
  59.         btnSupprimer =new Button("Supprimer");
  60.         btnFirst  =new Button("First");
  61.         btnPrevious=new Button("Previous");
  62.         btnNext=new Button("Next");
  63.         btnLast=new Button("Last");
  64.  
  65.         //inserting in the Panels
  66.         pAction=new Panel(new GridLayout(1,4));
  67.         pAction.add(btnAjouter);pAction.add(btnModifier);pAction.add(btnValider);pAction.add(btnSupprimer);
  68.         pEtiquette=new Panel(new GridLayout(4,1));
  69.         pEtiquette.add(lblNom);
  70.         pEtiquette.add(lblPop);
  71.         pEtiquette.add(lblCon);
  72.         pEtiquette.add(lblCap);
  73.         ptexte=new Panel(new GridLayout(4,1));
  74.         ptexte.add(txtNom);
  75.         ptexte.add(txtCap);
  76.         ptexte.add(txtPop);
  77.         ptexte.add(txtCon);
  78.         pNavigation=new Panel(new GridLayout(1,4));
  79.         pNavigation.add(btnFirst);pNavigation.add(btnPrevious);pNavigation.add(btnNext);pNavigation.add(btnLast);
  80.  
  81.         add(pAction,BorderLayout.NORTH);
  82.         add(pEtiquette,BorderLayout.WEST);
  83.         add(ptexte,BorderLayout.CENTER);
  84.         add(pNavigation,BorderLayout.SOUTH);
  85.  
  86.  
  87.       //traitement
  88.  
  89. //*
  90.  
  91.  
  92.  
  93.       btnAjouter.addActionListener(new ActionListener(){
  94.             public void actionPerformed(ActionEvent e) {
  95.             activer();
  96.             Mode="add";
  97.             txtNom.setText("");
  98.             txtPop.setText("");
  99.             txtCon.setText("");
  100.             txtCap.setText("");
  101.             }
  102.         });
  103.  
  104.       btnModifier.addActionListener(new ActionListener(){
  105.             public void actionPerformed(ActionEvent e) {
  106.             String Nom,Pop,Con,Cap;
  107.             //System.out.println( f.exists() + " " +f.getCanvPays.sizonicalPath() );
  108.            Mode="mod";
  109.  
  110.             activer();
  111.             txtNom.setFocusTraversalKeysEnabled(true);
  112.             txtNom.setBackground(Color.red);
  113.             Nom=txtNom.getText();
  114.             Pop=txtPop.getText();
  115.             Con=txtCon.getText();
  116.             Cap=txtCap.getText();
  117.  
  118.                
  119.             }
  120.         });
  121.         btnValider.addActionListener(new ActionListener(){
  122.             public void actionPerformed(ActionEvent e) {
  123.                 int n;
  124.                 String Nom,Pop,Cont,Cap;
  125.                 desactiver();
  126.                 Nom=txtNom.getText();
  127.                 Pop=txtPop.getText();
  128.                 Cont=txtCon.getText();
  129.                 Cap=txtCap.getText();
  130.                 if(Mode.equals("mod")){
  131.                     try {
  132.                          n=S.executeUpdate("update pays set nom=" + Nom +
  133.                                  " ,pop=" + Pop + " ,con=" + Cont + " cap=" + Cap + " where no=" + RS.getString("n_pay"));
  134.                     } catch (SQLException ex) {
  135.                         Logger.getLogger(Atlas_db.class.getName()).log(Level.SEVERE, null, ex);
  136.                     }
  137.                 }
  138.                 else try {
  139.                      n= S.executeUpdate("insert into pays values(null," + Nom + "," + Cap + "," + Pop + "," + Cont);
  140.                 } catch (SQLException ex) {
  141.                     Logger.getLogger(Atlas_db.class.getName()).log(Level.SEVERE, null, ex);
  142.                 }
  143.             }
  144.         });
  145.         btnSupprimer.addActionListener(new ActionListener(){
  146.             public void actionPerformed(ActionEvent e) {
  147.               int n;
  148.                 desactiver();
  149.                 try {
  150.                     String req = "DELETE FROM pays WHERE n_pay=" + RS.getString("n_pay");
  151.                     n=S.executeUpdate(req);
  152.  
  153.                 } catch (SQLException ex) {
  154.                     Logger.getLogger(Atlas_db.class.getName()).log(Level.SEVERE, null, ex);
  155.                 }
  156.  
  157.             }
  158.         });
  159. //*
  160.     this.addWindowListener(new WindowAdapter(){
  161.           int Rep;
  162.             @Override
  163.      public void windowClosing(WindowEvent e) {
  164.  
  165.        Rep=JOptionPane.showConfirmDialog(null,"Ete vous sure de quité","configurate",JOptionPane.YES_NO_OPTION);
  166.        if (Rep==JOptionPane.YES_NO_OPTION){
  167.                     try {
  168.                         save();
  169.                     } catch (IOException ex) {
  170.                         Logger.getLogger(Atlas.class.getName()).log(Level.SEVERE, null, ex);
  171.                     }
  172.            System.exit(0);
  173.        }
  174.      }
  175.  });
  176.  
  177.  //*/
  178.  
  179.  //*/
  180.         btnFirst.addActionListener(new ActionListener(){
  181.             public void actionPerformed(ActionEvent e) {
  182.                 try {
  183.                    if(RS.first())
  184.                     afficher();
  185.  
  186.                 } catch (SQLException ex) {
  187.                     Logger.getLogger(Atlas_db.class.getName()).log(Level.SEVERE, null, ex);
  188.                 }
  189.            
  190.             }
  191.         });
  192.         btnPrevious.addActionListener(new ActionListener(){
  193.             public void actionPerformed(ActionEvent e) {
  194.                 try {
  195.                     if(RS.previous())
  196.                     afficher();
  197.                 } catch (SQLException ex) {
  198.                     Logger.getLogger(Atlas_db.class.getName()).log(Level.SEVERE, null, ex);
  199.                 }
  200.                
  201.             }
  202.  
  203.  
  204.         });
  205.         btnNext.addActionListener(new ActionListener(){
  206.             public void actionPerformed(ActionEvent e) {
  207.                 try {
  208.                     if(RS.next())
  209.                     afficher();
  210.                 } catch (SQLException ex) {
  211.                     Logger.getLogger(Atlas_db.class.getName()).log(Level.SEVERE, null, ex);
  212.                 }
  213.  
  214.            }
  215.         });
  216.  
  217.         btnLast.addActionListener(new ActionListener(){
  218.              public void actionPerformed(ActionEvent e) {
  219.                 try {
  220.                     if(RS.last())
  221.                     afficher();
  222.                 } catch (SQLException ex) {
  223.                     Logger.getLogger(Atlas_db.class.getName()).log(Level.SEVERE, null, ex);
  224.                 }
  225.            
  226.             }
  227.         });
  228.         this.charger();
  229.         this.desactiver();
  230.         this.afficher();
  231.         this.setVisible(true);
  232.     }
  233.  
  234.  
  235.  
  236.  
  237.  
  238. public void charger(){
  239.  
  240.     //Connection connect=null;
  241.         String uid="root";
  242.         String mdp="rachid";
  243.         String pilote="com.mysql.jdbc.Driver";//"sun.jdbc.odbc.JdbcOdbcDriver";
  244.         String Base="jdbc:mysql://localhost:3306/pays";//"jdbc:odbc:Globe";
  245.         try{
  246.             Class.forName(pilote);
  247.             connect=DriverManager.getConnection(Base,uid,mdp);
  248.             System.out.println("** Connexion avec la base "+Base+" est établié **");
  249.         }catch(Exception e){
  250.             System.out.println("Erreur: "+e);
  251.         }
  252.         //===============================
  253.  
  254.         try{
  255.         RS =S.executeQuery("select * from pays");
  256.         RS.next();
  257.                 //while(RS.next()){ // tant qu'il y a une ligne à exploiter
  258.             // on l'affiche à l'écran
  259.         System.out.println(WIDTH);
  260.                 System.out.println(RS.getString("Nom")+ "-"+
  261.             RS.getString("cap")+ "-"+
  262.             RS.getString("pop")+ "-"+
  263.             RS.getString("con"));
  264.         //}// ligne suivante
  265.  
  266.     } catch (Exception e){
  267.         System.out.println ("charger:Erreur  " + e);
  268.     }
  269.  
  270.  
  271. }
  272.         //fermeture de la base
  273.  public void disconect(String Base){
  274. try{
  275.             connect.close();
  276.             System.out.println("Base "+Base+" est Fermé");
  277.         }catch(Exception e){
  278.             System.out.println(e);
  279.         }
  280. }
  281.  
  282.  
  283.  
  284.  
  285. public void save() throws IOException{
  286.  
  287.     try{
  288.      /*int i=0;
  289.      File f = new File("Pays.txt");
  290.      FileWriter fw=new FileWriter(f);
  291.      BufferedWriter bw=new BufferedWriter(fw);
  292.      while (i<vPays.size()){
  293.      bw.write(((Pays)vPays.elementAt(i)).toString());   //.Nom+";"+Cap+";"+Pop";"+Cont";");
  294.      bw.newLine();
  295.      }
  296.      bw.close();*/
  297.         System.out.println("sortie de programe");
  298.      }
  299.     catch(Exception e)
  300.     {
  301.         System.out.println("erreur in opening file "+e);
  302.     }
  303. }
  304.    
  305.  
  306.     void afficher() throws SQLException
  307.  {
  308.        if(RS != null){
  309.       // Pays p=(Pays) vPays.elementAt(Indice);
  310.        txtNom.setText(RS.getString("nom"));
  311.        txtCap.setText(RS.getString("cap"));
  312.        txtCon.setText(RS.getString("con"));
  313.        txtPop.setText(RS.getString("pop"));
  314.  
  315.     }
  316.     }
  317.  
  318.     public void activer(){
  319.    btnValider.setEnabled(true);
  320.    txtNom.setEnabled(true);
  321.    txtCon.setEnabled(true);
  322.    txtCap.setEnabled(true);
  323.    txtPop.setEnabled(true);
  324.    btnAjouter.setEnabled(false);
  325.    btnModifier.setEnabled(false);
  326.    btnSupprimer.setEnabled(false);
  327.    btnFirst.setEnabled(false);
  328.    btnPrevious.setEnabled(false);
  329.    btnNext.setEnabled(false);
  330.    btnLast.setEnabled(false);
  331.  
  332. }
  333.  public void desactiver(){
  334.    btnValider.setEnabled(false);
  335.    txtNom.setEnabled(false);
  336.    txtCon.setEnabled(false);
  337.    txtCap.setEnabled(false);
  338.    txtPop.setEnabled(false);
  339.    btnAjouter.setEnabled(true);
  340.    btnModifier.setEnabled(true);
  341.    btnSupprimer.setEnabled(true);
  342.    btnFirst.setEnabled(true);
  343.    btnPrevious.setEnabled(true);
  344.    btnNext.setEnabled(true);
  345.    btnLast.setEnabled(true);
  346.  
  347. }
  348.  
  349.  
  350.  
  351.  
  352. //*
  353.  
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement