Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package graphic;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.sql.Statement;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- /**
- *
- * @author rachid
- */
- public class Atlas_db extends Frame{
- Button btnAjouter,btnModifier,btnValider,btnSupprimer,btnFirst,btnPrevious,btnNext,btnLast;
- Label lblNom,lblCap,lblPop,lblCon;
- TextField txtNom,txtPop,txtCon,txtCap;
- Panel pAction,pNavigation,pEtiquette,ptexte;
- String Mode;
- //Vector vPays=new Vector();
- //int Indice=0;
- ResultSet RS;
- Connection connect;
- Statement S;
- public Atlas_db() throws SQLException{
- super("Atlas");
- setSize(400,250);
- this.setLayout(new FlowLayout());
- lblNom=new Label("Nom :");
- lblCap=new Label("Capitale :");
- lblPop=new Label("Population:");
- lblCon=new Label("Continent:");
- txtNom=new TextField("",10);
- txtPop=new TextField("",10);
- txtCon=new TextField("",10);
- txtCap=new TextField("",10);
- btnAjouter=new Button("Ajouter");
- btnModifier =new Button("Modifier");
- btnValider =new Button("Valider");
- btnSupprimer =new Button("Supprimer");
- btnFirst =new Button("First");
- btnPrevious=new Button("Previous");
- btnNext=new Button("Next");
- btnLast=new Button("Last");
- //inserting in the Panels
- pAction=new Panel(new GridLayout(1,4));
- pAction.add(btnAjouter);pAction.add(btnModifier);pAction.add(btnValider);pAction.add(btnSupprimer);
- pEtiquette=new Panel(new GridLayout(4,1));
- pEtiquette.add(lblNom);
- pEtiquette.add(lblPop);
- pEtiquette.add(lblCon);
- pEtiquette.add(lblCap);
- ptexte=new Panel(new GridLayout(4,1));
- ptexte.add(txtNom);
- ptexte.add(txtCap);
- ptexte.add(txtPop);
- ptexte.add(txtCon);
- pNavigation=new Panel(new GridLayout(1,4));
- pNavigation.add(btnFirst);pNavigation.add(btnPrevious);pNavigation.add(btnNext);pNavigation.add(btnLast);
- add(pAction,BorderLayout.NORTH);
- add(pEtiquette,BorderLayout.WEST);
- add(ptexte,BorderLayout.CENTER);
- add(pNavigation,BorderLayout.SOUTH);
- //traitement
- //*
- btnAjouter.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- activer();
- Mode="add";
- txtNom.setText("");
- txtPop.setText("");
- txtCon.setText("");
- txtCap.setText("");
- }
- });
- btnModifier.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- String Nom,Pop,Con,Cap;
- //System.out.println( f.exists() + " " +f.getCanvPays.sizonicalPath() );
- Mode="mod";
- activer();
- txtNom.setFocusTraversalKeysEnabled(true);
- txtNom.setBackground(Color.red);
- Nom=txtNom.getText();
- Pop=txtPop.getText();
- Con=txtCon.getText();
- Cap=txtCap.getText();
- }
- });
- btnValider.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- int n;
- String Nom,Pop,Cont,Cap;
- desactiver();
- Nom=txtNom.getText();
- Pop=txtPop.getText();
- Cont=txtCon.getText();
- Cap=txtCap.getText();
- if(Mode.equals("mod")){
- try {
- n=S.executeUpdate("update pays set nom=" + Nom +
- " ,pop=" + Pop + " ,con=" + Cont + " cap=" + Cap + " where no=" + RS.getString("n_pay"));
- } catch (SQLException ex) {
- Logger.getLogger(Atlas_db.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- else try {
- n= S.executeUpdate("insert into pays values(null," + Nom + "," + Cap + "," + Pop + "," + Cont);
- } catch (SQLException ex) {
- Logger.getLogger(Atlas_db.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- btnSupprimer.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- int n;
- desactiver();
- try {
- String req = "DELETE FROM pays WHERE n_pay=" + RS.getString("n_pay");
- n=S.executeUpdate(req);
- } catch (SQLException ex) {
- Logger.getLogger(Atlas_db.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- //*
- this.addWindowListener(new WindowAdapter(){
- int Rep;
- @Override
- public void windowClosing(WindowEvent e) {
- Rep=JOptionPane.showConfirmDialog(null,"Ete vous sure de quité","configurate",JOptionPane.YES_NO_OPTION);
- if (Rep==JOptionPane.YES_NO_OPTION){
- try {
- save();
- } catch (IOException ex) {
- Logger.getLogger(Atlas.class.getName()).log(Level.SEVERE, null, ex);
- }
- System.exit(0);
- }
- }
- });
- //*/
- //*/
- btnFirst.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- try {
- if(RS.first())
- afficher();
- } catch (SQLException ex) {
- Logger.getLogger(Atlas_db.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- btnPrevious.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- try {
- if(RS.previous())
- afficher();
- } catch (SQLException ex) {
- Logger.getLogger(Atlas_db.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- btnNext.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- try {
- if(RS.next())
- afficher();
- } catch (SQLException ex) {
- Logger.getLogger(Atlas_db.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- btnLast.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- try {
- if(RS.last())
- afficher();
- } catch (SQLException ex) {
- Logger.getLogger(Atlas_db.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- this.charger();
- this.desactiver();
- this.afficher();
- this.setVisible(true);
- }
- public void charger(){
- //Connection connect=null;
- String uid="root";
- String mdp="rachid";
- String pilote="com.mysql.jdbc.Driver";//"sun.jdbc.odbc.JdbcOdbcDriver";
- String Base="jdbc:mysql://localhost:3306/pays";//"jdbc:odbc:Globe";
- try{
- Class.forName(pilote);
- connect=DriverManager.getConnection(Base,uid,mdp);
- System.out.println("** Connexion avec la base "+Base+" est établié **");
- }catch(Exception e){
- System.out.println("Erreur: "+e);
- }
- //===============================
- try{
- RS =S.executeQuery("select * from pays");
- RS.next();
- //while(RS.next()){ // tant qu'il y a une ligne à exploiter
- // on l'affiche à l'écran
- System.out.println(WIDTH);
- System.out.println(RS.getString("Nom")+ "-"+
- RS.getString("cap")+ "-"+
- RS.getString("pop")+ "-"+
- RS.getString("con"));
- //}// ligne suivante
- } catch (Exception e){
- System.out.println ("charger:Erreur " + e);
- }
- }
- //fermeture de la base
- public void disconect(String Base){
- try{
- connect.close();
- System.out.println("Base "+Base+" est Fermé");
- }catch(Exception e){
- System.out.println(e);
- }
- }
- public void save() throws IOException{
- try{
- /*int i=0;
- File f = new File("Pays.txt");
- FileWriter fw=new FileWriter(f);
- BufferedWriter bw=new BufferedWriter(fw);
- while (i<vPays.size()){
- bw.write(((Pays)vPays.elementAt(i)).toString()); //.Nom+";"+Cap+";"+Pop";"+Cont";");
- bw.newLine();
- }
- bw.close();*/
- System.out.println("sortie de programe");
- }
- catch(Exception e)
- {
- System.out.println("erreur in opening file "+e);
- }
- }
- void afficher() throws SQLException
- {
- if(RS != null){
- // Pays p=(Pays) vPays.elementAt(Indice);
- txtNom.setText(RS.getString("nom"));
- txtCap.setText(RS.getString("cap"));
- txtCon.setText(RS.getString("con"));
- txtPop.setText(RS.getString("pop"));
- }
- }
- public void activer(){
- btnValider.setEnabled(true);
- txtNom.setEnabled(true);
- txtCon.setEnabled(true);
- txtCap.setEnabled(true);
- txtPop.setEnabled(true);
- btnAjouter.setEnabled(false);
- btnModifier.setEnabled(false);
- btnSupprimer.setEnabled(false);
- btnFirst.setEnabled(false);
- btnPrevious.setEnabled(false);
- btnNext.setEnabled(false);
- btnLast.setEnabled(false);
- }
- public void desactiver(){
- btnValider.setEnabled(false);
- txtNom.setEnabled(false);
- txtCon.setEnabled(false);
- txtCap.setEnabled(false);
- txtPop.setEnabled(false);
- btnAjouter.setEnabled(true);
- btnModifier.setEnabled(true);
- btnSupprimer.setEnabled(true);
- btnFirst.setEnabled(true);
- btnPrevious.setEnabled(true);
- btnNext.setEnabled(true);
- btnLast.setEnabled(true);
- }
- //*
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement