【问题标题】:send latitude, longitude to mysql by JDBC every 30 seconds每 30 秒通过 JDBC 向 mysql 发送纬度、经度
【发布时间】:2015-05-25 04:21:17
【问题描述】:

目前我想通过使用 JDBC 将我的 GPS 纬度和经度发送到 MYSQL。 为了检索 GPS 纬度,我使用了下面的代码,并且我在 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。不胜感激是否能给我一个简单适用的解决方案。

【问题讨论】:

  • 这是我正在寻找的答案,以编程方式检索 GPS 参数并使用 onClickListener 直接将其发送到数据库。
  • 请描述拓扑。我设想一个移动设备收集 GPS 坐标,然后将它们发送到将插入数据库的固定服务器。这是正确的还是错误的?
  • 如果您打算将 GPS 坐标存储在 mysql 等数据库中,那么您可以使用此方法。

标签: mysql jdbc gps latitude-longitude


【解决方案1】:

这就是我在寻找以编程方式检索 GPS 参数并使用 onClickListener 直接将其发送到数据库的答案。 静态最终字符串用户=“根”; 静态最终字符串 PASS = "root"; 字符串 sql = null; GPSTracker 全球定位系统; 双 tmplat = 0; 双 tmplong = 0; 按钮 btnShowLocation; 双纬度; 双经度; TextView 电视;

@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

【讨论】:

    猜你喜欢
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多