0

I have a connection with ORACLE database and I have class (model). I put my database information in the list when I run a.Output()-I can see data. But when I want to use my list in a method it's showing nothing!

It's my model Class: From the database, I wanna get an only first name, last name and section name

public class Customer implements Comparable<Object> {

public String ime;
public String fam;
public String oime;

Customer(){}

Customer(String n, String f, String o) {
    ime = n;
    fam=f;
    oime=o;
}

public String getIme() {
    return ime;
}

public void setIme(String ime) {
    this.ime = ime;
}
public String getFam() {
    return fam;
}

public void setFam(String fam) {
    this.fam = fam;
}

public String getOime() {
    return oime;
}

public void setOime(String o) {
    oime = o;
}
 public int compareTo(Object a) {
     if(getOime().equals(((Customer)a).getOime())) 
         return -1;
     if(this.getOime().equals(((Customer)a).getOime()))
         return 1;
     return 0;
    }

    public boolean equals(Object o) {
    return (this.getOime()==((Customer)o).getOime());
    }
    public String toString() {
    return  "Ime="+ ime+" "+fam + ", Otdel = " + oime;
    }

    }

And here I have my main class: Here I have a connection with my DB and pushing in the list all information

   public class ConnectionDatabase {

   protected  List<Customer> warehouses = new ArrayList<Customer>();

   public static Connection connectDB() {
    Connection connect = null;
    try {
        String driver = "oracle.jdbc.driver.OracleDriver";
        Class.forName(driver);       
         connect = 

     DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", 
        "asd","pass");
    }
    catch(Exception e) {
        System.out.println(e);
    }
    return connect;
}

 ConnectionDatabase() {
    Connection connect1 = connectDB();
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
    String query = "SELECT firstname,lastname,sectionname FROM 
            staff,section";
        ps = connect1.prepareStatement(query);
        rs = ps.executeQuery();
        while(rs.next()) {
        warehouses.add(new 
   Customer(rs.getString("firstname"),
   rs.getString("lastname"),rs.getString("sectionname")));              
        }
    rs.close();
    connect1.close();

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

public void Output() {
    for(int i=0; i < warehouses.size(); i++){
        System.out.println(warehouses.get(i));
    }
}

public List<Customer> typeReference(String type) {
    Customer w1 = new Customer();
    List<Customer> w2 = new ArrayList<Customer>();
    for(int i=0; i < warehouses.size(); i++) {
        w1 =  warehouses.get(i);
        if(w1.getOime().equals(otdel))
        w2.add(w1);
    }
    return w2;      
}

public static void main(String args[]) {
ConnectionDatabase a=new ConnectionDatabase();
a.typeReference("Pool");
}
    }
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Joe
  • 11
  • 2
  • 5

1 Answers1

1

You need to override the Customer toString() method.

The default is the object memory location inherited from Object.

Cwrwhaf
  • 324
  • 3
  • 5