插入数据库时​​出错:ERROR:语法错误在"$ 1"或附近

vai*_*hav 1 java postgresql jdbc prepared-statement java-6

我在java中使用查询在postgresql DB中插入.

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

错误:语法错误在"$ 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();
Run Code Online (Sandbox Code Playgroud)

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

我还检查了传递给参数的值的类型.这些是:

TimInsert(int tId , String moduleId, String moduleName , JSONObject ti, JSONObject additionalOption , int coCount , String caType)
Run Code Online (Sandbox Code Playgroud)

Jan*_*Jan 7

你的插入缺少一个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,?,?,?,?)";
Run Code Online (Sandbox Code Playgroud)

也许打电话executeUpdate()而不是execute()