Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.json.JSONObject;
- import org.restlet.Server;
- import org.restlet.data.Form;
- import org.restlet.data.Protocol;
- import org.restlet.representation.Representation;
- import org.restlet.representation.StringRepresentation;
- import org.restlet.resource.Get;
- import org.restlet.resource.Post;
- import org.restlet.resource.ServerResource;
- import java.sql.*;
- public class Servertest extends ServerResource {
- public static void main(String[] args) throws Exception {
- try {
- Server server = new Server(Protocol.HTTP, 1338, Servertest.class);
- server.start();
- System.out.println("hello");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Post("/post")
- public void someMethod(Representation rep) throws ClassNotFoundException, SQLException {
- final Form form = new Form(rep);
- String longi = form.getFirstValue("longi");
- String lat = form.getFirstValue("lat");
- String user = form.getFirstValue("username");
- System.out.println(form.getFirstValue("username"));
- System.out.println(form.getFirstValue("longi"));
- System.out.println(form.getFirstValue("lat"));
- Class.forName("org.h2.Driver");
- Connection conn = DriverManager.getConnection("jdbc:h2:~/test3", "sa", "");
- Statement stmt = conn.createStatement();
- stmt.executeUpdate("INSERT INTO location (user, lat, lng) VALUES ('" + user + "', '" + lat + "', '" + longi + "' )");
- ResultSet rs = stmt.executeQuery("SELECT * FROM location");
- stmt.close();
- conn.close();
- }
- @Get("/location")
- public StringRepresentation getFromDb() throws ClassNotFoundException, SQLException {
- System.out.println("get request start");
- Class.forName("org.h2.Driver");
- Connection conn = DriverManager.getConnection("jdbc:h2:~/test3", "sa", "");
- Statement stmt = conn.createStatement();
- ResultSet getRs = stmt.executeQuery("SELECT TOP 1 * FROM location ORDER BY id DESC");
- String result = "";
- while (getRs.next()) {
- String getName = getRs.getString("user");
- String getLat = getRs.getString("lat");
- String getLng = getRs.getString("lng");
- JSONObject jObject = new JSONObject();
- jObject.put("lat" , getLat);
- jObject.put("longi", getLng);
- System.out.println(jObject.toString());
- result = jObject.toString();
- }
- stmt.close();
- conn.close();
- return new StringRepresentation(result);
- }
- @Post("/bluetooth")
- public void bluetoothPost(Representation rep) throws ClassNotFoundException, SQLException {
- final Form form = new Form(rep);
- String info = form.getFirstValue("info");
- String address = form.getFirstValue("address");
- System.out.println(form.getFirstValue("info"));
- System.out.println(form.getFirstValue("address"));
- Class.forName("org.h2.Driver");
- Connection conn = DriverManager.getConnection("jdbc:h2:~/test3", "sa", "");
- Statement stmt = conn.createStatement();
- stmt.executeUpdate("INSERT INTO BLUETOOTH (info, address) VALUES ('" + info + "', '" + address + "', )");
- ResultSet rs = stmt.executeQuery("SELECT * FROM BLUETOOTH");
- stmt.close();
- conn.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement