Results 1 to 4 of 4
- 08-08-2011, 04:33 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
Strange behaviour of If statement inside an Array derived from ResultSet
First of all I say Hello to everyone; I'm new here !
My problem:
I can't understant why if-else-statemnt does not work inside an array derived from a database ResultSet.
I can connect to the database (Ms Access).
I can retrieve the data from the database and populate the arrays.
I can print all the record and every field is correct.
... but i cannot use if-else-condition for these data.
Suppose i have a simple table (MyTable) with just 3 String fields: "Town", "Country" and "isGeoLocalized".
Suppose i have only 4 records in my data base:
"Paris","France","ok"
"Berlin",Germany","no"
"Genoa","Italy","ok"
"Sao Paolo","Brazil","no"
i just want to print every record with town and country ..
and print "Geolocalized!" if isGeoLocalized is "ok" .. or "not GeoLocalized" if isGeoLocalized is "no"
It SHOULD be very very easy !! but i cant do !!
this is my code:
All records result "not GeoLocalized".Java Code:import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; public class MyTest { public static void main(String[] args) { String[] town = new String[4]; String[] country = new String[4]; String[] isGeoLocalized = new String[4]; final String Driver = "sun.jdbc.odbc.JdbcOdbcDriver"; final String Percorso = "C:\\"; final String MyDataBase = "TestDatabase.mdb"; final String Url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + Percorso + MyDataBase; Connection con; try{ Class.forName(Driver); con = DriverManager.getConnection(Url); Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs; rs = stmt.executeQuery("SELECT * FROM MyTable"); int counter = 0; while ( rs.next() ) { town[counter] = rs.getString("Town"); country[counter] = rs.getString("Country"); isGeoLocalized[counter] = rs.getString("isGeoLocalized"); counter++; } con.close(); } catch(ClassNotFoundException e){System.out.println(e.toString());} catch(SQLException e){System.out.println(e.toString());} int index = 0; while (index<4){ System.out.println("Index of Array = " + index); System.out.println(town[index]); System.out.println(country[index]); if (isGeoLocalized[index]=="ok"){ System.out.println("GeoLocalized!"); } else { System.out.println("not GeoLocalized"); } System.out.println("---------------------"); index++; } } }
isGeoLocalized[index] always result null if i call it inside if-statement.
But if I print it outside if-statement no problem occur.
I cant understand why !
If I use the same code with the same arrays populated manually (without a connection to the database and without using a ResultSet) it works !!!!
That is the code:
why ??????Java Code:public class MyTest2 { public static void main(String[] args) { String[] town = {"Paris","Berlin","Genoa","Sao Paolo"}; String[] country = {"France","Germany","Italy","Brazil"};// String[] isGeoLocalized = {"ok","no","ok","no"};// int index = 0; while (index<4){ System.out.println("Index of Array = " + index); System.out.println(town[index]); System.out.println(country[index]); if (isGeoLocalized[index]=="ok"){ System.out.println("GeoLocalized!"); } else { System.out.println("not GeoLocalized"); } System.out.println("---------------------"); index++; } } }
please Help (I spent the last 6 hours trying to understand where is the error ..)
Thank you in advance
Matteo
- 08-08-2011, 04:35 PM #2
You should use the equals method for comparing Strings. == is for primitives or for testing if two object references point to the same object.Java Code:isGeoLocalized[index]=="ok"
Last edited by Norm; 08-08-2011 at 05:03 PM. Reason: Keep in line with Tolls explanation
- 08-08-2011, 05:00 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
And just to add to what Norm's said, the reason the above works is that the compiler has a created a single String object for all three of those "ok" Strings, so the == works since they are the same object. When you are using the String returned from the result set that is a new String object with the string "ok" in it, but it is not the same object...so == returns false everytime.Java Code:String[] isGeoLocalized = {"ok","no","ok","no"};// ... if (isGeoLocalized[index]=="ok"){
- 08-08-2011, 05:07 PM #4
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Strange behaviour
By imadabh in forum Threads and SynchronizationReplies: 1Last Post: 05-11-2011, 03:31 PM -
Strange JVM behaviour
By pjpr in forum Advanced JavaReplies: 13Last Post: 01-03-2011, 07:39 PM -
Strange behaviour in serialization
By Wolverine in forum NetworkingReplies: 0Last Post: 05-23-2009, 12:03 PM -
AffinedTransform strange behaviour
By Echilon in forum AWT / SwingReplies: 3Last Post: 12-11-2008, 09:58 AM -
Strange behaviour in swing
By cbalu in forum AWT / SwingReplies: 1Last Post: 05-23-2008, 09:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks