Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从Java中的数据库读取表中的行_Java_Mysql - Fatal编程技术网

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

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

我正在编写代码来实现库存模型。作为其中的一部分,我必须从数据库中获取项目,设置项目的属性,并将该项目对象添加到列表中,然后在列表中执行一些功能。数据库中有三个表,每个表包含3行。但问题是,只有最后一个表中的最后一行被全部插入了九次。 请帮我找出代码中的问题所在。 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;
}

您必须在while循环的每个迭代开始时创建一个新的Item对象

   while(rs.next()){
        item = new Item();
        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 = new Item();
        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 = new Item();
        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); 
    }

这是因为您始终使用相同的Item对象,您需要为每个rs.next调用创建一个新的Item。

您只有一个object Item Item=new Item的实例;在函数开始时,然后在每个函数中使用相同的对象项。在每次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); 
    }