1

Hi I want to do something like that:

StringTokenizer tokenizer = new StringTokenizer(getCompletedAnswers, "$");
                while(tokenizer.hasMoreTokens()){

                    System.out.println(tokenizer.nextToken());
                    rs = st.executeQuery("INSERT INTO "+db+".answersTable(`answer`) VALUES ('"+tokenizer.nextToken()+"')");

                }

But it gives me an error Can not issue data manipulation statements with executeQuery().

As I read in documentation that I have to use executeUpdate() I'm doing it:

rs = st.executeUpdate("INSERT INTO "+db+".answersTable(`answer`) VALUES ('"+tokenizer.nextToken()+"')");

and it tells me: Type mismatch: cannot convert from int to ResultSet

Any idea how to use StringTokenizer to put those tokens into database ? thank you

1 Answer 1

2

You should use Statement.executeUpdate instead

The executeUpdate method returns an int:

Returns: either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing

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

5 Comments

ok it works but it only adds one token to database, if I have 2 tokens abc1 and abc2 read from tokenizer.nextToken() it adds only abc2, any idea why?
The reason is that you cannot reuse a Statement (st). So initialize st inside the while loop instead of outside the while loop.
is what I did and still no result, same like it was before. I have used Statement st = null; and then st.executeUpdate("INSERT INTO "+db+".answersTable(answer) VALUES ('"+tokenizer.nextToken()+"')"); inside the while loop
ok problem has been solved, needed to assign tokenizer.nextToken() to string String str = tokenizer.nextToken();
also my bad, the sql statement can actually be reused

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.