Results 1 to 3 of 3

Thread: mysql and java

  1. #1
    Playagood is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

    Default mysql and java

    Hi,



    My mysql table ''Compagnie'' contains a company list. Each company is define this way: name, field, size, latitude, longitude, distance . I wants to be able to input my current location so java would classify the companies by the nearest.


    **The value "Distance" is not define. It will change depending of the current location.**

    Not looking for the code...I have been able to query, delete, select and update information from/to mysql through Wamp server(myphpadmin). Just need a clue.


    This is where I get stuck.

    -Input my current location
    -Query the latitude and longitude of each company.
    -Calculate the distance between my location and the companies location
    -Update the "distance" value for each company in mysql.

    It does calculate the distance but applies the result of the last company on the table to all the company...so when I come back to mysql, each company seems to be located at the same distance from my current location. Need some advices.

    Here is the code:


    import java.sql.*;
    import java.lang.Math;


    public class update2 {
    public static void main(String[] args) {
    System.out.println("Updates Records Example through Prepared Statement!");
    Connection con = null;
    Statement stmt = null;

    try{
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/company","root","");



    try{


    stmt = con.createStatement();

    String sql1 = "SELECT Longitude, Latitude FROM companyinfo" ;
    ResultSet rs = stmt.executeQuery(sql1);




    while(rs.next()){


    String sql = "UPDATE companyinfo SET Distance = (?) ";
    PreparedStatement prest = con.prepareStatement(sql);

    float currentlatitude= 50;
    float currentlongitude= 100;

    float Latitude = rs.getFloat("Latitude");
    float Longitude = rs.getFloat("Longitude");



    float currentdistance = (float) Math.sqrt(((currentlatitude-Latitude)*(currentlatitude-Latitude))+((currentlongitude-Longitude)*(currentlongitude-Longitude)));
    prest.setFloat(1, currentdistance);
    prest.executeUpdate();


    }



    rs.close();

    System.out.println(" Updating Successfully!");
    con.close();
    }
    catch (SQLException s){
    System.out.println("SQL statement is not executed!");
    }
    }
    catch (Exception e){
    e.printStackTrace();
    }

    }
    }



    Thank you. Merci vous.
    Last edited by Playagood; 11-09-2012 at 09:34 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: mysql and java

    What does your code look like?
    Specifically how you do the update and how that update code is called.
    Sounds (and this is a guess) like you're simply updating the entire table each time, rather than specific rows.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Playagood is offline Member
    Join Date
    Feb 2012
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Java DB, MySQL and MySQL Connector/J
    By jimihunter007 in forum JDBC
    Replies: 2
    Last Post: 07-30-2012, 05:23 AM
  2. Replies: 4
    Last Post: 03-04-2012, 11:26 PM
  3. Java and MySQL
    By mroushdy in forum New To Java
    Replies: 3
    Last Post: 12-08-2011, 10:59 PM
  4. Java and MySQL
    By Abder-Rahman in forum JDBC
    Replies: 6
    Last Post: 04-21-2009, 09:55 AM
  5. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 01:57 AM

Posting Permissions

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