目前,我想使用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数据(纬度,经度)发送到mysql。我知道使用PHP可能更好,但是对于此项目,我希望使用JDBC。赞赏是否能给我一个简单的适用解决方案。

最佳答案

这就是我寻找以编程方式检索GPS参数并将其直接通过onClickListener发送到数据库的答案。
    静态最终字符串USER =“ root”;
    静态最终字符串PASS =“ root”;
    字符串sql = null;
    GPSTracker GPS;
    双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

关于mysql - 每30秒通过JDBC发送纬度,经度给mysql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30431186/

10-13 07:17