我正在通过无限for循环运行SQL语句,但找不到仅在数据库中不存在值的情况下仅执行一次的方法。我该如何解决?
if(phoneWeight[0] <= weight && weight <= phoneWeight[1]) {
phoneOnScale = true;
System.out.println("Phone is on scale");
// write one phone to table in db.
try {
// create a mysql database connection
String myDriver = "com.mysql.jdbc.Driver";
String myUrl = "jdbc:mysql://localhost:3306/smartfridge";
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(1, "Eggs");
preparedStmt.setInt(2, 1);
preparedStmt.setInt(3, 6);
preparedStmt.setString(4, "Milk, Butter & Eggs");
preparedStmt.setBoolean(5, phoneOnScale);
// execute the preparedstatement
preparedStmt.execute();
} catch (Exception e) {
e.printStackTrace();
//System.out.println("Eggs added to Fridge");
}
} else {
phoneOnScale = false;
}