File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes How can i tell if a resultset is empty? 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 "How can i tell if a resultset is empty?" Watch "How can i tell if a resultset is empty?" New topic
Author

How can i tell if a resultset is empty?

carlos flamenco
Greenhorn

Joined: Feb 28, 2001
Posts: 7
Hi,
I want to check to see if a value is in a column, if it is i want to update that row for the column. If not ,
I want to insert a new row (will null be returned for the resultset if the value for the column is not found?)

Any help would be greatly appreciated!
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 565
Originally posted by carlos flamenco:
Hi,
I want to check to see if a value is in a column, if it is i want to update that row for the column. If not ,
I want to insert a new row (will null be returned for the resultset if the value for the column is not found?)

Any help would be greatly appreciated!


You can see the size of a rowset with .size()
Depending on the rowset properties, if you want to insert a row, you can too and then go to the insert_row and update the column and then insert the row. cant remember the exact lingo but it's all in the rowSet documentation under java.sql at
http://java.sun.com/j2se/1.3/docs/api/index.html


I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

This little code snippet might help:

Although I'm not sure what you are trying to accomplish, this code meets your specs.
ResultSet.wasNull()-->
"Reports whether the last column read had a value of SQL NULL. Note that you must first call one of the getXXX methods on a column to try to read its value and then call the method wasNull to see if the value read was SQL NULL.
Returns:
true if the last column value read was SQL NULL and false otherwise"
Good Luck,
Jamie
[This message has been edited by Jamie Robertson (edited November 07, 2001).]
Anusaya Sehgal
Greenhorn

Joined: Nov 09, 2001
Posts: 2
Originally posted by carlos flamenco:
Hi,
I want to check to see if a value is in a column, if it is i want to update that row for the column. If not ,
I want to insert a new row (will null be returned for the resultset if the value for the column is not found?)

Any help would be greatly appreciated!

rs=st.executeQuery("select A,B,C from table");
int y=0;
while(rs.next())
{
if(rs.getString("B").equals("IC"))
{
String ss="Update table set A='Yes' where B='IC'";
int x=st.executeUpdate(ss);
System.out.print("Record Updated");
y++;
break;
}
}

if(y==0)
{
String ss="insert into table(A,B,C) values('IC','yes','bray')";
rs=st.executeQuery(ss);
System.out.print("Record Inserted");
}

------------------
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

I think that I didn't interpret your request properly:
Algorithm: Search the database for a specific row. If it exists, then update it, if it does not exist, create a new row
if you are expecting a maximum of 1 row:

If you are expecting(and updating more than one row:
if you are expecting a maximum of 1 row:

the difference between this procedure and the previous poster's(besides the code being in code tags)is that you don't want to return or process any rows that don't match(weed out the ones you don't need via the query instead of selecting all the records, iterating through each row and checking for each row).
Jamie

[This message has been edited by Jamie Robertson (edited November 09, 2001).]
[This message has been edited by Jamie Robertson (edited November 09, 2001).]
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 565
OK, if you've got a connected, updatable rowset (CONCUR_UPDATABLE) then you can do this without executing any seperate UPDATE sql:

(from the docs)
(why do i like databases so much? i should have been a dba)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can i tell if a resultset is empty?
 
Similar Threads
How to replace the value in a row with previous row value if it is null or 0.
Hiding group of rows depending on dropdown list
Sort a ResultSet based on data in one of the ResultSet Columns
How to sort a ResultSet
How To compare two excel sheets using java