besoin d'un peu d'aide à ce sujet. Avoir quelques problèmes avec une exception et je suis très nouveau dans l'utilisation de cette bibliothèque. Merci d'avance :)Instruction préparée avec HashMap, Exception
Erreur:
com.mysql.jdbc.exceptions.jdbc4.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 ''common_translations.name' as nm JOIN ('common_translations.name_id' as nid)
Mon code:
private DatabaseConnection db;
private final HashMap <String, String> statements;
public DatabaseReader(DatabaseConnection db) {
statements = new HashMap<String, String>() {
private static final long serialVersionUID = -1827340576955092045L;
{
put("odds","vfl::%");
put("common_translations", "vhc::%");
put("common_translations","vdr::%");
put("common_translations", "vto::%");
put("common_translations","vbl::%");
put("common_translations", "vf::%");
put("odds","vsm::%");
put("odds", "rgs::%");
put("odds", "srrgs::%");
}};
this.db = db;
}
public void read() {
try {
Connection connection = db.connect(db.getUrl_common_translation());
PreparedStatement ps = (PreparedStatement) connection.prepareStatement("SELECT nm.id, nid.key, nm.name FROM ? as nm JOIN (? as nid)\r\n" +
" ON (nm.id = nid.id) where nid.key like ? and nm.typeId=8 and nm.sourceId=-1 and nm.languageCode='en'");
for(Entry <String,String> e : statements.entrySet()) {
ps.setString(1, e.getKey() + ".name");
ps.setString(2, e.getKey() + ".name_id");
ps.setString(3, e.getValue());
ResultSet rs = ps.executeQuery();
while(rs.next()) {
int id = rs.getInt("id");
//String tag = rs.getString("tag");
//String translation = rs.getString("translation");
System.out.println(id);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
Vous ne pouvez pas utiliser le système '' placeholders pour remplacer un nom de table ou un nom de colonne, il ne fonctionne que pour les valeurs. – Berger
Oh d'accord, merci. Avez-vous peut-être des conseils pour le faire de manière intelligente? –
Eh bien peut-être que vous pourriez utiliser un 'StringBuilder' pour construire votre chaîne de requête en fonction des tables ciblées. – Berger