samedi 16 février 2019

How to write a value from the command line to a database if it's a certain double value

I'm working on a USB Scale program that reads value's from an external weighing scale and writes them to the command line when a weighted object is placed on it in an infinite loop. However what I am attempting to do is incorporate an else if statement that when the value is equal to the double value 'Dozen Eggs' of the weighted object it queries the database and inserts the values and attributes into the table. The integer doesn't seem to compare to the double value and skips over the if statement and therefore I was wondering how would I be able to make the else if statement recognizable and for it to write to the database in real time, simultaneously to when the values are writing to the command line?

I've tested the connection to the database in a separate class and it commits the attributes perfectly so the connection isn't the issue.

 @Override
 public void dataEventOccurred(UsbPipeDataEvent upde) {
boolean empty = data[1] == 2;
boolean overweight = data[1] == 6;
boolean negative = data[1] == 5;
boolean grams = data[2] == 2;
int scalingFactor = data[3];
double DozenEggs = 144.0;
double OneEgg = 0.2;
int AverageSellingUnitWeight = (data[4] & 0xFF) + (data[5] << 8);


if (empty) {
  System.out.println("EMPTY");
} else if (overweight) {
  System.out.println("OVERWEIGHT");
} else if (negative) {
  System.out.println("NEGATIVE");
} else if(AverageSellingUnitWeight == DozenEggs) {
    //then we have a dozen eggs in the fridge.
    //add 6 eggs to Fridge table in the database.
        {
            try {
                // create a mysql database connection
                String myDriver = "com.mysql.cj.jdbc.Driver";
                String myUrl = "jdbc:mysql://localhost:3306/smartfridge?autoReconnect=true&useSSL=false;";
                Class.forName(myDriver);
                Connection conn = DriverManager.getConnection(myUrl, "root", "admin");


                // the mysql insert statement
                String query = " insert into fridge (name, UnitOfSale, ContentsQuantity, department, AverageSellingUnitWeight)"
                        + " values (?, ?, ?, ?, ?)";

                // create the mysql insert preparedstatement
                PreparedStatement preparedStmt = conn.prepareStatement(query);
                preparedStmt.setString(2, "Eggs");
                preparedStmt.setInt(3, 1);
                preparedStmt.setInt(4, 6);
                preparedStmt.setString(5, "Milk, Butter & Eggs");
                preparedStmt.setDouble(6, DozenEggs);

                // execute the preparedstatement
                preparedStmt.execute();

                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

Aucun commentaire:

Enregistrer un commentaire