SQL语法错误附近“X')”在1号线(Insert语句)
问题描述:
我越来越想运行这个简单的SQL语句的问题。SQL语法错误附近“X')”在1号线(Insert语句)
try{
stm.executeUpdate("INSERT INTO exam_somatique_6_12(id_p, id_m, id_u, Date, age, poids, taille, TA, exam_clinique, acuite_visuelle, acuite_auditive, age_puberte, conclusion) VALUES ("+idpat+","+idmed+","+idum+",'"+currentdate+"',"+txtage.getText()+","+txtpoids.getText()+","+txttaille.getText()+","+txtta.getText()+",'"+Clinique+"','"+Visuelle+"', '"+Auditive+"', "+Signe+", '"+txtobservation.getText()+"')");
}
catch(SQLException e1)
{
System.err.println(e1.getMessage());
}
dispose();
我在MySQL上运行时,它都没有问题,但是当我尝试这样做在Java中,我得到这个消息的错误:
语法错误附近“‘X’)”在第1行
而x是txtobservation.getText()的结果。
而且,我敢肯定这不是一个问题帖,我使用“”时,它的文本,而不是做它时,它是一个整数。
感谢您的帮助。
答
你必须在使用PreparedStatement代替它的是更安全,更有益的
String query = "INSERT INTO table(id_p, id_m, id_u, Date, age, poids, taille,
TA, clinique, visuelle, auditive, puberte, observation)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement ps = connection.prepareStatement(query) {
ps.setInt(1, idpat);//set values to your query
ps.setInt(2, idmed);
....
ps.executeUpdate();//execute your query
}
注意
的getText它返回的字符串,而不是诠释,而不是浮动,如果txtage.getText()
为int你必须将其转换为int你可以使用:
Integer.parseInt(txtage.getText());//get int value form a String
Float.parseFloat(txtpoids.getText());//get float value from a String
等
这将是有助于打印正在执行 – shiladitya
了解如何使用参数的准确查询。然后你不会遇到这些问题,因为这个字符串会导致语法问题。 –
@shiladitya我不想语句太长,这就是为什么我已经改变了表的名称。无论如何,我已编辑它,这是确切的查询。 – Adel