Insertion base de données MySQL avec java

Résolu
bakloutimarou - 15 févr. 2013 à 15:59
 M_R - 13 févr. 2020 à 11:45
Bonjour,
je veux insérer des données dans ma table MySQL.J'ai écrit le code suivant mais ça marche pas:
package com.controle.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class connectio {
public static void main(String[] args) throws Exception {
String url = "com.mysql.jdbc.Driver";
Connection con = null ;
try {

System.out.println("Connection au driver JDBC");
//moyPaiement=txtmoy.getSelectedText();
Class.forName(url);
System.out.println("Chargement du pilote Mysql réussi");

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/controle_indiciaire","marouen","97480934");
//insertion
String str = "insert into controleindiciaire(id,N°Comptes,Debit,Credit) VALUES (?,?,?,?)";
PreparedStatement stmt = con.prepareStatement(str);
stmt.setInt(1,1);
stmt.setInt(2,987456);
stmt.setDouble(3,152.05);
stmt.setDouble(4,152.05);
stmt.executeUpdate(str);
con.commit();
System.out.println("ligne insérée");
con.close();
//System.out.println(str+" ligne insérée");
}
catch(ClassNotFoundException cnfe){
System.out.println("Driver introuvable : ");
cnfe.printStackTrace();
}
catch(SQLException sqle){
System.out.println("Erreur SQL : ");
//Cf. Comment gérer les erreurs ?
}
catch(Exception e){
System.out.println("Autre erreur : ");
e.printStackTrace();
}
finally
{
if(con!=null){try{con.close();}catch(Exception e){e.printStackTrace();}}
//etc.
}}}
la console affiche
Connection au driver JDBC
Chargement du pilote Mysql réussi
Erreur SQL :
merci de m'aider.
A voir également:

9 réponses

alvinemambele Messages postés 72 Date d'inscription mardi 27 mai 2008 Statut Membre Dernière intervention 31 mai 2013 15
19 févr. 2013 à 18:30
je m'excuse, voici le deux partie du code qui t’intéresse

private class SauverEnBase{
        String myUrl="";
        String myUser = "";
        String myPwd ="";
        String Data="";
        private Connection cn=null;
        //Statement st=null;
        private java.sql.Statement st = null;

        private SauverEnBase(String url, String user, String password){
            this.myUrl=url;
            this.myUser=user;
            this.myPwd=password;
        }
        
        private Boolean Connect(){
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                this.cn = DriverManager.getConnection("jdbc:mysql:"+this.myUrl, this.myUser, this.myPwd);
                this.st = this.cn.createStatement();
                return true;
            } catch (SQLException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
            //Class.forName("com.mysql.jdbc.Driver").newInstance();
            return false;
        }
        
        private ResultSet Execut (String SqlString){
            try {
                ResultSet rs = this.st.executeQuery(SqlString);
                return rs;
            } catch (SQLException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }
        
        private int myUpdate (String SqlString){
            try {
                int rs = this.st.executeUpdate(SqlString);
                return rs;
            } catch (SQLException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
            return 0;
        }
        
        private void Close(){
            try {
                this.st.close();
                this.cn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }


cette partie vous permet d'appeler la classe et faire votre insertion

SauverEnBase mysqlCli = new SauverEnBase("//localhost/psbase", "root", "");
            if (mysqlCli.Connect()) {
               int rs = mysqlCli.myUpdate("INSERT INTO devises (Libelle_Devise, Pays_Devise, Taux_Tevise) VALUE ('USD', 'USA', 919)");
            } else {
                System.out.println("Mysql connection failed !!!");
            }
        mysqlCli.Close();


Un orateur trop long est comme une horloge qui sonne les minutes!
3