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 using 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
Bookmark "doubt using resultsets and queries in servlets" Watch "doubt using resultsets and queries in servlets" New topic

doubt using resultsets and queries in servlets

Yell Srik
Ranch Hand

Joined: Mar 10, 2004
Posts: 61

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???

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??
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

The JDBC forum would be the perfect place to ask such a question.
If you ask them, you might want to tell them what Database you're using.
Some have transaction support, stored procedures, some don't. The answer you get will depend on these things.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Mike Nightsky
Ranch Hand

Joined: Aug 18, 2001
Posts: 48
Although it do not belong to here, because its an SQL-problem.
I think you could not accomplish the task in one select/query.
It will be a row of selects/querys.

Win the opportunity to make money on the Internet<br /><a href="" target="_blank" rel="nofollow"></a>
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

I am going to move this to the JDBC forum.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1141

Method "executeUpdate()", in interface "java.sql.Statement", returns the number of rows affected. Hence, if you try to update a non-existent row, method "executeUpdate()" will return 0 (zero). In that case, you can then safely insert a new row, otherwise you aree done! [Just a suggestion.]

Good Luck,
I agree. Here's the link:
subject: doubt using resultsets and queries in servlets
jQuery in Action, 3rd edition