problem with my method java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manjava
    New Member
    • Sep 2009
    • 132

    problem with my method java

    I have a problem in my method I know how to continue to find the shortest distance and then extract cinques the nearest distance method must return cinques longitudes and latitudes with their cities to see my method that I have not yet Finally some things can give gaps
    Code:
    public static String getMagasinProche(double lat,double lon){
    	double latitude;
            double longitude;
            String v;
            String chaine="";
            double madistance;
            ArrayList  distances=new ArrayList();
            if(Magasins.c==null) etablirConnexion();
    		try{
                Statement s=c.createStatement();
                java.sql.ResultSet curseur=s.executeQuery("select latitude,longitude,nomville from liste_magasins");
                ResultSetMetaData rsmd=curseur.getMetaData();
                int nCol=rsmd.getColumnCount();
                    while(curseur.next()){
                latitude=curseur.getDouble("latitude");
                longitude=curseur.getDouble("longitude");
                v=curseur.getString("nomville");
                double latr=Math.toRadians(lat);
                double lonr=Math.toRadians(lon);
                double latituder=Math.toRadians(latitude);
                double longituder=Math.toRadians(longitude);
                double substrire=longituder-lonr;
                madistance=6378*Math.acos(Math.cos(latr)*Math.cos(latituder)*Math.cos(substrire)+Math.sin(latr)*Math.sin(latituder));
                distances.add(madistance);
                Object obj=Collections.min(distances);
                
                
                 }}catch(Exception e){System.out.println("erreur selection"+e);
    	}return ;
    thank u
    Last edited by Frinavale; Nov 16 '09, 09:21 PM. Reason: Please post code in [code] ... [/code] tags. Change quote tags into code tags.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Your code "Object obj=Collections .min(distances) ;" should be outside the while-loop at the end. "obj" (a Double, an inboxed double) contains the desired result, just print it out.

    By the way:
    - what is "Cinques" ? And what can give gaps? Your text is very hard to understand! Please make shorter sentences and look up english words.
    - Your code is very hard to understand. Please use english only! Use long and meaningful variable names only!
    - Please use code tags around your code when posting.

    If you follow these rules, you will get much better and faster help!

    Comment

    • manjava
      New Member
      • Sep 2009
      • 132

      #3
      thank you,

      cinques :five number in fransh


      at first:

      - i want to find the minumum five distance in the Arraylist

      in the second :

      -i want to find the "longitude" and "latitude" of these distances(Minim um distances)

      because my function so return the values of the "logitude" and "latitude"
      Code:
      public static String getMagasinProche(double lat,double lon){
      double latitude;
      double longitude;
      String v;
      String chaine="";
      double madistance;
      ArrayList distances=new ArrayList();
      if(Magasins.c==null) etablirConnexion();
      try{
      Statement s=c.createStatement();
      java.sql.ResultSet curseur=s.executeQuery("select latitude,longitude,nomville from liste_magasins");
      ResultSetMetaData rsmd=curseur.getMetaData();
      int nCol=rsmd.getColumnCount();
      while(curseur.next()){
      latitude=curseur.getDouble("latitude");
      longitude=curseur.getDouble("longitude");
      v=curseur.getString("nomville");
      double latr=Math.toRadians(lat);
      double lonr=Math.toRadians(lon);
      double latituder=Math.toRadians(latitude);
      double longituder=Math.toRadians(longitude);
      double substrire=longituder-lonr;
      madistance=6378*Math.acos(Math.cos(latr)*Math.cos( latituder)*Math.cos(substrire)+Math.sin(latr)*Math .sin(latituder));
      distances.add(madistance);
      Object obj=Collections.min(distances);
      
      
      }}catch(Exception e){System.out.println("erreur selection"+e);
      }return ;
      thank you

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        You listed your code again, but the solution to your problem I gave before is not done. "Object obj=Collections .min(distances) ;" is still inside the while-loop, and "System.out.pri ntln(obj)" is still missing.

        Comment

        • manjava
          New Member
          • Sep 2009
          • 132

          #5
          Hello,

          i want to know how can augmente interval of the latitude and longitude
          for exemple my methode(lat, lon){

          requetesql=sele ct latitude,longit ude,nomville from liste_magasins where latitude,longit ude between lat+step and lon+step

          //and the return the five latitude and longitude

          please can you exploite this with code java

          }

          Comment

          • chaarmann
            Recognized Expert Contributor
            • Nov 2007
            • 785

            #6
            At program beginning, initialize the variables needed:
            int lat=10; int lon=20; int step=3; // or whatever values you want

            Then replace your "select"-statement inside your java code of the s.executeQuery( ) method with this one:

            "select latitude, longitude, nomville from liste_magasins where (latitude between"+ lat + " and " + lat + "+" + step + ") and (longitude between "+ lon + " and " + lon + "+" + step+ ")

            Later, if you are an expert, you can use Prepared Statements" instead to increase performance.

            Comment

            • manjava
              New Member
              • Sep 2009
              • 132

              #7
              yes for obtains juste the five longitude and latitude i use LIMIT 5

              Comment

              • manjava
                New Member
                • Sep 2009
                • 132

                #8
                Hello ,

                i do that :java.sql.Resul tSet curseur=s.execu teQuery("select latitude, longitude, nomville from liste_magasins where (latitude between"+ lat + " and " + lat + "+" + 10 + ") and (longitude between "+ lon + " and " + lon + "+"+10+ ") LIMIT 5");
                i return erreur :erreur selectioncom.my sql.jdbc.except ions.jdbc4.MySQ LSyntaxErrorExc eption: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'between49.8945 93 and 49.894593+10) and (longitude between 2.298256 and 2.298256+' at line 1

                please can you help me

                Comment

                • chaarmann
                  Recognized Expert Contributor
                  • Nov 2007
                  • 785

                  #9
                  There is a space missing in your SQL. Look in error message, the command and number are concatenated together: "between49.8945 93". It should be "between 49.894593". So you must correct your source code, correct is:
                  ... where (latitude between "+ lat + " and ...
                  (note the space at the end of "between")

                  Comment

                  • manjava
                    New Member
                    • Sep 2009
                    • 132

                    #10
                    thank you so i want you see my methode and tell me what can modified about it because i want exploite on web service
                    public static String ListeMagasinPro cheDe(double lat,double lon){
                    double latitude;
                    double longitude;
                    String chaine="";
                    String v;
                    if(Magasins.c== null) etablirConnexio n();
                    try{
                    Statement s=c.createState ment();
                    java.sql.Result Set curseur=s.execu teQuery("SELECT latitude,longit ude,nomville FROM liste_magasins WHERE (latitude BETWEEN "+lat+" and "+lat+"+10) AND (longitude BETWEEN "+lon+" and "+lon+"+ 10) LIMIT 5 ");
                    ResultSetMetaDa ta rsmd=curseur.ge tMetaData();
                    int nCol=rsmd.getCo lumnCount();
                    while(curseur.n ext()){
                    latitude=curseu r.getDouble("la titude");
                    longitude=curse ur.getDouble("l ongitude");
                    v=curseur.getSt ring("nomville" );
                    chaine+="Latitu de:"+latitude+ " Longitude :"+longitude +" Ville :"+v+"\n";
                    }
                    }catch(Exceptio n e){System.out.p rintln("erreur selection"+e);
                    }return chaine;
                    thank you

                    Comment

                    • chaarmann
                      Recognized Expert Contributor
                      • Nov 2007
                      • 785

                      #11
                      I'll give you some advice, please take it serious. Usually beginners laugh about it and ignore it, but then when they went through all the disadvantages and pains because of that and later become experts, they understand why it was so important. the advice is:
                      Use English names for your methods, variables and error messages!
                      There are too much arguments for that to list them all, but I will give you three of them that I think will be strongest in your case.
                      1.) You want to deploy as web service, that means make it publich to the world, so a method name like "ListeMagasinPr ocheDe" will not be understood by 90% of your users.
                      2.) Your error messages prints "erreur" to the system log. The error message you print there is a mixture of French and English! (because the exception "e" is printed in English). A system administrator usually scans the system log for messages containing "error", so he will not find your error, and nobody notices which is dangerous. Imagine million of angry user calls because your web page isn't working, and an Indian system administrator jumping around in circles to figure out the error!
                      3) It's a fact that a piece of source code in production will be read and modified by a least 10 different programmers during lifetime. All programmers understand English, but only a few French. If a programmer can't understand your code, he will most likely throw it away and rewrite it. So you wasted your lifetime and the lifetime of your successor.

                      If you want to improve the performance of your code, read about "prepared SQL statements" and use them.

                      Comment

                      Working...