我已经在 SO 上检查了类似的答案,大多数情况下他们建议使用结果集来检查数据库中是否已经存在该条目。不知道我在这里做错了什么
public void setMonitorData(String name, monitorData data){
Statement stmt = null;
PreparedStatement stmtInsert;
PreparedStatement stmtUpdate;
ResultSet rs = null;
for(int i = 0; i<data.wiredVector.size(); i++){
if(data.wiredVector.elementAt(i).getInterfaceName().equals(name)){
try{
String checkRecord = "SELECT * FROM list_A WHERE NAME = '" + name + "'";
stmt = conn.createStatement();
rs = stmt.executeQuery(checkRecord);
if(!rs.next()){
String insertData = "INSERT INTO list_A (NAME, mac_add, ip_add, default_gateway,"
+ " net_add, mask, broad_add, max_transfer_rate, curr_transfer_rate,"
+ " curr_used_bandwidth, packet_error_rate )"
+ "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
//preparedStatemend code here....
else{
String update = "UPDATE list_A SET ..."
.....
}
在我执行代码之后..在数据库中有重复的记录,好像它完全跳过了条件