Java 如何在if语句中调用函数,该语句将在权重更改时写入数据库
我试图调用一个函数(如下面的伪代码所示),当它校准我的USB DYMO M5秤上的纸箱重量时,该函数将计算一箱6个鸡蛋的50个左右读数,一旦取出一个鸡蛋,它将向购物数据库表写入一个鸡蛋,并从冰箱数据库中的6个鸡蛋中取出一个鸡蛋。我想知道最有效的方法是什么?如有任何反馈,将不胜感激 整数计数=0; int-oldwight=0Java 如何在if语句中调用函数,该语句将在权重更改时写入数据库,java,database,if-statement,Java,Database,If Statement,我试图调用一个函数(如下面的伪代码所示),当它校准我的USB DYMO M5秤上的纸箱重量时,该函数将计算一箱6个鸡蛋的50个左右读数,一旦取出一个鸡蛋,它将向购物数据库表写入一个鸡蛋,并从冰箱数据库中的6个鸡蛋中取出一个鸡蛋。我想知道最有效的方法是什么?如有任何反馈,将不胜感激 整数计数=0; int-oldwight=0 //oldweight = weight; if (oldweight == weight) { // do nothing } e
//oldweight = weight;
if (oldweight == weight) {
// do nothing
} else {
System.out.println("Weight Changes");
/* write DB
//if (weight < oldweight){
//then something was taken off
*
* remove equivalent amount of eggs from db.fridge.
//if(db.frdige == 0 eggs){
//order eggs
}
if(weight > oldweight){
somebody put eggs in the fridge.
}
*
*
*
}*/
oldweight=weight;
}
if (weight != oldweight) {
while (count <= 5000) {
count++;
}
while (count < 5)
weight = count++;
//oldwight=weight;
如果(旧重量==重量){
//无所事事
}否则{
系统输出打印项次(“重量变化”);
/*写数据库
//如果(重量<旧重量){
//然后有东西被拿走了
*
*从冰箱中取出等量的鸡蛋。
//如果(db.frdige==0){
//点鸡蛋
}
如果(重量>旧重量){
有人把鸡蛋放进冰箱。
}
*
*
*
}*/
重量=重量;
}
如果(重量!=旧重量){
while(count您的第一步:在java中处理一般数据库时,您编写一个类来表示数据库中的表,这称为“bean”。您可以将食物列表和冰箱列表分类为一般项目
package scrap;
public class FoodList {
private String Vegetable;
private String meat;
private String liquid;
private String other;
public FoodList() {
//default constructor
}
public String getVegetable() {
return Vegetable;
}
public void setVegetable(String vegetable) {
Vegetable = vegetable;
}
public String getMeat() {
return meat;
}
public void setMeat(String meat) {
this.meat = meat;
}
public String getLiquid() {
return liquid;
}
public void setLiquid(String liquid) {
this.liquid = liquid;
}
public String getOther() {
return other;
}
public void setOther(String other) {
this.other = other;
}
}
像这样的冰箱课:
package scrap;
public class FridgeContents {
private String Vegetable;
private String meat;
private String liquid;
private String other;
public FridgeContents() {
//default constructor
}
public String getVegetable() {
return Vegetable;
}
public void setVegetable(String vegetable) {
Vegetable = vegetable;
}
public String getMeat() {
return meat;
}
public void setMeat(String meat) {
this.meat = meat;
}
public String getLiquid() {
return liquid;
}
public void setLiquid(String liquid) {
this.liquid = liquid;
}
public String getOther() {
return other;
}
public void setOther(String other) {
this.other = other;
}
}
然后,您将需要一个类来创建与您使用的数据库程序的连接,以及一个“rowmapper”类,它是一个将数据库返回的内容写入bean的类。
不必全部写出来,只要说数据库表与JavaBean匹配就足够了,根据您使用的框架和项目,有许多不同的风格和方法写入Bean