0

I am trying to run a SQL query using a Java method for my Selenium project. The below method executeQuery gets sqlQuery as argument, which I plan to use for INSERT, UPDATE and DELETE.

For DELETE the method works fine, but for INSERT and UPDATE it's giving me issue.

My insert query:

INSERT INTO NSIDE.DOCUMENT_REC (DOCUMENT_ID,RECIPIENT_CODE,DELIVERY_METHOD_CODE,PRINT_METHOD_CODE,TRANSACTION_EVENT_SEQ_NUMB,UPDATED_BY_ID) VALUES('ABCD000011','PC','R','F',1,1);

My update query:

update NSIDE.job_pmar set commit_freq_numb='96',exception_threshold_numb='28',file_name='RCUSOCKEWB',server_path_name='SW9L5CNLMU' where JOB_CODE='ABC0198';

Java Program for query execution:

public void executeQuery(String sqlQuery) { 
    String myUserName = ReadPropertyFile.getInstance().getPropertyValue("DatabaseUserName");
    String myPassword = ReadPropertyFile.getInstance().getPropertyValue("DatabasePassword");
    String dbURL = ReadPropertyFile.getInstance().getPropertyValue("DatabaseURL");
    String username = myUserName;
    String password = myPassword;
    String selectquery = sqlQuery;
    log.debug("Preparing to execute DML Query : "+sqlQuery);
    try (Connection con = DriverManager.getConnection(dbURL,username,password);
         Statement st=con.createStatement()) {
        st.executeUpdate(selectquery);
        /* Load DB2 JDBC Driver */
        Class.forName("com.ibm.db2.jcc.DB2Driver");
        log.info("Successfully executed the DML Query :"+sqlQuery);
    }
    catch (Exception e) {
        log.error(e);
    }
}

When I run, it's giving me below error for update query:

com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=END-OF-STATEMENT;PMAR;JOIN <joined_table>, DRIVER=4.25.13

NOTE: When I run the query individually using Squirrel SQL Client, it is able to run and update successfully. But, when I run through java code, it fails.

Kindly provide your views on why the update is not working for me.

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
Sangeetha
  • 31
  • 2
  • 8
  • You may need to provide stack trace from Java containing more details about the place where this error occurs. Possibly, you have some overridden method in your code. Aside comment: why do you reload JDBC driver each time after calling `executeUpdate`? – Nowhere Man Jun 11 '20 at 06:38
  • Which error do you get for INSERT query, is it the same as for UPDATE? – Nowhere Man Jun 11 '20 at 06:49
  • @AlexRudenko Yes the error for insert query is same as update – Sangeetha Jun 11 '20 at 08:10
  • The full error message is: `An unexpected token "END-OF-STATEMENT" was found following "PMAR". Expected tokens may include: "JOIN "`. Show the **exact** message printed out to your log from `log.debug("Preparing to execute DML Query : "+sqlQuery);` – Mark Barinstein Jun 11 '20 at 08:23
  • Hi @MarkBarinstein The full error message that was printed out to my log is same as what i mentioned. No other changes. – Sangeetha Jun 11 '20 at 08:46
  • I don't need your error message. `PMAR` word is mentioned there, but I don't see such a distinct word (there is `job_pmar`, but not `pmar`) in the query text you provided. This is why I suspect, that your java code tries to execute some different statement, and I need the output of `log.debug("Preparing to execute DML Query : "+sqlQuery)` in your log. – Mark Barinstein Jun 11 '20 at 09:26
  • @MarkBarinstein When i checked with debug mode, i found out that im calling the wrong method name in my java class to execute the query. Sorry, it was my mistake. Both method names are little similar. Thanks for your time. – Sangeetha Jun 11 '20 at 09:48

1 Answers1

0

When i checked with debug mode, i found out that im calling the wrong method name in my java class to execute the query. Sorry, it was my mistake. (i.e.,As Both method names are little similar.)

Sangeetha
  • 31
  • 2
  • 8