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 doubt in resultsets and queries in servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Reply locked New topic

doubt in resultsets and queries in servlets

Yell Srik
Ranch Hand

Joined: Mar 10, 2004
Posts: 61

i am reposting this question that i initially posted in 'Servlets' forum..

i am writing a code that involves JDBC-ODBC in servlets. the place where i am currently stuck is:

i have a table named emp with 4 columns: name,title,city and country

now i need to write a query that first checks if the table is empty. if empty, new values are entered into the table. if the new data (entered by the user) already exists, the old values in the emp table should be overwritten by the new values.

how can this query be written???

{ i am using MS-Access as the database)

PS: the logic i have thought of (but finding it difficult to implememt) is that: first use the query "select * from emp" and assign the results to a ResultSet (say rs1). now we can check the contents of rs1. if empty, the insert statement can be written else an update statement is written.. will the above logic work and if yes, how to write such a code??
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
a better option would be to check if the table contains 'that specific employee record', if yes, then update 'that particular employee record', if not, then insert 'that particular employee record'.

going by your logic,

if the result set is not empty, then you will be trying an update, but look, the resultset has some rows, but that specific employee record which you want to update does not exist, so the update should fail.

that's just my opinion, lets see what others have to say.

Tualha Khan

SCJP2, BEA WLS 6.0, DB2 UDB 7.1
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

Please continue discussion in the original thread, which has since been moved to this forum. In the future, don't cross-post the same question. Just ask a bartender/sheriff to move it for you.

JDBC Forum Bartender

[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
I agree. Here's the link:
subject: doubt in resultsets and queries in servlets
jQuery in Action, 3rd edition