Results 1 to 4 of 4
  1. #1
    matthelazy is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default 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:

    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++;
    		}
    	}
    }
    All records result "not GeoLocalized".
    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:

    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++;
    		}
    	}
    }
    why ??????
    please Help (I spent the last 6 hours trying to understand where is the error ..)
    Thank you in advance
    Matteo

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Java Code:
    isGeoLocalized[index]=="ok"
    You should use the equals method for comparing Strings. == is for primitives or for testing if two object references point to the same object.
    Last edited by Norm; 08-08-2011 at 06:03 PM. Reason: Keep in line with Tolls explanation

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Java Code:
    		String[] isGeoLocalized = {"ok","no","ok","no"};//
    ...			
    			if (isGeoLocalized[index]=="ok"){
    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.

  4. #4
    matthelazy is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Strange behaviour
    By imadabh in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-11-2011, 04:31 PM
  2. Strange JVM behaviour
    By pjpr in forum Advanced Java
    Replies: 13
    Last Post: 01-03-2011, 08:39 PM
  3. Strange behaviour in serialization
    By Wolverine in forum Networking
    Replies: 0
    Last Post: 05-23-2009, 01:03 PM
  4. AffinedTransform strange behaviour
    By Echilon in forum AWT / Swing
    Replies: 3
    Last Post: 12-11-2008, 10:58 AM
  5. Strange behaviour in swing
    By cbalu in forum AWT / Swing
    Replies: 1
    Last Post: 05-23-2008, 10:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •