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??
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.
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.
subject: doubt using resultsets and queries in servlets