我正在通过if语句传递一个sql查询,如下所示。我想知道如何限制查询从无限次执行到一次关闭?我需要循环,因为它一直在权衡秤的内容,并且当值在我的数组范围内时,它将执行查询,但是只要权重在该范围内,它就会重复执行。有没有一种解决方法,即使权重仍在范围内,一旦执行就不会再次执行?
public class UsbScale implements UsbPipeListener {
private final UsbDevice device;
private UsbInterface iface;
private UsbPipe pipe;
private byte[] data = new byte[6];
private UsbScale(UsbDevice device) {
this.device = device;
}
public static void main(String[] args) throws UsbException {
UsbScale scale = UsbScale.findScale();
scale.open();
try {
for (boolean i = true; i; i = true) {
scale.syncSubmit();
}
} finally {
scale.close();
}
}
public static UsbScale findScale() throws UsbException {
UsbServices services = UsbHostManager.getUsbServices();
UsbHub rootHub = services.getRootUsbHub();
// Dymo M5 Scale:
UsbDevice device = findDevice(rootHub, (short) 0x0922, (short) 0x8003);
// Dymo M25 Scale:
if (device == null) {
device = findDevice(rootHub, (short) 0x0922, (short) 0x8005);
}
if (device == null) {
return null;
}
return new UsbScale(device);
}
private static UsbDevice findDevice(UsbHub hub, short vendorId, short productId) {
for (UsbDevice device : (List<UsbDevice>) hub.getAttachedUsbDevices()) {
UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor();
if (desc.idVendor() == vendorId && desc.idProduct() == productId) {
return device;
}
if (device.isUsbHub()) {
device = findDevice((UsbHub) device, vendorId, productId);
if (device != null) {
return device;
}
}
}
return null;
}
private void open() throws UsbException {
UsbConfiguration configuration = device.getActiveUsbConfiguration();
iface = configuration.getUsbInterface((byte) 0);
// this allows us to steal the lock from the kernel
iface.claim(usbInterface -> true);
final List<UsbEndpoint> endpoints = iface.getUsbEndpoints();
pipe = endpoints.get(0).getUsbPipe(); // there is only 1 endpoint
pipe.addUsbPipeListener(this);
pipe.open();
}
private void syncSubmit() throws UsbException {
pipe.syncSubmit(data);
}
public void close() throws UsbException {
pipe.close();
iface.release();
}
@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];
int weight = (data[4] & 0xFF) + (data[5] << 8);
// int phoneWeights[] = new int[5];
// int minWeight = 142;
//int previous weight=0;
boolean phoneOnScale = false;
int[] phoneWeight = {140,150};
/*
for(int i=0, i=Length(phoneWeights); i++) { phoneWeights[i] = minweight+i; }
*/
/*
* System.out.println(String.format("Weight = %,.1f%s", scaleWeight(weight,
* scalingFactor), grams ? "g" : "oz"));
*/
System.out.println("My Weight: " + weight);
/*if(newweight != oldweight) {
oldweight = newweight;
write to db shopping list;
}*/
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();
}
} else {
phoneOnScale = false;
}
答案 0 :(得分:0)
您可以尝试做的是在开始时将布尔值设置为false。然后在查询前有另一个if循环,如果查询为假,它将仅运行查询。然后,一旦运行查询,就可以使用循环本身将变量更改为true。 用伪代码
boolean querychecker = False
#this would have to be done outside your other loop. Maybe at the beginning of the class.
if (querychecker == False){
do(query)
querychecker = True
}
else {
#whatever you want to do
}