SQLSyntaxErrorException:'DOUBLE'类型的列不能包含'CHAR'类型的值

时间:2015-02-20 12:55:30

标签: java

我在java中编写代码,例如

Double Alish = Double.valueOf(productAlishText.getText().toString());
Double Satish = Double.valueOf(productSatishText.getText().toString());
Query = "INSERT INTO MALLAR (CUSTOMERID,PRODUCTNAME,PRODUCTALISH,PRODUCTSATISH) "
                    + "VALUES ('" + getEkspeditorIndex() + "','" + productAdiText.getText() + "','" + Alish+ "','" + Satish+ "')";
stat.execute(Query);

但是此代码会出现此错误。

  

SQLSyntaxErrorException:类型为' DOUBLE'的列不能保持价值观   类型' CHAR'

1 个答案:

答案 0 :(得分:1)

您也引用了Double个值。删除引号。

Query = "INSERT INTO MALLAR (CUSTOMERID,PRODUCTNAME,PRODUCTALISH,PRODUCTSATISH) "
        + "VALUES ('" + getEkspeditorIndex() + "','" + productAdiText.getText()
        + "'," + Alish+ "," + Satish+ ")";

请注意,上面的''Alish周围没有引号Satish

但是,我建议你改用PreparedStatement。您不仅可以摆脱这种连接问题,还可以防止SQL注入攻击。

PreparedStatement pstmt = connection.prepareStatement("INSERT INTO MALLAR " 
    + "(CUSTOMERID, PRODUCTNAME, PRODUCTALISH, PRODUCTSATISH) VALUES (?, ?, ?, ?)");

pstmt.setString(1, getEkspeditorIndex());
pstmt.setString(2, productAdiText.getText());
pstmt.setDouble(3, Alish);
pstmt.setDouble(4, Satish);

pstmt.executeUpdate();