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,

Reece Kenney
  • 2,734
  • 3
  • 24
  • 57
  • 1
    Your query returns the whole row(s) and not a single integer. What you probably need is `select count(*) ...`. – Bhesh Gurung Apr 10 '14 at 04:03
  • possible duplicate of [Java ResultSet how to check if there are any results](http://stackoverflow.com/questions/867194/java-resultset-how-to-check-if-there-are-any-results) – Jabir Apr 10 '14 at 04:04

1 Answers1

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.

pundit
  • 322
  • 1
  • 7
  • Excellent, thank you very much sir! This is a much easier implementation than I was thinking of! – Reece Kenney Apr 10 '14 at 04:16
  • 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. – pundit Apr 10 '14 at 04:34