每30秒通过JDBC向mysql发送纬度,经度

时间:2015-05-25 04:21:18

标签: mysql jdbc gps latitude-longitude

目前我想通过使用JDBC将我的GPS纬度和经度发送到MYSQL。 要检索GPS lat,我已经使用了下面的代码,我在GPSTracker类中有GPS跟踪器代码。只要按下按钮,这就可以提供纬度和经度。

public class MainActivity extends Activity {
    Button btnShowLocation;
    GPSTracker gps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //testDB();

        btnShowLocation = (Button) findViewById(R.id.show_location);
        btnShowLocation.setOnClickListener(new View.OnClickListener() {
            //    txtv = (TextView) findViewById(R.id.txtv);

            //   btnShowLocation.setOnClickListener(this);
            // txtv.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                gps = new GPSTracker(MainActivity.this);
                //     txtv = getText().getApplicationContext(this);
                if (gps.canGetLocation()) {
                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();
                    Toast.makeText(getApplicationContext(), "Your Location is: \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                } else {
                    gps.showSettingsAlert();
                }
            }
        });
    }
}

要将JDBC数据发送到MYSQL,我使用下面的代码只能发送我在括号中给它的值:

public class MainActivity extends Activity {

    static final String USER = "root";
    static final String PASS = "root";
    GPSTracker gps;
    double tmplat = 0;
    double tmplong = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appDB();
    }

    public void appDB() {
        //   TextView tv = (TextView) this.findViewById(R.id.txtv);
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                //connection to data base.
                Connection con = DriverManager.getConnection("jdbc:mysql://192.168.1.6:3306/k_sql1", USER, PASS);

                //create a statement
             //   String result = "Database connection successfull !\n";
                Statement statement = con.createStatement();

                // execute sql query
                String sql = ("INSERT INTO `gps-data2`(`ID`,`Latitude`,`Longitude`) VALUES (1,123.45678, 345.678901);");

                // String sql = (" CREATE TABLE IF NOT EXISTS GPS_data ( ID int, Latitude Double, Longitude Double ); INSERT INTO GPS_data (`ID`,`Latitude`,`Longitude`) VALUES (1,1234.5678,56789.123456); ");

                statement.executeUpdate(sql);

              //  System.out.println("Inserted records into the table...");

            } catch (SQLException se) {
                //Handle errors for JDBC
                se.printStackTrace();
            } catch (Exception e) {
                //Handle errors for Class.forName
                e.printStackTrace();
            }
        }
    }
enter code here

请告诉我如何组合这两个系统并使用JDBC方法将GPS数据(lat,lng)发送到mysql。我知道使用PHP可能更好,但对于这个项目,我想通过使用JDBC。感谢能否给我一个简单的适用解决方案。

1 个答案:

答案 0 :(得分:0)

我正在寻找以编程方式检索GPS参数并使用onClickListener直接将其发送到数据库的答案。     static final String USER =" root&#34 ;;     static final String PASS =" root&#34 ;;     String sql = null;     GPSTracker gps;     double tmplat = 0;     double tmplong = 0;     按钮btnShowLocation;     双纬度;     双重经度;     TextView tv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     btnShowLocation = (Button) findViewById(R.id.show_location);
     btnShowLocation.setOnClickListener(new View.OnClickListener() {
        @Override
         public void onClick(View v) {
            gps = new GPSTracker(MainActivity.this);
            if (gps.canGetLocation()) {
                latitude = gps.getLatitude();
                longitude = gps.getLongitude();
                Toast.makeText(getApplicationContext(), "Your Location is: \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                appDB();
            } else {
                gps.showSettingsAlert();
            }
        }
        });
}
protected void appDB() {

    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/k_sql1", USER, PASS);

        String result = "Database connection successfull !\n";
        Statement statement = con.createStatement();

        String sql = ("INSERT INTO `gps-data2`(`Latitude`,`Longitude`) VALUES (" + latitude + ", " + longitude + ");");

        statement.executeUpdate(sql);


    } catch (SQLException se) {

        se.printStackTrace();
    } catch (Exception e) {
        //Handle errors for Class.forName
        e.printStackTrace();
    }
}

}            ˚F