0

I filled an array using .split(" ") on a string containing a paragraph of text scanned from a webpage. I would like to insert each element of the array into a table but only if said word does not already exist in the table. So each word should only exist in the table once.

I am using the following statement to check if the word exists in the table already:

SELECT * FROM table WHERE word = "**Word to check for**";

In my java program, how can I check if the statement returns an empty set or not? i.e. what would be the condition of my if statement? Would I check if the above SELECT query returns 0 rows?

Thanks,

2
  • 1
    Your query returns the whole row(s) and not a single integer. What you probably need is select count(*) .... Commented Apr 10, 2014 at 4:03
  • possible duplicate of Java ResultSet how to check if there are any results Commented Apr 10, 2014 at 4:04

1 Answer 1

1

If resultSet.next() returns true then the word is present in the table and no need to check the number of rows.

This way you will query the DB for two times. Rather consider using stored procedure.

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

2 Comments

Excellent, thank you very much sir! This is a much easier implementation than I was thinking of!
You are welcome. If you are doing this for a real time project then try to use stored procedure. Learning curve is there but it is much better than embedded query.

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.