Bonsoir,

J'ai un soucis dans une portion de code qui lit dans un fichier .txt avant insertion dans un table mySQL...
J'ai réalisé le même type de classe sans problèmes, mais là je coince sur une erreur.

En fait, la connexion se passe sans problème, mais dès que je souhaite exécuter une requête à l'aide d'un prepared statement ya un bug (j'ai testé un statement qui ne fonctionne pas, avec la même erreur).

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
Exception SQL : 
Message = You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index' at line 1
SQLState = 42000
ErrorCode = 1064
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index' at line 1
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
	at com.mysql.jdbc.Connection.execSQL(Connection.java:3250)
	at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1355)
	at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1270)
	at index.index_import.connect_bdd(index_import.java:101)
	at index.index_import.hierarchie_2(index_import.java:50)
	at index.index_import.main(index_import.java:20)
Voici le bout de code concerné :

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
package index;
 
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
 
public class index_import {
 
 
	public static void main(String[] args) throws IOException{
 
		hierarchie_2("lMCL.txt");
 
	}
 
	public static void alpha(String nom) throws IOException{
		BufferedReader lecteurAvecBuffer = null;
	    String ligne;
	    String[] lg;
 
	    try{
	    	lecteurAvecBuffer = new BufferedReader(new FileReader(nom));
	    }catch(FileNotFoundException exc){
	    	System.out.println("Erreur d'ouverture");
	    }
 
	    while ((ligne = lecteurAvecBuffer.readLine()) != null){
 
	    	lg = ligne.split("\t");
	    	System.out.println(lg[0]);
 
		}
	    lecteurAvecBuffer.close();
}
 
	public static void hierarchie_2(String nom) throws IOException{
			BufferedReader lecteurAvecBuffer = null;
		    String ligne;
		    String[] lg;
		    String niv_sup;
 
		    Connection connexion = connect_bdd();
 
		    try{
		    	lecteurAvecBuffer = new BufferedReader(new FileReader(nom));
		    }catch(FileNotFoundException exc){
		    	System.out.println("Erreur d'ouverture");
		    }
 
		    while ((ligne = lecteurAvecBuffer.readLine()) != null){
 
		    	lg = ligne.split("\t");
 
		    	if(lg.length<2){
		    		System.out.println(lg[0]);
		    		niv_sup = lg[0];
		    		insert_bdd(connexion,nom,lg[0],"","");
		    	}else{
		    		System.out.println("--- "+lg[1]);
		    	}
			}
		    lecteurAvecBuffer.close();
	}
 
	public static Connection connect_bdd(){
		Connection connection = null;
		try {
			try {
				//Charge le driver JDBC
				Class.forName("com.mysql.jdbc.Driver").newInstance();
				System.out.println("Driver chargé!");
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
 
		//Paramètres de connexion à la base de données
		String url = "jdbc:mysql://localhost/webcath";
		String login = "root";
		String password = "";
		System.out.println("Paramètres de connexion chargés!");
		try{
			//Connexion à la base de données
			connection = DriverManager.getConnection(url,login,password);
			System.out.println("Connexion à la bdd ok!");
 
			PreparedStatement pstm;
 
			pstm = connection.prepareStatement("TRUNCATE TABLE index");
			pstm.executeUpdate();
 
		}
		catch(SQLException sqle){
			System.out.println("Exception SQL : ");
			while (sqle != null) {
				String message = sqle.getMessage();
				String sqlState = sqle.getSQLState();
				int errorCode = sqle.getErrorCode();
				System.out.println("Message = "+message);
				System.out.println("SQLState = "+sqlState);
				System.out.println("ErrorCode = "+errorCode);
				sqle.printStackTrace();
				sqle = sqle.getNextException();
			}
		}
		return connection;
	}
 
	/**
         * Import dans la base de données
         */
	public static void insert_bdd(Connection connection,String nom,String h1,String h2,String h3){
 
		try {
 
			//Préparation de la requète
			PreparedStatement pstm = connection.prepareStatement("INSERT INTO index (nom,h1,h2,h3) VALUES(?,?,?,?)");
 
			//Insertion dans la base de données
 
			//Complète le PreparedStatement avec les données de l'objet en cours
			pstm.setString(1,nom);
			pstm.setString(2,h1);
			pstm.setString(3,h2);
			pstm.setString(4,h3);
 
			//Exécute la requète
			pstm.executeUpdate();
 
		}
		catch(SQLException sqle){
			System.out.println("Exception SQL : ");
			while (sqle != null) {
				String message = sqle.getMessage();
				String sqlState = sqle.getSQLState();
				int errorCode = sqle.getErrorCode();
				System.out.println("Message = "+message);
				System.out.println("SQLState = "+sqlState);
				System.out.println("ErrorCode = "+errorCode);
				sqle.printStackTrace();
				sqle = sqle.getNextException();
			}
		}
	}
 
}
Merci pour l'aide que vous pourrez m'apporter!