Connection à une BD avec java

nadia_15 Messages postés 9 Statut Membre -  
myweb Messages postés 10 Statut Membre -
bonjour;
je travaille avec une BD à l'aide du java la connexion est marché mais quand je crée un nouveau compte ça semble bien marché mais en retournant à ma BD avec sql je trouve que le compte est inséré 2 fois dans la table car j'ai fais l'incremntation ds le code jv
remaque:
ds tous les codes la requete s'execute 2 fois.
je veux savoir pourquoi! est ce que c la connection qui se fait à chaque fois 2 fois de suite et comment je peux resoudre ce probleme..
merci d'avance pour m'avoir rependre..

1 réponse

  1. myweb Messages postés 10 Statut Membre 29
     
    voici un exemple de connection à une BD mysql avec java :

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    
    public class ConnectionMySQL extends JFrame
    	
    	implements ActionListener {
    	
    	public static void main(String st[]) {
    		CONNECTION_POOL_JDBC_TEST F = new CONNECTION_POOL_JDBC_TEST("kled");
    // ajoutez une image comme icone en indiquant le chemin
    		Image image = new ImageIcon("icone.ico").getImage();
    		F.setIconImage(image);
    		F.move(500, 500);
    		F.show();
    	}
    	Connection con;
    	private JTabbedPane jtab = new JTabbedPane();
    	private String [] nom = new String[2];
        private JPanel p1;
    	private JPanel p2;
    	private JTextField req;
    	private Object[][] resultat = new Object[100][4];
    	public ResultSet rs;
    	private Statement stm;
    	private JTable table;
    	private JButton valid, sortie;
    
    	public ConnectionMySQL(String titre) {
    		super(titre);
    		p1 = new JPanel(new FlowLayout());
    		valid = new JButton("Valider");
    		valid.setActionCommand("1");
    		valid.addActionListener(this);
    		sortie = new JButton("Sortie");
            sortie.setActionCommand("3");
    		sortie.addActionListener(this);
    
    		req = new JTextField(8);
    		req.setActionCommand("2");
    		req.addActionListener(this);
    		p1.add(new JLabel("Numéro:"));
    		p1.add(req);
    		p1.add(valid);
    		p1.add(sortie);
    		this.setSize(50, 30);
    		getContentPane().setLayout(new BorderLayout());
    		getContentPane().add(jtab);
            addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e) {
    				System.exit(0);
    			}
    		});
    		try 
    		{
    		 Class.forName("org.gjt.mm.mysql.Driver");
    		 // l'URL de la base doit être suivant cette forme khaled étant le nom de la base
             /*  A)*/ con =DriverManager.getConnection("jdbc:mysql:/127.0.0.1/khaled","","");
    		          stm = con.createStatement();
    		} 
    		catch (Exception ex) 
    		{
    		System.out.println("exception :" + ex.toString() + " lors de la construction");
    		}
    		setSize(500, 200);
    		pack();
    		show();
     
    	}
    	public void actionPerformed(ActionEvent e) {
    
    		if (e.getActionCommand()=="3") System.exit(0);
    		
    		if (e.getActionCommand() == "1"
    			|| e.getActionCommand() == "2")
    			 {
    
    			try {
    				if(req.getText()!=null)
    				{ /* B) */ rs = stm.executeQuery("select num, nom from personne where num >= "+req.getText()+";");} 
    						
    				ResultSetMetaData rsm = rs.getMetaData();
    				for (int i = 1; i <= rsm.getColumnCount(); i++)
    				nom[i-1] = rsm.getColumnName(i);
    				int h = 0;
    				if(!rs.next()){System.out.println("Erreur");}
    				else {
    				while (rs.next()) {
    					resultat[h][0] = rs.getString("num");
    					resultat[h][1] = rs.getString("nom");
    					h++;
    				                  }
    				}
    					
    			Object[][] tab = new Object[h][2];
    				for (int j = 0; j < h; j++) {
    					for (int k = 0; k < 2; k++)
    						tab[j][k] = resultat[j][k];
    											}
    					
    			table = new JTable(tab,nom);
    			table.setForeground(Color.black);
    			table.setGridColor(Color.BLACK);
    			
    
               System.err.println("sqddddddddddddddddddddddddddddddddddd");
    				
    				jtab.addTab("Rep", new JScrollPane(table));
    			
    				// pack();
    				show();
    
    			} 
    			catch (Exception ex) 
    			{
    				System.err.println("exception :" + ex.toString() + " lors de l'action");
    			}
    			
    		}
    
    	}
    
    }
    
    1