SQL SELECT result

try{

// Create a result set containing all data from my_table

Statement s = connection.createStatement();

ResultSet rs = s.executeQuery("SELECT * FROM station WHERE stationID = '" + stationID +"'");

}catch (SQLException e){

}

If there are no rows for that SQL statement, what will it return back to me? On my server, a client is sending a packet to me that contains the stationID. First, I want to make sure stationID already exists in the database. If it doesn't exist, I would like to run more SQL statements, but first I need to know what it will return to me.

In guessing code, this is what I'm looking for:try{

// Create a result set containing all data from my_table

Statement s = connection.createStatement();

ResultSet rs = s.executeQuery("SELECT * FROM station WHERE stationID = '" + stationID +"'");

if (rs == NULL)// if no rows were selected

{

// if there were no rows selected from the original SELECT statement, INSERT a new row using this query

ResultSet rs = s.executeQuery("INSERT INTO station VALUES ('stationID', 'address', 'city', 'state', zip'");

}

}catch (SQLException e){

}

Any help?

Message was edited by:

tristanlee85

[1969 byte] By [tristanlee85a] at [2007-11-27 1:56:24]
# 1
Hi, Check ResultSet.first function (return false if there are no rows in result set). Note: It's always safe to compare the result of a function to null, it can avoir NullPointerException.Hope that help, Jack
jack@square6a at 2007-7-12 1:30:43 > top of Java-index,Java Essentials,Java Programming...
# 2

Why would you guess when you can just look at the API documentation? (Which specifically says the executeQuery() method never returns null.)

If the SQL statement returns zero rows, you get a ResultSet that contains zero rows. Does that seem peculiar to you? In that case calling "rs.next()" returns false because there are zero rows.

DrClapa at 2007-7-12 1:30:43 > top of Java-index,Java Essentials,Java Programming...
# 3
By the way, the keyword null in Java is not block letter NULL.
rym82a at 2007-7-12 1:30:43 > top of Java-index,Java Essentials,Java Programming...