我与ORACLE数据库建立了连接,并且具有类(模型)。运行a.Output()时,我将数据库信息放在列表中,我可以看到数据。 但是,当我想在某种方法中使用列表时,什么也没显示!
这是我的模型班级:我想从数据库中获得唯一的名字,姓氏和节名
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;
}
}
在这里,我有我的主班:在这里,我与数据库建立了连接,并将所有信息推送到列表中
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");
}
}
答案 0 :(得分:1)
您需要重写Customer toString()方法。
默认值为从Object继承的对象存储位置。