如何插入列表<产品>进入数据库 [英] How to insert List <Products> into database

查看:78
本文介绍了如何插入列表<产品>进入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手.我已经创建了一个通用的Products类型列表,如何将其添加到数据库中.该列表包含Products的对象,数据库中的列是Products类的字段.即使我用listvariable.get(0)分隔列表,依此类推,我仍然得到对象,而不是对象内部的值.

I am new to Java. I have created a generic list of Products type ,how can I add it into the database. The list contains objects of Products, and columns in the database are fields of Products class. Even if I separate the list by listvariable.get(0) and so on, I get the object, not the values inside that objects.

UPDATE:插入使用for循环并获取每个对象的字段.有没有更好的方法

UPDATE : Inserted using for loop and getting fields for each objects. Is there any better way

import java.util.*;
public class Products {

    public static List <Products> li  = new ArrayList<Products> ();

    static 
    {
        Products o = new Products (1,"Milky Way",12.0,7);   // Static because  
        Products o1 = new Products (2,"Dairy Milk",50.0,17); // these entries 
        Products o2 = new Products (3,"Borunville",70.0,27);  // are mandatory
        Products o3 = new Products (4,"Lindt",1022.0,107);
        li.add(o);
        li.add(o1);
        li.add(o2);
        li.add(o3);
    }
    int ItemCode;
    String ItemName;
    double UnitPrice;
    int AvailableCount; 
    public int v=3;

    Products()
    {}

    Products (int x,String y, double c, int d)
    {
        ItemCode=x;
        ItemName=y;
        UnitPrice=c;
        AvailableCount=d;

    }
    public String toString ()       
    {

        return (  ItemName+" "+ ItemCode +" "+ UnitPrice + " "+ AvailableCount);
    }
    void addProduct()
    {
        li.add(this);
    }

    public List <Products> initProducts()       

    {                   return li;
    }

}

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Iterator;


public class Shopowner {



    public static void main (String ...args)

    { 

        Products o = new Products(6,"Eclairs",12.33,5);
        o.addProduct();

    System.out.println(new Products().initProducts());



    try
    {
        Connectivity.establishConnection(); // static method for db url and drivers

         for (int i =0;i<4;i++)
    {
        Products x=Products.li.get(i);

        String name=x.ItemName;
        int id= x.ItemCode;
        int count =x.AvailableCount;
        double price = x.UnitPrice;
        PreparedStatement stm = Connectivity.con.prepareStatement("insert into Products_tbl values (?,?,?,?)");
        stm.setInt(1, id);
        stm.setString(2, name);
        stm.setDouble(3,price);
    stm.setInt(4, count);
    stm.executeUpdate();
    }



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

    //System.out.println(new Products().li);


    }
}

推荐答案

以这种方式使用批量插入:

Use batch insert in this manner:

try {
  connection con.setAutoCommit(false);        
  PreparedStatement prepStmt = con.prepareStatement(    
    "insert into product(code,name,price,available) values (?,?,?,?");
  Iterator<Product> it = li.iterator();
  while(it.hasNext()){
     Product p = it.next();
    prepStmt.setString(1,p.getCode());            
    prepStmt.setString(2,p.getCode());
    prepStmt.setInt(3,p.getPrice());
    prepStmt.setBoolean(4,p.isAvailable());
    prepStmt.addBatch();                      

  }      
  int [] numUpdates=prepStmt.executeBatch();
  for (int i=0; i < numUpdates.length; i++) {
    if (numUpdates[i] == -2)
      System.out.println("Execution " + i + 
        ": unknown number of rows updated");
    else
      System.out.println("Execution " + i + 
        "successful: " + numUpdates[i] + " rows updated");
  }
  con.commit();
} catch(BatchUpdateException b) {
  // process BatchUpdateException
} 

这篇关于如何插入列表&lt;产品&gt;进入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆