【问题标题】:How to Insert ArrayList data to the DataBase如何将 ArrayList 数据插入数据库
【发布时间】:2015-12-09 15:57:04
【问题描述】:

我尝试使用 ArrayList 将数据插入数据库。有一个错误消息。 那是我的 Custmer.class 方法。这就是我将 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();
            }
        }

这是我的 Inquiries.class :

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;
    }

}

有人可以解释一下这有什么问题吗?请问?

【问题讨论】:

    标签: java mysql oop jdbc compiler-errors


    【解决方案1】:

    您为ArrayList 使用了错误的类型参数。你需要ArrayList&lt;Inquiries&gt; 而不是ArrayList&lt;String&gt;。要解决此问题,您应该删除此代码...

        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));
    

    【讨论】:

    • then:- 不兼容的类型:ArrayList 无法转换为 ArrayList ----
    • 这可能意味着,您需要修复 Inquiries 类的导入语句。如果没有更多信息,我无法提供更具体的帮助。
    【解决方案2】:

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

    一般我们将所有与数据库相关的操作代码放在各自的模型中。 例如我有模型“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.....
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      • 1970-01-01
      • 1970-01-01
      • 2011-08-06
      相关资源
      最近更新 更多