2014-07-08 89 views
-2

我有这些代码,它说插入到语句中的语法错误。我的代码插入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','静态'引用,尝试格式化数据库中'Date'时应该将其表示为'Date'(或'TimeStamp')... – MadProgrammer

+0

您是否尝试过System.out声明?很有可能您使用的变量值包含一些使您的插入无效的字符。当然,除了缺少准备好的陈述之外。 –

回答

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. 
    } 

在代码中有与'