Getting error while inserting in database : ERROR: syntax error at or near "$1"

Getting error while inserting in database : ERROR: syntax error at or near "$1"

我在 java 中使用查询来插入 postgresql 数据库。

当我手动附加参数时,我正在使用的查询工作正常,但是当我通过准备好的语句提供参数时,它给出了错误:

错误:“$1”处或附近的语法错误

我正在使用以下代码:

String InsertInTi="INSERT INTO ssr_timeline_test("
                    +"ti_id, module_id, module_name, updated_date, tie_updates," 
                    +"additional_options, co_count, ca_type)"
                    +"(?,?,?,current_timestamp,?,?,?,?)";

            pres = con.prepareStatement(InsertInTi);
            pres.setInt(1,tId);
            pres.setString(2,moduleId);
            pres.setString(3,moduleName);
            pres.setString(4,ti.toJSONString());
            pres.setString(5,additionalOption.toJSONString());
            pres.setInt(6,coCount);
            pres.setString(7,caType);
            System.out.println("Query : "+pres );
            pres.execute();

任何人都可以提出相同的解决方案吗?

我还检查了传递给 parameters.These 的值的类型是:

TimInsert(int tId , String moduleId, String moduleName , JSONObject ti, JSONObject additionalOption , int coCount , String caType)

您的插入缺少 VALUES 子句 - 因此出现错误。

改为

 String InsertInTi="INSERT INTO ssr_timeline_test("
                +"ti_id, module_id, module_name, updated_date, tie_updates," 
                +"additional_options, co_count, ca_type)  values " //added here
                +"(?,?,?,current_timestamp,?,?,?,?)";

也许调用 executeUpdate() 而不是 execute()