0

I am taking domains from a text file and passing it to a query one by one. for first time the query is executing fine .. but when it takes the second domain and passing it to query getting error "ORA-00933: SQL command not properly ended" Below is the code

sql.append("select person_org_id,profile_type_id as NEXUS, profile_option_id,profile_option_value  from TABLE1 ");
            sql.append(" where profile_type_id=1 and person_org_id in (select person_org_id from TABLE2 where ");
            sql.append(" account_id in (select account_id from TABLE3 where prod_id=10001 and prod_inst_name = ?)) ");

            ps = con.prepareStatement(sql.toString());
            System.out.println("----------checkpoint -----------");

            ps.setString(1,domain_name);
            System.out.println("----------checkpoint 4-----------");
            rs= ps.executeQuery();
            System.out.println("----------checkpoint 5-----------");

1 Answer 1

2

If you have this code in a loop, and you do not clear the StringBuilder or use a new one, then the second time around, you will have the SQL statement twice and that would explain the error.

Why use a StringBuilder at all if a simple String would do? There is no variation in the SQL statement at all. Of course, this may have been a simplified example.

Also, if you do this in a loop, and the SQL is indeed the exact same one every time, you could just prepare the statement once, and execute it repeatedly in the loop. That is kind of what prepared statements are for.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you so much.. I have placed the sql outside the loop .. and execute it repeatedly in the loop its working... thanks u soo much. But i am writing the data retrievied to an xls file and its writing twice... checking that one...
Hey data is writing correctly in xls file... once again thanks alot for your answer.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.