Bonjour,

je voulais faire une petite connexion JDBC mysql avec une base de données.
Lorsque je lance mon programme (j'ai fait un debug run) celui ci me revoie un message d'exception bizarre après un peu de temps (sans planter pendant 5 mins) dans le createStatement()(dans Connexion) (insert de mon TestLivreBd).

J'utilise NetBeans, j'ai ajouté mon driver par lui.
C'est le test d'un bean et de sa sérialisation dans un projet java web ou j'utilise TomCat (le projet est complètement fait dans netbeans)
Le connecteur JDBC mysql de netbeans est le 5.1.6. Je viens d'essayer avec le dernier driver à jour c'est à dire 5.1.14 mais toujours le même problème.

Voici le code :
Livre :
Code : 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
 
 
package catalogue;
 
import database.Connexion;
import database.DatabaseInterface;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class Livre implements java.io.Serializable, DatabaseInterface{
 
    private int idLivre;
 
    private String titre;
 
    private double prix;
 
    private String langue;
 
    private String ISBN;
 
    private String auteur;
 
    private String editeur;
 
    public Livre () {
    }
 
    public Livre(int id) {
        this.idLivre = id;
        this.select();
    }
 
    public Livre(String titre, double prix, String langue, String ISBN, String auteur, String editeur) {
        this.titre = titre;
        this.prix = prix;
        this.langue = langue;
        this.ISBN = ISBN;
        this.auteur = auteur;
        this.editeur = editeur;
    }
 
    public Livre(int idLivre, String titre, double prix, String langue, String ISBN, String auteur, String editeur) {
        this.idLivre = idLivre;
        this.titre = titre;
        this.prix = prix;
        this.langue = langue;
        this.ISBN = ISBN;
        this.auteur = auteur;
        this.editeur = editeur;
    }
 
    public String getISBN () {
        return ISBN;
    }
 
    public void setISBN (String val) {
        this.ISBN = val;
    }
 
    public String getLangue () {
        return langue;
    }
 
 
    public void setLangue (String val) {
        this.langue = val;
    }
 
    public double getPrix () {
        return prix;
    }
 
    public void setPrix (double val) {
        this.prix = val;
    }
 
    public String getTitre () {
        return titre;
    }
 
    public void setTitre (String val) {
        this.titre = val;
    }
 
    public String getAuteur() {
        return auteur;
    }
 
    public void setAuteur(String auteur) {
        this.auteur = auteur;
    }
 
    public String getEditeur() {
        return editeur;
    }
 
    public void setEditeur(String editeur) {
        this.editeur = editeur;
    }
 
    public int getIdLivre() {
        return idLivre;
    }
 
    public void setIdLivre(int idLivre) {
        this.idLivre = idLivre;
    }
 
    /**
     * Valorise toutes les variables membres du bean à partir d'un jeu de donnée sql
     *
     * @access  private
     * @param   ResultSet   rs  jeu de resultat d'une requete sql
     */
    public void setAllValues(ResultSet rs) throws SQLException {
        this.ISBN = rs.getString("isbn");
        this.auteur = rs.getString("auteur");
        this.editeur = rs.getString("editeur");
        this.idLivre = rs.getInt("idLivre");
        this.langue = rs.getString("langue");
        this.prix = rs.getDouble("prix");
        this.titre = rs.getString("titre");
    }
 
    /**
     * CRUD.select
     *
     * @description     Rechercher les infos d'un template dans la base et valorise le beans
     * @access          public
     * @return          boolean resultat de la valorisation du beans
     */
    public boolean select(){
 
        String sql = "SELECT * FROM Livre WHERE idLivre = " + this.idLivre;
 
        try {
            ResultSet rs = Connexion.requeteSQL(sql);
            rs.next();
            setAllValues(rs);
        }
        catch(SQLException e) {
            System.out.println("\n<--------------------------------------->");
            System.out.println("Erreur lors de la recherche de livre : " + e.getMessage());
            System.out.println(sql);
            return false;
        }
        return true;
    }
 
    /**
     * CRUD.insert
     *
     * @description     Insert un nouveau template dans la base et retourne son identifiant
     * @access          public
     * @return          integer id du template inséré
     */
    public int insert() {
 
        String sql = "INSERT INTO livre (idLivre, titre, langue, auteur, editeur, isbn, prix) " +
                "VALUES (null, '" + titre + "', '" + langue + "', '" + auteur + "', '" + editeur + "', '" + ISBN + "', " + prix + ") ";
	int cle=0;
 
        try {
            cle = Connexion.requeteInsertSQL(sql, "livre");
            System.out.println(sql);
            return cle;
        }
        catch(SQLException e){
 
            System.out.println("\n<--------------------------------------->");
            System.out.println("Erreur lors de la création de livre : " + e.getMessage());
            System.out.println(sql);
            return cle;
        }
    }
 
    /**
     * CRUD.update
     *
     * @description     Mets à jour un template dans la base
     * @access          public
     * @return          boolean resultat de l'opération de mise à jour
     */
    public boolean update(){
 
        String sql = "UPDATE Livre SET "
		+ " titre = '"+ titre + "' "
		+ " WHERE idLivre = '" + idLivre + "' ";
        try {
            Connexion.requeteUpdateSQL(sql);
        }
        catch(SQLException e) {
            System.out.println("\n<--------------------------------------->");
            System.out.println("Erreur lors de la mise à jour de livre : " + e.getMessage());
            System.out.println(sql);
            return false;
        }
        return true;
    }
 
    /**
     * CRUD.delete
     *
     * @description     Supprime un template dans la base
     * @access          public
     * @return          boolean resultat de l'opération de mise à jour
     */
    public boolean delete() {
 
        String sql = new String();
 
        try {
            sql = "DELETE FROM Livre WHERE idLivre = '" + idLivre + "' ";
            Connexion.requeteUpdateSQL(sql);
        }
        catch(SQLException e) {
            System.out.println("\n<--------------------------------------->");
            System.out.println("Erreur lors de la suppression de livre : " + e.getMessage());
            System.out.println(sql);
            return false;
        }
        return true;
    }
 
    @Override
    public String toString() {
        return "Livre " + titre + " " + langue + " " + auteur + " " + prix + " " + editeur + " " + ISBN;
    }
}
TestLivreBd ou est mon problème
Code : 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
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package catalogue;
 
/**
 *
 * @author Emilien
 */
public class TestLivreBd {
     public static void main(String args[]) {
 
        System.out.println("-----------------------------");
        System.out.println("Insertion d'un livre");
 
        Livre l = new Livre("livre de test", 15.55, "français", "Aaaa", "unAuteur", "unEditeur");
        int idLivreInsere = l.insert(); //ici
        System.out.println("id livre inséré : " + idLivreInsere);
 
        System.out.println("-----------------------------");
        System.out.println("Lecture d'un livre");
 
        Livre l2 = new Livre(idLivreInsere);
        System.out.println("Nom du livre lu : " + l2);
 
        System.out.println("-----------------------------");
        System.out.println("Mise à jour d'un livre");
 
        l2.setAuteur("unVRAIVRAIAuteur");
        boolean upd = l2.update();
        System.out.println("Resultat de la mise à jour : " + upd);
        System.out.println("Nom du template mis à jour : " + l2);
 
        System.out.println("-----------------------------");
        System.out.println("Suppression d'un template");
 
        Livre l3 = new Livre(idLivreInsere);
       // boolean del = l3.delete();
       // System.out.println("Resultat de la suppression : " + del);
 
        System.out.println("-----------------------------");
 
    }
}
C'est le insert qui bug... voici la suite

Connexion
Code : 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
 
package database;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class Connexion {
 
	private static Connection connexion;
 
	private static void creerConnexion(){
		try{
			Class.forName("org.gjt.mm.mysql.Driver").newInstance();
			connexion = DriverManager.getConnection("jdbc:mysql://localhost:8888/bdCatalogue","root","");
			if (connexion == null){
				System.out.println("Erreur de connexion à la base");
				return;
			}
		}
		catch(Exception e){
			System.out.println("Erreur durant la connexion à la base de données" + e.getMessage());
                        e.printStackTrace();
		}
	}
 
	public static Connection getConnexion() {
		if(connexion==null)
			creerConnexion();
		return connexion;
	}
 
	public static ResultSet requeteSQL(String sql) throws SQLException{
		try {
			Statement stmt = getConnexion().createStatement();
			ResultSet rs = stmt.executeQuery (sql);
			return rs;
		}
		catch(Exception e){
			System.out.println("Erreur durant la requete");
			System.out.println(sql);
			return null;
		}
	}
 
	public static boolean requeteUpdateSQL(String sql) throws SQLException{
		try {
			Statement stmt = getConnexion().createStatement();
			int rs = stmt.executeUpdate(sql);
			return true;
		}
		catch(Exception e){
			System.out.println("Erreur durant la requete");
			System.out.println(sql);
			return false;
		}
	}
 
 
	public static int requeteInsertSQL(String sql, String table) throws SQLException{
		int i;
                Statement stmt = null;
                try{
                    stmt = Connexion.getConnexion().createStatement();
                } catch(Exception e){
                    e.printStackTrace();
                }
 
		//if(table!=null)
		//	stmt.executeQuery("LOCK TABLES " + table + " WRITE");
		stmt.executeUpdate (sql);
		int cle = 0;
		if(table!=null){
			ResultSet rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");
			rs.next();
			cle = rs.getInt(1);
			//stmt.executeQuery("UNLOCK TABLES");
		}
		return cle;
	}
}
(et l'interface pas très utile mais bon voila :
Code : 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
 
 
package database;
 
import java.sql.ResultSet;
import java.sql.SQLException;
 
public interface DatabaseInterface {
 
	//public void setAllValues(ResultSet rs) throws SQLException;
	public boolean select();
	public int insert();
	public boolean update();
	public boolean delete();
 
}
)

La sortie :
Code : 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
 
-----------------------------
Insertion d'un livre
Erreur durant la connexion à la base de donnéesCommunications link failure
 
Last packet sent to the server was 0 ms ago.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
 
Last packet sent to the server was 0 ms ago.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2103)
        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)
        at database.Connexion.creerConnexion(Connexion.java:16)
        at database.Connexion.getConnexion(Connexion.java:30)
        at database.Connexion.requeteInsertSQL(Connexion.java:65)
        at catalogue.Livre.insert(Livre.java:206)
        at catalogue.TestLivreBd.main(TestLivreBd.java:19)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
 
Last packet sent to the server was 1 ms ago.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
        at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:666)
        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1069)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
        ... 16 more
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
        at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2431)
        at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:590)
        ... 18 more
java.lang.NullPointerException
        at database.Connexion.requeteInsertSQL(Connexion.java:65)
        at catalogue.Livre.insert(Livre.java:206)
        at catalogue.TestLivreBd.main(TestLivreBd.java:19)
Exception in thread "main" java.lang.NullPointerException
        at database.Connexion.requeteInsertSQL(Connexion.java:72)
        at catalogue.Livre.insert(Livre.java:206)
        at catalogue.TestLivreBd.main(TestLivreBd.java:19)
Java Result: 1
BUILD SUCCESSFUL (total time: 5 minutes 1 second)

Avez vous une idée du problème ?

Merci par avance,
@+,
Tidus.