有 Java 编程相关的问题?

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

mysql无法从Java中的数据库读取表中的行

我正在编写代码来实现库存模型。作为其中的一部分,我必须从数据库中获取项目,设置项目的属性,并将该项目对象添加到列表中,然后在列表中执行一些功能。数据库中有三个表,每个表包含三行。但问题是,只有最后一个表中的最后一行被全部九次插入。 请帮我找出代码中的问题所在。 readAllItemsFromDb方法必须执行上述任务并返回项对象列表。Item类包含id、description、weight、price、manufacturingDate、useBeforeMonths等字段,以及它们的set和get方法

我的方法代码如下--

public List<Item> readAllItemsFromDb() {
    // TODO Auto-generated method stub
    List<Item> list=new ArrayList<Item>();
    Item item=new Item();
    Milk milk=new Milk();
    Cheese cheese=new Cheese();
    Wheat wheat=new Wheat();

    String cheese_type=new String();
    String milk_type=new String();
    String wheat_type=new String();
    String ingred_cheese=new String();

    String cheese_query="select id,description,weight,price,mfg_date,UseBeforeInMonths from cheese_tbl;";
    String milk_query="select id,description,weight,price,mfg_date,UseBeforeInMonths from milk_tbl;";
    String wheat_query="select id,description,weight,price,mfg_date,UseBeforeInMonths from wheat_tbl;";

    try {
        con=dcm.getConnection();//Theres' no problem with Connection 
        st=con.createStatement();
        rs=st.executeQuery(cheese_query);
        while(rs.next()){
            item.setId(rs.getInt(1));
            item.setDescription(rs.getString(2));
            item.setWeight(rs.getFloat(3));
            item.setPrice(rs.getFloat(4));
            item.setManufacturingDate(rs.getDate(5));
            item.setUseBeforeMonths(rs.getInt(6));

            list.add(item);
        }

        rs=st.executeQuery(milk_query);
        while(rs.next()){
            item.setId(rs.getInt(1));
            item.setDescription(rs.getString(2));
            item.setWeight(rs.getFloat(3));
            item.setPrice(rs.getFloat(4));
            item.setManufacturingDate(rs.getDate(5));
            item.setUseBeforeMonths(rs.getInt(6));
            list.add(item);

        }

        rs=st.executeQuery(wheat_query);
        while(rs.next()){
            item.setId(rs.getInt(1));
            item.setDescription(rs.getString(2));
            item.setWeight(rs.getFloat(3));
            item.setPrice(rs.getFloat(4));
            item.setManufacturingDate(rs.getDate(5));
            item.setUseBeforeMonths(rs.getInt(6));

            list.add(item); 
        }


    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return list;
}

共 (2) 个答案

  1. # 1 楼答案

    您只有一个object Item=new Item()的实例;在功能开始时,然后在每个while中使用相同的对象项。在每次while循环迭代中,您需要创建将添加到列表中的新对象实例

    while(rs.next()){
            item = new Item(); //this one 
            item.setId(rs.getInt(1));
            item.setDescription(rs.getString(2));
            item.setWeight(rs.getFloat(3));
            item.setPrice(rs.getFloat(4));
            item.setManufacturingDate(rs.getDate(5));
            item.setUseBeforeMonths(rs.getInt(6));
    
            list.add(item); 
        }
    
  2. # 2 楼答案

    这是因为您总是使用同一个Item对象,需要为每个rs.next()调用创建一个new Item()