[英]send latitude, longitude to mysql by JDBC every 30 seconds
目前,我想使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.