【发布时间】: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