有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将XML中的不同值插入数据库表

我有如下XML:

    <employee>
        <code>13</code>
        <label>Admin</label>
    </employee>
    <employee>
        <code>13</code>
        <label>Admin</label>
    </employee>
    <employee>
        <code>09</code>
        <label>Logistics</label>
    </employee>

在我的Oracle数据库中,我有两列,即CODE1、CODE2。 数据应该像CODE1=13和CODE2=09那样插入

但是,目前的情况是,代码1=13和代码2=13。和09未插入到数据库中

它只存储前2个值,忽略其余值。 我的要求是,重复值只能在DB中插入一次

预期结果: 代码1=13,代码2=09

以下是我的java代码:

    for (int i = 0; i < 2; i++) {
            final int count = i + 1;
            String code = null;
            final Emploi[] employee = tabLieuTrav.getEmployee();
                code = employee[i].getCode();
                if (code != null) {
                mapParam.addParamValue(CODE + count,
                        code);
            } else {
                mapParam.addParamValue(CODE + count, null,
                        Types.VARCHAR);
            }

getCode()从标记返回值(例如13)

提前谢谢你


共 (1) 个答案

  1. # 1 楼答案

    尝试以下解决方案

    首先,您应该创建一个Employee类,包括hasCode()equals()方法,如下所示

    public class Employee {
    
        private int code; 
        private String lable;
    
        public Employee(int code, String lable) {
            super();
            this.code = code;
            this.lable = lable;
        }
        public int getCode() {
            return code;
        }
        public void setCode(int code) {
            this.code = code;
        }
        public String getLable() {
            return lable;
        }
        public void setLable(String lable) {
            this.lable = lable;
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + code;
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Employee other = (Employee) obj;
            if (code != other.code)
                return false;
            return true;
        }
    }
    

    上面的hasCode()equals()方法是由EclipseIDE生成的。您可以像这样手动创建这些方法

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Employee) {
            return Objects.equals(code, ((Employee) obj).code);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return this.code;
    }
    

    等于方法:指示其他对象是否“等于”此对象for more info

    哈希代码方法:返回对象的哈希代码值。支持此方法是为了使用哈希表,例如HashMap提供的哈希表for more info

    然后,将employee数组添加到ArrayList。因为下面提到的方法描述了如何从ArrayList中获取不同的值

    Emploi[] employee = tabLieuTrav.getEmployee();
    List<Employee> empList = new ArrayList(Arrays.asList(employee));
    

    然后,可以使用以下方法之一从ArrayList(empList)中删除重复值

    方法一,使用Set(不包含重复元素的集合)for more info从ArrayList中删除重复项

    HashSet<Employee> uniqueEmployee = new HashSet(empList);
    

    方法二,使用Java8流不同方法从ArrayList中删除重复项(从集合中返回不同元素)for more info

    List<Employee> uniqueEmployee = empList..stream().distinct().collect(Collectors.toList();
    

    最后,您可以使用uniqueEmployee集合,如下所示

    for (Employee employee : uniqueEmployee) {
        code = employee.getCode();
        if (code != null) {
            mapParam.addParamValue(CODE + count, code);
        } else {
            mapParam.addParamValue(CODE + count, null, Types.VARCHAR);
        }
    }