我正在从覆盖旧值的数据库中获取值,怎么知道变量值何时更改。
while(result.next())
{
String rteCd = result.getString("Rte_Cd")
}
每次rteCd都会被数据库值覆盖,我想检查它在哪一点已更改,因为它也可以相同,当它更改时我需要执行一些操作
npte:-我无法更改数据库,查询可以多次返回相同的值
答案 0 :(得分:0)
Java.util.Observable可能会有所帮助。 如果此对象已更改,则返回java.util.Observable.hasChanged()方法。 这是您可以签出的示例代码
import java.util.Observable;
import java.util.Observer;
class ObservedObject extends Observable {
private String watchedValue;
public ObservedObject(String value) {
watchedValue = value;
}
public void setValue(String value) {
// if value has changed notify observers
if(!watchedValue.equals(value)) {
watchedValue = value;
// mark as value changed
setChanged();
}
}
}
public class ObservableDemo implements Observer {
public String name;
public ObservableDemo(String name) {
this.name = name;
}
public static void main(String[] args) {
// create watched and watcher objects
ObservedObject watched = new ObservedObject("Original Value");
// watcher object listens to object change
ObservableDemo watcher = new ObservableDemo("Watcher");
// add observer to the watched object
watched.addObserver(watcher);
// trigger value change
System.out.println("setValue method called...");
watched.setValue("New Value");
// check if value has changed
if(watched.hasChanged())
System.out.println("Value changed");
else
System.out.println("Value not changed");
}
public void update(Observable obj, Object arg) {
System.out.println("Update called");
}
}
输出
setValue方法称为... 值已更改
答案 1 :(得分:-1)
在while循环之外声明两个以上的变量,然后看到这样的旧值和新值
String oldvalue="";
String newvalue="";
while(result.next())
{
oldvalue=rteCd;
String rteCd = result.getString("Rte_Cd")
newvalue=rteCd;
}
//Now Display them
println("old value"+oldvalue);
println("new value"+newvalue);
答案 2 :(得分:-1)
dd(User::find(1)->role);