bizlogic.Sensors.java Source code

Java tutorial

Introduction

Here is the source code for bizlogic.Sensors.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bizlogic;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ett11281
 */
public class Sensors {

    public static void list(Connection DBcon) throws IOException, ParseException, SQLException {

        Statement st = null;
        ResultSet rs = null;

        try {
            st = DBcon.createStatement();
            rs = st.executeQuery("SELECT * FROM USERCONF.SENSORLIST");

        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Sensors.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        }
        try {
            FileWriter sensorsFile = new FileWriter("/var/lib/tomcat8/webapps/ROOT/Records/sensors.json");
            sensorsFile.write("");
            sensorsFile.flush();

            JSONParser parser = new JSONParser();

            JSONObject Records = new JSONObject();

            JSONObject operation_Obj = new JSONObject();
            JSONObject operand_Obj = new JSONObject();
            JSONObject unit_Obj = new JSONObject();
            JSONObject name_Obj = new JSONObject();
            JSONObject ip_Obj = new JSONObject();
            JSONObject port_Obj = new JSONObject();

            int _total = 0;

            JSONArray sensorList = new JSONArray();

            while (rs.next()) {

                JSONObject sensor_Obj = new JSONObject();
                int id = rs.getInt("sensor_id");
                String operation = rs.getString("operation");
                int operand = rs.getInt("operand");
                String unit = rs.getString("unit");
                String name = rs.getString("name");
                String ip = rs.getString("IP");
                int port = rs.getInt("port");

                sensor_Obj.put("recid", id);
                sensor_Obj.put("operation", operation);
                sensor_Obj.put("operand", operand);
                sensor_Obj.put("unit", unit);
                sensor_Obj.put("name", name);
                sensor_Obj.put("IP", ip);
                sensor_Obj.put("port", port);

                sensorList.add(sensor_Obj);
                _total++;

            }
            rs.close();

            Records.put("total", _total);
            Records.put("records", sensorList);

            sensorsFile.write(Records.toJSONString());
            sensorsFile.flush();
            sensorsFile.close();
        }

        catch (IOException ex) {
            Logger.getLogger(Sensors.class.getName()).log(Level.WARNING, null, ex);
        }
    }

    public static void add(Connection DBcon, String name, String IP, String operation, String operand, String unit,
            String port) throws SQLException {

        String _operation = operation;
        Statement st = null;
        ResultSet rs = null;

        switch (operation) {
        case "Add":
            _operation = "+";
            break;
        case "Subtract":
            _operation = "-";
            break;
        case "Multiply":
            _operation = "*";
            break;
        case "Divide":
            _operation = "/";
            break;
        default:
            break;

        }

        String sql_statement;

        st = DBcon.createStatement();
        //for(int i = 0; i<30; i++) {
        sql_statement = "INSERT INTO USERCONF.SENSORLIST(OPERATION, OPERAND, NAME, \"IP\", UNIT, PORT) "
                + "VALUES (" + "'" + _operation + "'" + ", " + "'" + operand + "'" + ", " + "'" + name + "'" + ", "
                + "'" + IP + "'" + ", " + "'" + unit + "'" + ", " + "'" + port + "'" + " );";
        System.out.println(sql_statement);
        st.clearBatch();
        st = DBcon.createStatement();
        DBcon.createStatement();
        st.executeUpdate(sql_statement);

    }

    public static void del(Connection DBcon, String deleteSensors) throws SQLException {

        Statement st = null;
        ResultSet rs = null;

        String sql_statement;

        String _deleteSensors = deleteSensors.replace(";", ",");

        st = DBcon.createStatement();
        //for(int i = 0; i<30; i++) {
        sql_statement = "DELETE FROM USERCONF.SENSORLIST WHERE SENSOR_ID IN(" + _deleteSensors + ")";
        System.out.println(sql_statement);
        st.clearBatch();
        st = DBcon.createStatement();
        DBcon.createStatement();
        st.executeUpdate(sql_statement);

    }
}