2017-04-01 112 views
0

我越來越想運行這個簡單的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()的結果。

而且,我敢肯定這不是一個問題帖,我使用「」時,它的文本,而不是做它時,它是一個整數。

感謝您的幫助。

+1

這將是有助於打印正在執行 – shiladitya

+0

瞭解如何使用參數的準確查詢。然後你不會遇到這些問題,因爲這個字符串會導致語法問題。 –

+0

@shiladitya我不想語句太長,這就是爲什麼我已經改變了表的名稱。無論如何,我已編輯它,這是確切的查詢。 – Adel

回答

1

你必須在使用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