This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can i tell if a resultset is empty?

 
carlos flamenco
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic