bonjour j'ai créer une classe BD pour la connexion les insertion dans une BD
voici la classe

Code java : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package packageBD;

import java.sql.*;

/**
 *
 * @author stagiaire
 */
public class BD {

    private String bdname;
    private String usr;
    private String pwd;
    private Connection cnx = null;

    public BD()
    {
        
    }
    public BD(String bdname, String usr, String pwd) {
        this.usr = usr;
        this.pwd = pwd;
        this.bdname = "jdbc:mysql://localhost/" + bdname;
    }

    public StringBuilder seConnecter(String bdname, String usr, String pwd) {

        StringBuilder msg = new StringBuilder("");
        try {
            Class.forName("org.gjt.mm.mysql.Driver"); // org.gjt.mm.mysql.Driver
            //Class.forName("com.mysql.jdbc.Driver");
            this.cnx = DriverManager.getConnection("jdbc:mysql://localhost/" + bdname, usr, pwd);
            cnx.setAutoCommit(false);
            msg.append("OK");
        } catch (ClassNotFoundException err) {
            msg.append(err.getMessage());
        } catch (SQLException err) {
            msg.append(err.getMessage());
        }
        return msg;
    }

    public boolean seDeconnecter() {
        this.cnx = null;
        return true;
    }

    public Connection getConnexion()
    {
        return this.cnx;
    }

    // ------------------------
    public ResultSet getCurseur(String asSelect)
    // ------------------------
    {
            // --- Pour créer des curseurs En avant et RO
            Statement  lstSql = null;
            ResultSet  lrs    = null;

            try
            {
                lstSql = this.cnx.createStatement();
                lrs    = lstSql.executeQuery(asSelect);
            }
            catch(SQLException err)
            {
                lrs = null;
            }
            return lrs;
        }


        // ------------------------------- 
        public StringBuilder getCurseurCSV(String asSelect)
        // ------------------------------- 
        { 
            StringBuilder lsbResultat = new StringBuilder("");
            ResultSet lrs = null;
            ResultSetMetaData lrsmd; 
        // --- Objet structure de curseur 
        // --- Récupération du contenu du curseur
            lrs = this.getCurseur(asSelect);
        try { 
            // --- La structure du curseur 
            lrsmd = lrs.getMetaData(); 
            // --- Les en-têtes 
            for(int i=1; i<=lrsmd.getColumnCount(); i++)
            { 
                lsbResultat.append(lrsmd.getColumnName(i) + ";"); 
            } 
            lsbResultat.deleteCharAt(lsbResultat.length()-1);
            lsbResultat.append("\r\n");
            while(lrs.next()) 
            { 
                for(int i=1; i<=lrsmd.getColumnCount(); i++) 
                { 
                    lsbResultat.append(lrs.getString(i) + ";");
                } 
                lsbResultat.deleteCharAt(lsbResultat.length()-1);
                lsbResultat.append("\r\n");
            } 
        } 
        catch(SQLException err) { 
            lsbResultat.append(err.getMessage());
        } 

        return lsbResultat; 
    }
        
        
        
        //  fonction insertion 
        
        public  int  inserer(String aTable,String aColonnes, String  avaleur) throws SQLException
        {
          //    StringBuilder msg = new StringBuilder("");
            Statement  lstSql = null;
            int  lrs    ;
            
         
              lstSql =this.cnx.createStatement();
             //  String requete ="insert into "+aTable+"("+aColonnes+") values("+avaleur+")" ;
               
                lrs  = lstSql.executeUpdate("INSERT INTO "+aTable+"("+aColonnes+")values("+avaleur+")");
                
              //   System.out.println(lrs);
              //     msg.append(lrs);
            
            
           
      return lrs;
            
        }
        
        public  int supprimer(String aTable,String aColonnes, String avaleur)
        {
        return 0;
        }
        
        
     
}


j'ai creer une autre classe Test qui contient ma fonction Main qui permet de faire des insertion

voici le code

Code java : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package packageBD;
 
import java.sql.ResultSet;
import java.sql.SQLException;
 
/**
 *
 * @author stagiaire
 */
public class Test_1 {
 
public static void main(String[] args) throws SQLException
{
    BD bd = new BD();
    StringBuilder lsbConnect = bd.seConnecter("test", "root", "");
     System.out.println(lsbConnect);
    /*
    StringBuilder lsbCSV = bd.getCurseurCSV2("SELECT * FROM clients");
    System.out.println(lsbCSV);
     */
 
        String  colonne ="id,contry";
 
        String  champ = "251,MAURITANIE";
 
       int  lsMessage = bd.inserer("countries",colonne,champ);
 
     System.out.println(lsMessage);
 
    bd.seDeconnecter();
}
}

voici ma trace d'erreur et j arrive a comprendre l'erreur

Code java : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
run:
OK
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'MAURITANIE' in 'field list'
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
	at com.mysql.jdbc.Util.getInstance(Util.java:384)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2690)
	at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1648)
	at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1567)
	at packageBD.BD.inserer(BD.java:129)
	at packageBD.Test_1.main(Test_1.java:31)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

merci d'avance de votre aide