user2380447
user2380447

Reputation: 11

Working with latitude/longitude values in Java/MySQL

I have a database table containing location latitude and longitude. I am given following values latitude, longitude, minX, minY, maxX, maxY. I need to return all the records from database table whose lat, long fall within the range.

Upvotes: 0

Views: 378

Answers (2)

drew moore
drew moore

Reputation: 32670

You'll want to use a PreparedStatement anytime you need to make a complex SQL query such as this. Here's some pseduocode to give you an idea of what you'll be doing:

String query = "SELECT * from `DBTableName` WHERE latitude >= ? AND latitude <= ? AND     
longitude >= ? AND longitude <= ?`";

PreparedStatement ps = connection.PrepareStatement(query);
ps.setLong(1, minX);
ps.setLong(2, maxX);
ps.setLong(3, minY);
ps.setLong(4, maxY); 
ResultSet rs = ps.executeQuery();

The list of records satisfying your query will then be accessible via the ResultSet

Upvotes: 1

jtight_404
jtight_404

Reputation: 51

I hope I got the question.

Try

SELECT * FROM locationTable
WHERE (latitude >= minX AND latitude =< maxX)
AND ( longitude >= minY AND longitude =< maxY)

Upvotes: 0

Related Questions