Java-list中两种不同的对象类型for each遍历时用Object类型,出现了乱码,这事什么意思
public class cartil {
private String owerid;
private String type;
private String buydate;
private String cph;
public String getOwerid() {
return owerid;
}
public void setOwerid(String owerid) {
this.owerid = owerid;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getBuydate() {
return buydate;
}
public void setBuydate(String date) {
this.buydate = date;
}
public String getCph() {
return cph;
}
public void setCph(String cph) {
this.cph = cph;
}
}
public class person {
private String hostname;
private String hostage;
private String hostid;
private String hostaddress;
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public String getHostage() {
return hostage;
}
public void setHostage(String hostage) {
this.hostage = hostage;
}
public String getHostid() {
return hostid;
}
public void setHostid(String hostid) {
this.hostid = hostid;
}
public String getHostaddress() {
return hostaddress;
}
public void setHostaddress(String hostaddress) {
this.hostaddress = hostaddress;
}
}
public List selectallbycph(String cph){
List list=new ArrayList();
conn=DBUtil.getconn();
try {
psmt=conn.prepareStatement("select * from modelcar inner join host on host.hostid=modelcar.owerid where cph=?");
psmt.setString(1, cph);
res=psmt.executeQuery();
if(res.next()){
//System.out.print("jsafhjafjas");
cartil c=new cartil();
person p=new person();
c.setBuydate(res.getString("buydate"));
c.setCph(res.getString("cph"));
c.setOwerid(res.getString("owerid"));
c.setType(res.getString("type"));
p.setHostid(res.getString("hostid"));
p.setHostname(res.getString("hostname"));
p.setHostage(res.getString("hostage"));
p.setHostaddress(res.getString("hostaddress"));
list.add(c);
list.add(p);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
DefaultTableModel dtm = new DefaultTableModel();
dtm.addColumn("车牌号");
dtm.addColumn("证件号");
dtm.addColumn("车型");
dtm.addColumn("购买日期");
dtm.addColumn("用户身份证号");
dtm.addColumn("用户姓名");
dtm.addColumn("用户年龄");
dtm.addColumn("用户地址");
cardao cd = new cardao();
List c = (List) cd.selectallbycph(jTextField1.getText());
Vector v = new Vector();
for(Object s :c){
v.add(s);
}
//v.add(c);
dtm.addRow(v);
jTable1.setModel(dtm);
this.initable();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
18行断点试一下,从list取出来的Ojbect是Object类型
要或者你试试instanceof判断下类型?
重新看了一下问题, 传入的是一个对象实例, 所以给出dao.person@b6c6a2 这种. 这个不叫乱码啊...
java.lang.Object.toString():
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
如果想得到有意义的数据, 覆写Object的toString()方法.