6 Replies Latest reply: Aug 8, 2011 11:17 AM by maheshguruswamy RSS

    Does If-statement work correctly in arrays retrieved by database ResultSet?

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

      {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++;
                }
           }
      }
      {code}

      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:

      {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++;
                }
           }
      }
      {code}

      why ??????
      please Help (I spent the last 6 hours trying to understand where is the error ..)
      Thank you in advance
      Matteo

      Edited by: 877978 on 8-ago-2011 7.53

      Edited by: 877978 on 8-ago-2011 7.54