vaibhav
vaibhav

Reputation: 784

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

I am using a query in the java for the insertion in a postgresql DB.

The query which I am using is working fine when I manually append the parameters but when I give the parameters through prepared statement it gives the error as :

ERROR: syntax error at or near "$1"

I am using the following code :

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();

Can anyone suggest the solution for the same ?

I also have checked the types of the values that are being passed in to the parameters.These are :

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

Upvotes: 1

Views: 538

Answers (1)

Jan
Jan

Reputation: 13858

Your insert lacks a VALUES clause - thus the error.

Change it to

 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,?,?,?,?)";

And maybe call executeUpdate() instead of execute()

Upvotes: 7

Related Questions