如何在java中将arraylist传递给数据库

如何在java中将arraylist传递给数据库,java,xml,arraylist,Java,Xml,Arraylist,我用hibernate创建了一个新的derby数据库。我可以在Apache derby中创建一个数据库。我可以从xml中的节点获取数据。并将其存储在arraylist中。我知道如何将arraylist contant传递给数据库类 xml.java public class xml_read { public static void main(String argv[]) { try { File fXmlFile = new File("c:\\t

我用hibernate创建了一个新的derby数据库。我可以在Apache derby中创建一个数据库。我可以从xml中的节点获取数据。并将其存储在arraylist中。我知道如何将arraylist contant传递给数据库类

xml.java

       public class xml_read {

    public static void main(String argv[]) {

    try {

     File fXmlFile = new File("c:\\testing.xml");
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();

   // System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    NodeList nList = doc.getElementsByTagName("device");
   // System.out.println("-----------------------");

for (int temp = 0; temp < nList.getLength(); temp++) {

   Node nNode = nList.item(temp);       
   if (nNode.getNodeType() == Node.ELEMENT_NODE) {

     Element eElement = (Element) nNode;
     ArrayList arrayList = new ArrayList();

     //System.out.println(arrayList);
     String type=getTagValue("type", eElement);
     String Name=getTagValue("name", eElement);
     String Setup=getTagValue("setup", eElement);
     arrayList.add(type);
     arrayList.add(Name);
     arrayList.add(Setup);
     System.out.println(arrayList);
      System.out.println("type : "  + getTagValue("type",eElement));
      System.out.println("Name : "  + getTagValue("name",eElement));
      System.out.println("Set up : "  + getTagValue("setup",eElement));


    }
  }
       } catch (Exception e) {
           e.printStackTrace();
    }
   }

    private static String getTagValue(String sTag, Element eElement){
  NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
  Node nValue = (Node) nlList.item(0); 

  return nValue.getNodeValue();    
  }

 }
公共类xml\u读取{
公共静态void main(字符串argv[]){
试一试{
File fXmlFile=新文件(“c:\\testing.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
//System.out.println(“根元素:+doc.getDocumentElement().getNodeName());
NodeList nList=doc.getElementsByTagName(“设备”);
//System.out.println(“--------------------------”;
对于(int-temp=0;temp
device.java

   public class Device  {
    private String type;
    private String setup;
    private String name;
    private Long deviceId;
    private Set<CommandInfo> commandSet = new HashSet<CommandInfo>(); 

    public Device() {

    }

    public Device(String name, String type, String setup) {
            System.out.println("name = "+name+" type = "+type+" setup = "+setup);
            this.name = name;
            this.type = type;
            this.setup = setup;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @return the setup
     */
    public String getSetup() {
        return setup;
    }

    /**
     * @param setup the setup to set
     */
    public void setSetup(String setup) {
        this.setup = setup;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the id
     */
    @Id
    @GeneratedValue
    @Column(name = "DEVICE_ID")
    public Long getDeviceId() {
        return deviceId;
    }

    /**
     * @param id the id to set
     */
    public void setDeviceId(Long id) {
        this.deviceId = id;
    }

    /**
     * @return the commandSet
     */
    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "DEVICE_COMMAND", joinColumns = { 
            @JoinColumn(name = "DEVICE_ID") }, 
            inverseJoinColumns = { @JoinColumn(name = "COMMAND_ID") 
    })
    public Set<CommandInfo> getCommandSet() {
        return commandSet;
    }

    /**
     * @param commandSet the commandSet to set
     */
    public void setCommandSet(Set<CommandInfo> commandSet) {
        this.commandSet = commandSet;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Device [type=");
        builder.append(type);
        builder.append(", setup=");
        builder.append(setup);
        builder.append(", name=");
        builder.append(name);
        builder.append(", deviceId=");
        builder.append(deviceId);
        builder.append(", commandSet=");
        builder.append(commandSet);
        builder.append("]");
        return builder.toString();
    }


       }
公共类设备{
私有字符串类型;
私有字符串设置;
私有字符串名称;
专用长设备ID;
private Set commandSet=new HashSet();
公共设备(){
}
公用设备(字符串名称、字符串类型、字符串设置){
System.out.println(“name=“+name+”type=“+type+”setup=“+setup”);
this.name=名称;
this.type=type;
this.setup=setup;
}
/**
*@返回类型
*/
公共字符串getType(){
返回类型;
}
/**
*@param type要设置的类型
*/
公共void集合类型(字符串类型){
this.type=type;
}
/**
*@返回设置
*/
公共字符串getSetup(){
返回设置;
}
/**
*@param设置要设置的设置
*/
公共无效设置(字符串设置){
this.setup=setup;
}
/**
*@返回名称
*/
公共字符串getName(){
返回名称;
}
/**
*@param name要设置的名称
*/
公共void集合名(字符串名){
this.name=名称;
}
/**
*@返回id
*/
@身份证
@生成值
@列(name=“DEVICE\u ID”)
公共长getDeviceId(){
返回设备ID;
}
/**
*@param id要设置的id
*/
公共无效setDeviceId(长id){
this.deviceId=id;
}
/**
*@返回命令集
*/
@OneToMany(级联=级联类型.ALL)
@JoinTable(name=“DEVICE_COMMAND”,joinColumns={
@JoinColumn(name=“DEVICE_ID”)},
inverseJoinColumns={@JoinColumn(name=“COMMAND\u ID”)
})
公共集getCommandSet(){
返回命令集;
}
/**
*@param commandSet将commandSet设置为set
*/
公共void setCommandSet(Set commandSet){
this.commandSet=commandSet;
}
/*(非Javadoc)
*@see java.lang.Object#toString()
*/
@凌驾
公共字符串toString(){
StringBuilder=新的StringBuilder();
builder.append(“设备[type=”);
builder.append(类型);
builder.append(“,setup=”);
附加(设置);
builder.append(“,name=”);
builder.append(名称);
builder.append(“,deviceId=”);
builder.append(设备ID);
append(“,commandSet=”);
append(commandSet);
生成器。追加(“]”);
返回builder.toString();
}
}

我需要将arraylist值传递给设备类。有人能帮助我如何将值传递给设备类吗?如果使用集合类型(例如,在您的案例中设置),则必须指定此集合如何映射到另一个数据库表。因此,在您的对象中,它是一个可直接访问的子对象集合,但在您的数据库中,它仍然在两个具有1:n或n:m关系的表中分开

如果使用JPA,只需在注释中指定关系:

@OneToMany(mappedBy = "sale")
private List<Sale> saleList;
@OneToMany(mappedBy=“sale”)
私人名单;
在JPA 2.0中,如果只使用基本类型,甚至可以避免指定单独的实体:

@ElementCollection@CollectionTable(name="item")
@Column(name="name")
private List<String> items;
@ElementCollection@CollectionTable(name=“item”)
@列(name=“name”)
私人清单项目;

如果使用集合类型(例如,在您的案例中设置),则必须指定如何将此集合映射到另一个数据库表。因此,在您的对象中,它是一个可直接访问的子对象集合,但在您的数据库中,它仍然在两个具有1:n或n:m关系的表中分开

如果使用JPA,只需在注释中指定关系:

@OneToMany(mappedBy = "sale")
private List<Sale> saleList;
@OneToMany(mappedBy=“sale”)
私人名单;
在JPA 2.0中,如果只使用基本类型,甚至可以避免指定单独的实体:

@ElementCollection@CollectionTable(name="item")
@Column(name="name")
private List<String> items;
@ElementCollection@CollectionTable(name=“item”)
@列(name=“name”)
私人清单项目;

为什么不遍历ArrayList并将值设置为设备类?为什么不遍历ArrayList并将值设置为yo