2017-04-01 7 views
0

이 간단한 SQL 문을 실행하는 동안 문제가 발생합니다.''x '근처에서 SQL 구문 오류가 발생했습니다.)'1 번째 줄에서 (삽입 문)

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은 그것을 실행할 때 나는 아무 문제가 없지만, 최대한 빨리 자바에서 그것을 시도, 나는이 메시지가 오류를 얻을 :

구문 오류가 근처에 'X') ' 1 호선에서

x는 txtobservation.getText()의 결과입니다.

또한 견적 문제가 아니므로 텍스트 일 ​​때 ''를 사용하고 정수 일 때는 사용하지 않습니다.

도움 주셔서 감사합니다.

+1

에이 것을 실행중인 정확한 쿼리를 인쇄하는데 유용합니다. – shiladitya

+0

매개 변수 사용 방법을 배우십시오. 그런 다음 문자열을 무시하면 구문 문제가 발생하는 이러한 문제가 발생하지 않습니다. –

+0

@shiladitya 나는 진술이 너무 길기를 원하지 않았다. 그래서 나는 테이블의 이름을 바꾸었다. 어쨌든, 나는 그것을 편집했습니다, 여기에 정확한 쿼리가 있습니다. – Adel

답변

1

당신은 PreparedStatement로 대신 문자열을 반환하고 int로하지 txtage.getText() 당신이 int로 변환해야 INT 인 경우 뜨지
gettext에 더 안전하고 더 도움이

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 
} 

주입니다 사용해야합니다 당신은 사용할 수 있습니다

Integer.parseInt(txtage.getText());//get int value form a String 
Float.parseFloat(txtpoids.getText());//get float value from a String