This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JDBC 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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "doubt using resultsets and queries in servlets" Watch "doubt using resultsets and queries in servlets" New topic
Author

doubt using resultsets and queries in servlets

Yell Srik
Ranch Hand

Joined: Mar 10, 2004
Posts: 61
Hi,

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
Sheriff

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="http://sweeps.sitesell.com/minirich.html" target="_blank" rel="nofollow">http://sweeps.sitesell.com/minirich.html</a>
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

I am going to move this to the JDBC forum.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1121

Yell,
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,
Avi.
 
Consider Paul's rocket mass heater.
 
subject: doubt using resultsets and queries in servlets
 
Similar Threads
Design question
Updateable ResultSet just for doing inserts: good idea?
Query two tables using hibernate
doubt in resultsets and queries in servlets
compare resultset values with string value