Advertisement
Guest User

Untitled

a guest
Oct 8th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. import org.json.JSONObject;
  2. import org.restlet.Server;
  3. import org.restlet.data.Form;
  4. import org.restlet.data.Protocol;
  5. import org.restlet.representation.Representation;
  6. import org.restlet.representation.StringRepresentation;
  7. import org.restlet.resource.Get;
  8. import org.restlet.resource.Post;
  9. import org.restlet.resource.ServerResource;
  10.  
  11. import java.sql.*;
  12.  
  13. public class Servertest extends ServerResource {
  14.  
  15. public static void main(String[] args) throws Exception {
  16. try {
  17.  
  18. Server server = new Server(Protocol.HTTP, 1338, Servertest.class);
  19. server.start();
  20. System.out.println("hello");
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }
  25.  
  26. @Post("/post")
  27. public void someMethod(Representation rep) throws ClassNotFoundException, SQLException {
  28. final Form form = new Form(rep);
  29. String longi = form.getFirstValue("longi");
  30. String lat = form.getFirstValue("lat");
  31. String user = form.getFirstValue("username");
  32. System.out.println(form.getFirstValue("username"));
  33. System.out.println(form.getFirstValue("longi"));
  34. System.out.println(form.getFirstValue("lat"));
  35. Class.forName("org.h2.Driver");
  36. Connection conn = DriverManager.getConnection("jdbc:h2:~/test3", "sa", "");
  37. Statement stmt = conn.createStatement();
  38. stmt.executeUpdate("INSERT INTO location (user, lat, lng) VALUES ('" + user + "', '" + lat + "', '" + longi + "' )");
  39. ResultSet rs = stmt.executeQuery("SELECT * FROM location");
  40. stmt.close();
  41. conn.close();
  42. }
  43.  
  44. @Get("/location")
  45. public StringRepresentation getFromDb() throws ClassNotFoundException, SQLException {
  46. System.out.println("get request start");
  47. Class.forName("org.h2.Driver");
  48. Connection conn = DriverManager.getConnection("jdbc:h2:~/test3", "sa", "");
  49. Statement stmt = conn.createStatement();
  50. ResultSet getRs = stmt.executeQuery("SELECT TOP 1 * FROM location ORDER BY id DESC");
  51. String result = "";
  52. while (getRs.next()) {
  53. String getName = getRs.getString("user");
  54. String getLat = getRs.getString("lat");
  55. String getLng = getRs.getString("lng");
  56. JSONObject jObject = new JSONObject();
  57. jObject.put("lat" , getLat);
  58. jObject.put("longi", getLng);
  59. System.out.println(jObject.toString());
  60. result = jObject.toString();
  61. }
  62.  
  63. stmt.close();
  64. conn.close();
  65. return new StringRepresentation(result);
  66. }
  67.  
  68. @Post("/bluetooth")
  69. public void bluetoothPost(Representation rep) throws ClassNotFoundException, SQLException {
  70. final Form form = new Form(rep);
  71. String info = form.getFirstValue("info");
  72. String address = form.getFirstValue("address");
  73. System.out.println(form.getFirstValue("info"));
  74. System.out.println(form.getFirstValue("address"));
  75. Class.forName("org.h2.Driver");
  76. Connection conn = DriverManager.getConnection("jdbc:h2:~/test3", "sa", "");
  77. Statement stmt = conn.createStatement();
  78. stmt.executeUpdate("INSERT INTO BLUETOOTH (info, address) VALUES ('" + info + "', '" + address + "', )");
  79. ResultSet rs = stmt.executeQuery("SELECT * FROM BLUETOOTH");
  80. stmt.close();
  81. conn.close();
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement