File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Find duplicate value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Find duplicate value " Watch "Find duplicate value " New topic

Find duplicate value

Antony Amicone
Ranch Hand

Joined: Mar 11, 2006
Posts: 125
i need to know how can i compare a string value, with the result of a SELECT

i want to add a data just if that data is not present in the database.
Ex. I add the name of a book just if that book is not already in the field BOOKSNAME of my db
Skanda Raman
Ranch Hand

Joined: Mar 21, 2008
Posts: 205

just query the database and populate the resultset to get the value rs.getString("BOOKSNAME")

now check the condition

if(!yourbook.equals(rs.getString("BOOKSNAME")))// here rs is ResultSet obj
Add book
Dont add
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
You could check if the select statement returns an empty resultset, if it's empty you perform the add operation, otherwise you don't

for example, you could do something like that: ( let's state that your string is "mybooktitlestring" )

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33108

Alternatively, you could use the Merge statement to do it one trip to the database.

I think you are required to have an operation both if a match is found or not. But you could always set a value to itself in the matched case.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Antony Amicone
Ranch Hand

Joined: Mar 11, 2006
Posts: 125
done ty
I agree. Here's the link:
subject: Find duplicate value
It's not a secret anymore!