Two Laptop Bag*
The moose likes JDBC and the fly likes can't use data from resultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "can Watch "can New topic
Author

can't use data from resultSet

jmd odhiambo
Greenhorn

Joined: Jul 15, 2008
Posts: 2
I am a newbie, i want to retrieve data from a resultSet and compare it to another string then make decision to do something. SOMEONE COME TO MY RESCUE. The code is as below

try {"com.mysql.jdbc.Driver"

con =java.sql.DriverManager.getConnection("jdbc:mysql://localhost/bus_service", "root", "");
st = con.createStatement();

String query = "SELECT seat_no, seat_state FROM bus_booking WHERE _to LIKE 'dd' AND date_of_travel LIKE 'ff' AND departing_time LIKE 'dd'" +
"ORDER BY seat_no ";
rs = st.executeQuery(query);
while (rs.next()) {
String s = rs.getString("seat_no");
String n = rs.getString("seat_state");
if(s == mike )// THIS IF STATEMENT STOPS MY CODE FROM FUNCTIONING
{
A1Button.setText(n);
}
}

st.close();
con.close();
} catch(java.sql.SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, yeah. That's one of the standard beginner mistakes, nothing to do with JDBC. If you use "==" to compare two objects, that compares to see if they are the same object. But it's possible for two different String objects to contain the same data, and that's what you want to compare, isn't it? You want to compare the contents of the two String objects. For that you use the equals() method, like this:
<blockquote>code:
<pre name="code" class="core">if (s.equals(mike)) ...</pre>
</blockquote>
jmd odhiambo
Greenhorn

Joined: Jul 15, 2008
Posts: 2
Thanks Paul it did work, cant believe it was that simple ha! at ma self....
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"javachizi odhiambo",
Please check your private messages.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: can't use data from resultSet