在后台调用删除查询以维护表中的数据

时间:2015-05-05 21:06:13

标签: java

我正在尝试在后台调用查询来删除在5分钟之前插入的记录,并且此刻不会更新。

我已经尝试过这个查询但它有效,但我不知道如何每5分钟调用一次。

DELETE FROM bus WHERE created_at < (NOW() - INTERVAL 5 MINUTE) 

正在为来自其他类的每个请求调用insertData方法。

public int insertData(String mac, int route, double latD, double longD) {
        int status = 201;

        connection();
        String host = "jdbc:mysql://localhost/busTracker";
        String user = "root";
        String password = "";

        try {

            Connection con = DriverManager.getConnection(host, user, password);
            // Create a statement
            Statement stt = con.createStatement();

            boolean result = stt.execute("SELECT 1 FROM bus LIMIT 1");
            if (result) {

                PreparedStatement prepBefore = con
                        .prepareStatement("SELECT latitude, longitude from bus");
                ResultSet rs = prepBefore.executeQuery();

                ArrayList<Double> array = new ArrayList<Double>();
                while (rs.next()) {
                    double lat2 = rs.getDouble("latitude");
                    double lon2 = rs.getDouble("longitude");
                    double dist = haversineDistance(latD, longD, lat2, lon2);
                    array.add(dist);
                }
                System.out.println("Array size: " + array.size());
                for (double item : array) {
                    if (item < 4) {
                        status = 208;
                        System.out.println("Status: " + status);
                        return status;

                    } else {

                        insert_update_data(mac, route, latD, longD, con);

                    }
                }
                return status;

            } else {
                // Create bus table
                stt.execute("CREATE TABLE IF NOT EXISTS bus"
                        + "(id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,"
                        + "mac VARCHAR(30) NOT NULL UNIQUE,"
                        + "route int(11) NOT NULL,"
                        + "latitude FLOAT(10,6) NOT NULL,"
                        + "longitude FLOAT(10,6) NOT NULL,"
                        + "created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");

                insert_update_data(mac, route, latD, longD, con);
                return status;

            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return status = 501;

        }
    }

正在调用insert方法的storeData方法。

@POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response storeData(Data data) {

        String macD = data.getMac();
        int routeD = data.getRoute();
        float latD = data.getLatitude();
        float longD = data.getLongitude();

        Database db = new Database();
        int status = db.insertData(macD, routeD, latD, longD);

        return Response.status(status).build();

    }

}

0 个答案:

没有答案