473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

problem with my method java

132 100+
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
Expand|Select|Wrap|Line Numbers
  1. public static String getMagasinProche(double lat,double lon){
  2.     double latitude;
  3.         double longitude;
  4.         String v;
  5.         String chaine="";
  6.         double madistance;
  7.         ArrayList  distances=new ArrayList();
  8.         if(Magasins.c==null) etablirConnexion();
  9.         try{
  10.             Statement s=c.createStatement();
  11.             java.sql.ResultSet curseur=s.executeQuery("select latitude,longitude,nomville from liste_magasins");
  12.             ResultSetMetaData rsmd=curseur.getMetaData();
  13.             int nCol=rsmd.getColumnCount();
  14.                 while(curseur.next()){
  15.             latitude=curseur.getDouble("latitude");
  16.             longitude=curseur.getDouble("longitude");
  17.             v=curseur.getString("nomville");
  18.             double latr=Math.toRadians(lat);
  19.             double lonr=Math.toRadians(lon);
  20.             double latituder=Math.toRadians(latitude);
  21.             double longituder=Math.toRadians(longitude);
  22.             double substrire=longituder-lonr;
  23.             madistance=6378*Math.acos(Math.cos(latr)*Math.cos(latituder)*Math.cos(substrire)+Math.sin(latr)*Math.sin(latituder));
  24.             distances.add(madistance);
  25.             Object obj=Collections.min(distances);
  26.  
  27.  
  28.              }}catch(Exception e){System.out.println("erreur selection"+e);
  29.     }return ;
  30.  
thank u
Nov 14 '09 #1
10 2312
chaarmann
785 Expert 512MB
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!
Nov 16 '09 #2
manjava
132 100+
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(Minimum distances)

because my function so return the values of the "logitude" and "latitude"
Expand|Select|Wrap|Line Numbers
  1. public static String getMagasinProche(double lat,double lon){
  2. double latitude;
  3. double longitude;
  4. String v;
  5. String chaine="";
  6. double madistance;
  7. ArrayList distances=new ArrayList();
  8. if(Magasins.c==null) etablirConnexion();
  9. try{
  10. Statement s=c.createStatement();
  11. java.sql.ResultSet curseur=s.executeQuery("select latitude,longitude,nomville from liste_magasins");
  12. ResultSetMetaData rsmd=curseur.getMetaData();
  13. int nCol=rsmd.getColumnCount();
  14. while(curseur.next()){
  15. latitude=curseur.getDouble("latitude");
  16. longitude=curseur.getDouble("longitude");
  17. v=curseur.getString("nomville");
  18. double latr=Math.toRadians(lat);
  19. double lonr=Math.toRadians(lon);
  20. double latituder=Math.toRadians(latitude);
  21. double longituder=Math.toRadians(longitude);
  22. double substrire=longituder-lonr;
  23. madistance=6378*Math.acos(Math.cos(latr)*Math.cos( latituder)*Math.cos(substrire)+Math.sin(latr)*Math .sin(latituder));
  24. distances.add(madistance);
  25. Object obj=Collections.min(distances);
  26.  
  27.  
  28. }}catch(Exception e){System.out.println("erreur selection"+e);
  29. }return ;
thank you
Nov 16 '09 #3
chaarmann
785 Expert 512MB
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.println(obj)" is still missing.
Nov 17 '09 #4
manjava
132 100+
Hello,

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

requetesql=select latitude,longitude,nomville from liste_magasins where latitude,longitude between lat+step and lon+step

//and the return the five latitude and longitude

please can you exploite this with code java

}
Nov 17 '09 #5
chaarmann
785 Expert 512MB
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.
Nov 17 '09 #6
manjava
132 100+
yes for obtains juste the five longitude and latitude i use LIMIT 5
Nov 17 '09 #7
manjava
132 100+
Hello ,

i do that :java.sql.ResultSet curseur=s.executeQuery("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.mysql.jdbc.exceptions.jdbc4.MySQLSynt axErrorException: 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.894593 and 49.894593+10) and (longitude between 2.298256 and 2.298256+' at line 1

please can you help me
Nov 17 '09 #8
chaarmann
785 Expert 512MB
There is a space missing in your SQL. Look in error message, the command and number are concatenated together: "between49.894593". 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")
Nov 18 '09 #9
manjava
132 100+
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 ListeMagasinProcheDe(double lat,double lon){
double latitude;
double longitude;
String chaine="";
String v;
if(Magasins.c==null) etablirConnexion();
try{
Statement s=c.createStatement();
java.sql.ResultSet curseur=s.executeQuery("SELECT latitude,longitude,nomville FROM liste_magasins WHERE (latitude BETWEEN "+lat+" and "+lat+"+10) AND (longitude BETWEEN "+lon+" and "+lon+"+ 10) LIMIT 5 ");
ResultSetMetaData rsmd=curseur.getMetaData();
int nCol=rsmd.getColumnCount();
while(curseur.next()){
latitude=curseur.getDouble("latitude");
longitude=curseur.getDouble("longitude");
v=curseur.getString("nomville");
chaine+="Latitude:"+latitude+ " Longitude :"+longitude+" Ville :"+v+"\n";
}
}catch(Exception e){System.out.println("erreur selection"+e);
}return chaine;
thank you
Nov 18 '09 #10
chaarmann
785 Expert 512MB
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 "ListeMagasinProcheDe" 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.
Nov 19 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: mailkhurana | last post by:
Hii , I am trying to use a type 2 driver to connect to DB2 0n AIX 5 I have a small java test to class to establish a conneciton with the db .. I am NOT using WAS or any appserver When I try to...
4
by: yaron | last post by:
Hi, I have a problem when sending data over TCP socket from c# client to java server. the connection established ok, but i can't send data from c# client to java server. it's work ok with...
6
by: AZRebelCowgirl73 | last post by:
Here is my problem: I have two java files: One named Car.java and the other named CarDealerApp.java: In the CarDealerApp program, I read in through user input the make, model, year and price of...
1
by: amgupta8 | last post by:
Note: This problem occurred when I updated the JDK from 1.3.1 to 1.4.1 or 1.4.2. Nothing else was changed in the code, other than updating the JDK on the database server (dbm cfg parm jdk_path) and...
3
by: mayur1 | last post by:
Hello everybody, I am trying to connect to DB2 V 8.1 on Red Hat Linux V5 from WebSphere Application Server V6 on Windows Server 2003. The problem that i am facing is that its not able to opent...
0
by: shaileshkumar | last post by:
hi, iam unable to run my applications in netbeans4.1. problem may be due to JVM or port numbers conflict. i did not intentionally change any port numbers . please guide me accordingly. iam...
0
by: shahiz | last post by:
This the error i get when i try to run my program Error: Unable to realize com.sun.media.amovie.AMController@18b81e3 Basically i have a mediapanel class that initialize and play the media as...
5
by: xirowei | last post by:
public class Result { private int countA = 0; private int countB = 0; private int statement; private boolean statusA = false; private boolean statusB = false; private int arrayA = new...
3
dmjpro
by: dmjpro | last post by:
Have a look at my problem. try{ //String tokenLocatorUrl = "http://erp.iitkgp.ernet.in:80/SSO_Token_Object_locator/resources/getSSOToken"; String tokenLocatorUrl...
2
by: slapsh0t11 | last post by:
So, I've been working on this Game of Life (http://www.bitstorm.org/gameoflife/) project, and all the code has been written. However, it will not run. First, I will post the error message and the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.