SQL错误:条件表达式中的数据类型不匹配

时间:2014-03-16 01:44:27

标签: sql jsp javabeans

我试图只显示两个文本框指定范围之间的价格:

Java类:

 public Collection<Product> getRange(Double range1, Double range2){
    try {
        // re-use the getProductCollection method
        // even though we only expect to get a single Product Object

        String query = "Select * from Product where Price >= '" + range1 + "and Price <=" + range2 + "'";
        Collection<Product> c = getProductCollection( query );
        //Iterator<Product> i = c.iterator();
        //return i.next();
        return c;
    }
    catch(Exception e) {
        // unable to find the product matching that pid
        return null;
    }
}

我收到此错误消息:

Exception in getProductss(): java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

删除单引号并在and Price

之前添加空格
String query = "Select * from Product " + 
               "where Price >= " + range1 + " and Price <= " + range2

为避免将来出现此类错误,您应该使用更安全,更易于处理的准备语句