wood burning stoves
The moose likes JDBC and Relational Databases and the fly likes Determining null resultset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Determining null resultset" Watch "Determining null resultset" New topic

Determining null resultset

Steve Gebert

Joined: May 07, 2004
Posts: 17
Hi - I have my application working and I am now validating user input etc. This application takes the text output of a genetics test and inserts the rows into a Oracle DB. Part of this process requires that I check that a locus name is currently in the database and then look up some genetic information. I am trying to determine if the resultset is null because there was no record. Ie if the text value is "One_CytB26" and the value in the database is "One_CytB_26" then I would expect a null resultset. But how do I check it? Thanks for any help you can give.

private void LookupLocus(String strLocusName)
// this method does a db lookup for each locus to determine the appropriate allele values
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
String url = "jdbc racle:thin:@db-dbname";
Connection conn = DriverManager.getConnection(url,"usr","pwrd");
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("SELECT * FROM AKFINADM.LOCUS_LOOKUP WHERE LOCUS_NAME = '" + strLocusName + "'" );
if(rset == null)
JOptionPane.showMessageDialog(null,"There is a locus error.","SQL Error",JOptionPane.ERROR_MESSAGE);
strPloidy = rset.getString("PLOIDY");
strGenome1 = rset.getString("BOTH_GENOTYPE_1");
strGenome2 = rset.getString("BOTH_GENOTYPE_2");
catch(SQLException e)
JOptionPane.showMessageDialog(null,"There is a database error.","SQL Error",JOptionPane.ERROR_MESSAGE);
catch (Exception e)
[ May 21, 2004: Message edited by: Steve Gebert ]
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
The ResultSet will never be null, regardless of whether or not any records were found. A simple implementation of checking the number of records returned is to use an int counter. So, for instance:
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
If you only expect a single result back, you can use this syntax as well.

[ May 21, 2004: Message edited by: Wayne L Johnson ]
Steve Gebert

Joined: May 07, 2004
Posts: 17
That worked great! I guess I'm still a little used to VB. I appreciate your response, thank you.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the JDBC forum...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Srinivas Gogineni

Joined: May 06, 2004
Posts: 6
You can try using the wasNull method of the resultset

Thx and regards
I agree. Here's the link: http://aspose.com/file-tools
subject: Determining null resultset
It's not a secret anymore!