2014-07-08 2 views
-2

나는이 코드를 가지고 있으며 문에 삽입 할 때 구문 오류가 발생한다고 말합니다.insert sql에 대한 내 코드의 문제점은 무엇입니까?

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
    Date date = new Date(); 
    String tempo = dateFormat.format(date); 
    Global.temp1 = ""+Global.grandTotal; 

Global.s.executeUpdate(" INSERT INTO salesRecord(date,total,representative)" + 
         " VALUES('"+ tempo +"'," + 
           "'"+ Global.temp1 +"'," + 
           "'"+ Global.username.getText() +"')"); 

Global.temp1은 항목의 합계 값을 가져 와서 문자열로 변환하는 문자열입니다. 이걸로 나를 도울 수 있니?

+3

좋은 주인에 문제가 있습니다. 준비된 진술을 사용하십시오. –

+2

'PreparedStatement','static' 참조를 사용하지 않고 데이터베이스 내에서 'Date' (또는'TimeStamp')로 표현되어야 할 때'Date'를 포맷하려고합니다 ... – MadProgrammer

+0

System.out에 성명서? 대부분의 vars 값에는 삽입 문자가 유효하지 않은 문자가 포함되어 있습니다. 게다가 준비가 부족한 문장. –

답변

2

이것은 삽입 된 쿼리를 사용하는 나쁜 방법입니다. 사용하는 것이 더 좋습니다 PreparedStatement

시도해 볼 수 있습니다. 코드에서

Connection conn = null; 
    PreparedStatement preparedStatement = null; 
    try{ 
     conn=DriverManager.getConnection("");// get connection 
     preparedStatement=con.prepareStatement("INSERT INTO"+ 
             "alesRecord(date,total,representative)" + 
             " VALUES(?,?,?)"); 
     preparedStatement.setString(1,tempo); 
     preparedStatement.setString(2,Global.temp1); 
     preparedStatement.setString(3,Global.username.getText()); 
     preparedStatement.executeUpdate(); 
    }catch(Exception e){ 
     //log the exception 
    }finally{ 
    //close the resources. 
    } 

'

관련 문제