English 中文(简体)
Javatom ArrayList All Entries the Same
原标题:Java Custom ArrayList All Entries the Same

I have a custom an arraylist setup to hold data for me. However when I call the arraylist items all the items are the same.

我的阵列载体。

public static ArrayList<Animal> getTblHerd() throws Exception {
    CC_H2 db = new CC_H2();
    db.Connect(Variables.getStrConn(), Variables.getStrUser(),
            Variables.getStrPassword(), "Embedded");
    ResultSet rs = db.query("Select HERD_ID FROM tblHerd ORDER BY HERD_ID ASC");
    ArrayList<Animal> alAnimals = new ArrayList<Animal>();

    while (rs.next()) {
        int i = rs.getInt("HERD_ID");
        alAnimals.add(new Animal(i));
    }
    db.Disconnect();
    return alAnimals;
}

动物圈子

public Animal(int intAnimal_ID) throws Exception{
    CC_H2 db = new CC_H2();
    db.Connect(Variables.getStrConn(), "admin", "", "Embedded");
    ResultSet rs = db.query("Select * FROM tblHerd WHERE HERD_ID = "
            + intAnimal_ID);
    while (rs.next()) {
        setIntHerd_id(rs.getInt("Herd_ID"));
        setStrHerd_Tag_Letter(rs.getString("Herd_Tag_Letter"));
        setIntHerd_Tag_Num(rs.getInt("Herd_Tag_Num"));
        setStrHerd_Tag_Color(rs.getString("Herd_Tag_Color"));
        setStrHerd_Sex(rs.getString("Herd_Sex"));
        setStrHerd_Type(rs.getString("Herd_Type"));
        setDtHerd_Birthdate(rs.getDate("Herd_Birthdate"));
        setIntHerd_Sire(rs.getInt("Herd_Sire"));
        setIntHerd_Dam(rs.getInt("Herd_Dam"));
        setIntHerd_Owner(rs.getInt("Herd_Owner"));
        setDtHerd_TimeStamp(rs.getDate("Herd_TimeStamp"));
        setStrHerd_Status(rs.getString("Herd_Status"));
        setDtHerd_Status_Date(rs.getDate("Herd_Status_Date"));
    }
    db.Disconnect();
}
最佳回答

我认为,你所列举的法典应当像预期的那样发挥作用,这一错误很可能是其他地方。

  • Check the data in your database
  • Check the setters in the while loop do the right thing
  • Check that the fields in the Animal class are not static
  • If the problem persists, try to put something like System.out.println(rs.getInt("Herd_ID") + ": " + rs.getString("Herd_Tag_Letter")); into the where loop and check the output

希望其中一些帮助:-

问题回答

you have created array list static

仅从那里删除static





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签