Guest User

Untitled

a guest
Nov 17th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. package ru.spbau.farutin_solikov.gpstracker;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5.  
  6. import java.sql.PreparedStatement;
  7. import java.util.ArrayList;
  8.  
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14.  
  15. public class Controller {
  16.  
  17. private static final String url = "jdbc:mysql://146.185.144.144:3306/gps?autoReconnect=true&useSSL=false";
  18. private static final String user = "android";
  19. private static final String password = "GPSTracker-MySQL123";
  20.  
  21. private static Connection con;
  22. private static Statement stmt = null;
  23. private static PreparedStatement preparedStatement = null;
  24. private static ResultSet rs;
  25.  
  26. public static void startCoordinatesService(Context context) {
  27. CoordinatesService.enqueueWork(context, new Intent());
  28. }
  29.  
  30. public static void stopCoordinatesService() {
  31. CoordinatesService.stop();
  32. }
  33.  
  34. public static ArrayList<Coordinate> fetchCoordinates(Integer id) throws SQLException {
  35. ArrayList<Coordinate> coordinates = new ArrayList<>();
  36.  
  37. try {
  38.  
  39. Class.forName("com.mysql.jdbc.Driver");
  40.  
  41. con = DriverManager.getConnection(url, user, password);
  42. String sql = "SELECT * FROM coordinates where id > ?";
  43. preparedStatement = con.prepareStatement(sql);
  44.  
  45. preparedStatement.setInt(1, id);
  46. rs = preparedStatement.executeQuery();
  47. double lat;
  48. double lng;
  49. int coordinate_id;
  50. while (rs.next()) {
  51. lat = rs.getDouble("lat");
  52. lng = rs.getDouble("lng");
  53. coordinate_id = rs.getInt("id");
  54. coordinates.add(new Coordinate(lat, lng, coordinate_id));
  55. }
  56.  
  57. } catch (SQLException | ClassNotFoundException sqlEx) {
  58. sqlEx.printStackTrace();
  59. } finally {
  60. try {
  61. con.close();
  62. } catch (SQLException ignored) {
  63. }
  64. try {
  65. if (stmt != null) {
  66. stmt.close();
  67. }
  68. } catch (SQLException ignored) {
  69. }
  70. try {
  71. if (preparedStatement != null) {
  72. preparedStatement.close();
  73. }
  74. } catch (SQLException ignored) {
  75. }
  76. try {
  77. if (rs != null) {
  78. rs.close();
  79. }
  80. } catch (SQLException ignored) {
  81. }
  82. }
  83.  
  84. return coordinates;
  85. }
  86.  
  87. public static class Coordinate {
  88. double lat;
  89. double lng;
  90. int id;
  91.  
  92. Coordinate(double x, double y, int id) {
  93. lat = x;
  94. lng = y;
  95. this.id = id;
  96. }
  97. }
  98. }
Add Comment
Please, Sign In to add comment