有 Java 编程相关的问题?

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

java如何将ArrayList数据插入数据库

我尝试使用ArrayList将数据插入数据库。有一个错误消息。 那是我的顾客。类方法。这是我将ArrayList传递到另一个类时得到的结果

incompatible types: ArrayList<String> cannot be converted to ArrayList<Inquiries>

我想知道如何使用正确的OOP概念来做到这一点

   public void passingMsg(ArrayList<Inquiries> arrlist){
        try {
            System.out.println("Method "+arrlist);
            String sq = "INSERT INTO Inquiries (name,mail,tp,msg)VALUES(?,?,?)";
            PreparedStatement pr = con.prepareStatement(sq);
            for(int i=0;i<arrlist.size();i++){
                pr.setString(1,arrlist.get(i).getName());
                pr.setString(2,arrlist.get(i).getMail());
                pr.setString(3,arrlist.get(i).getTp());
                pr.setString(4,arrlist.get(i).getMsg());


            }
            pr.executeQuery();//executeBatch();
        } catch (SQLException ex) {
        }

    }

这就是我从用户那里获取值的方式

 String name = txtName.getText();
        String mail = txtEmail.getText();
        String tp = txtTp.getText();
        String msg = txtMsg.getText();

        ArrayList<String> arrInq = new ArrayList<String>();
        arrInq.add(name);
        arrInq.add(mail);
        arrInq.add(tp);
        arrInq.add(msg);


        Custmer c =new Custmer();
        if( c.passingMsg(arrInq)){
            try {
                JOptionPane.showMessageDialog(this, "Successs!!");
            } catch (Exception e) {
                 JOptionPane.showMessageDialog(this, "Unsuccesss!!");
                e.printStackTrace();
            }
        }

这是我的询问。类别:

public class Inquiries {
    private String name;
    private String mail;
    private String tp;
    private String msg;


     public Inquiries(String name,String mail,String tp,String msg){
        this.name = name;
        this.mail = mail;
        this.tp = tp;
        this.msg = msg;
    }
//     
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMail() {
        return mail;
    }
    public void setMail(String mail) {
        this.mail = mail;
    }
    public String getTp() {
        return tp;
    }
    public void setTp(String tp) {
        this.tp = tp;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }

}

谁能解释一下这有什么问题吗。求你了


共 (2) 个答案

  1. # 1 楼答案

    您对ArrayList使用了错误的类型参数。你需要的不是ArrayList<String>,而是ArrayList<Inquiries>。要解决此问题,您应该删除此代码

        ArrayList<String> arrInq = new ArrayList<String>();
        arrInq.add(name);
        arrInq.add(mail);
        arrInq.add(tp);
        arrInq.add(msg);
    

    。。。并将其替换为以下代码:

        ArrayList<Inquiries> arrInq = new ArrayList<Inquiries>();
        arrInq.add(new Inquiries(name, mail, tp, msg));
    
  2. # 2 楼答案

    让我们以O-O-p的方式谈谈。 这里是您的模型,模型只是一个简单的类,它有实例成员和公共方法来获取和设置模型实例变量的值

    通常,我们将所有与数据库相关的操作代码放在各自的模型中。 e、 g.我有一个模型“model”,它通常映射到数据库表,称之为“TableModel”,我会这样做:

    public class Model{
        private int id;
        private String attr;
        //other properties of the model
    
        public int getId(){
          return id;
        }
        public void setId(int id){
          this.id=id;
        }
        //other getters and setters
    
        //here we write methods to performs database operations 
        public void save(){
            //use "this" to get properties of object
            //logic to save to this object in database table TableModel as record
        }
        public void delete(int id){
            //logic to delete this object i.e. from database table TableModel 
        }
        public Model get(int id){
           //retrieve record   from table TableModel with this id
        }
       //other methods to get data from database.
    }
    

    现在的问题是我如何在另一节课上使用这个。假设我有一个模型对象列表,我希望将它们插入到数据库中。我会这样做:

    public class AnotherClass{
        public void someMethod(){
            //create list of models objects e.g. get them from user interface
           ArrayList<Model> models=new ArrayList<>();
           for(int i=0;i<3;i++){
                Model model=new Model();
                model.setId(i);
                model.setAttr("attr"+i);
                models.add(model);
           }
           SomeOtherClass obj=new SomeOtherClass();
           obj.insert(models);
        }
    }
    
    public class SomeOtherClass{
       //other code above.....
    
       //my method that inserts each Model object in database
       //Note: this is sample method , you should do it in optimized way
       // e.g. batch insert
        public void insert(ArrayList<Model> models){
    
          for(Model myModel:models){
              myModel.save();
          }
    
       }
       //other code below.....
    }