同一段代码不同输出结果

whhitstudent 2009-10-19 11:36:11
Consumables cyp = clservice.findByName("mining", "柴油", "2009","10");
Consumables qyp = clservice.findByName("mining", "汽油", "2009","10");
System.out.println("cyp.price"+cyp.getPrice());
System.out.println("qyp.price"+qyp.getPrice());

Consumables cyp = clservice.findByName("mining", "柴油", "2009","10");
System.out.println("cyp.price"+cyp.getPrice());
Consumables qyp = clservice.findByName("mining", "汽油", "2009","10");
System.out.println("qyp.price"+qyp.getPrice());

前者输出
20
20
后者输出:
30
20
为什么呢?
...全文
281 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
whhitstudent 2009-10-28
  • 打赏
  • 举报
回复
使用局部变量mrservice把问题解决了,谢谢大家
弘石 2009-10-21
  • 打赏
  • 举报
回复
菜鸟在改别人的代码???
gukuitian 2009-10-21
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 whhitstudent 的回复:]
数据库是20和30没错啊..
[/Quote]
那你输出的是什么?
whhitstudent 2009-10-21
  • 打赏
  • 举报
回复
数据库是20和30没错啊..
downice 2009-10-20
  • 打赏
  • 举报
回复
又一个全局变量使用不当的例子!!!!有可能被方法修改的数据,都用局部变量吧。
yuzhangchen 2009-10-20
  • 打赏
  • 举报
回复
Consumables u = new Consumables() 是在 clservice 这个类里声明的,这个类只有一个实例,所以会出现上面的情况。
whhitstudent 2009-10-20
  • 打赏
  • 举报
回复
楼上的输出结果:
0
0
一洽客服系统 2009-10-20
  • 打赏
  • 举报
回复
汽油 20 柴油 30 很明显的问题 按楼上说的做~
gukuitian 2009-10-20
  • 打赏
  • 举报
回复


public Consumables findByName(String dep, String cname,String year,String month) {
// TODO Auto-generated method stub
Consumables u = new Consumables();//这样会有空指针?不可能吧
Session session = HbnUtil.getSession();
Connection connection = session.connection();

Statement stmt = null;
ResultSet rs = null;
// System.out.println("dep="+dep+" cname="+cname+" year="+year+" month="+month);
String sql = "select * from Consumables_t where z_dep='"+dep+"' and z_cname='"+cname+"' and z_year='"+year+"' and z_month='"+month+"' order by z_id desc";
try {
stmt = connection.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
u.setId(rs.getLong(1));
u.setConsumablesModel(rs.getString(2));
u.setConsumablesName(rs.getString(3));
u.setYear(rs.getString(4));
u.setMonth(rs.getString(5));
u.setDepartment(rs.getString(6));
u.setPrice(rs.getDouble(7));
u.setUnit(rs.getString(8));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
HbnUtil.release(rs, stmt, connection, session);
}
return u;}

gukuitian 2009-10-20
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 whhitstudent 的回复:]
定义在方法内,空指针异常啊//...
[/Quote]
你没new()?


public Consumables findByName(String dep, String cname,String year,String month) {
// TODO Auto-generated method stub
Consumables u = new Consumables();
Session session = HbnUtil.getSession();
Connection connection = session.connection();

Statement stmt = null;
ResultSet rs = null;
// System.out.println("dep="+dep+" cname="+cname+" year="+year+" month="+month);
String sql = "select * from Consumables_t where z_dep='"+dep+"' and z_cname='"+cname+"' and z_year='"+year+"' and z_month='"+month+"' order by z_id desc";
try {
stmt = connection.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
u.setId(rs.getLong(1));
u.setConsumablesModel(rs.getString(2));
u.setConsumablesName(rs.getString(3));
u.setYear(rs.getString(4));
u.setMonth(rs.getString(5));
u.setDepartment(rs.getString(6));
u.setPrice(rs.getDouble(7));
u.setUnit(rs.getString(8));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
HbnUtil.release(rs, stmt, connection, session);
}
return u;}
whhitstudent 2009-10-20
  • 打赏
  • 举报
回复
定义在方法内,空指针异常啊//...
gukuitian 2009-10-20
  • 打赏
  • 举报
回复
问题出在findName方法,你的返回值u,注意,这个u是在方法外new()的,连续两回调用findName方法返回的是一个Consumables 对像,只是修改了属性,
应该把u定义在方法内,
lxxzhy 2009-10-20
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 whhitstudent 的回复:]
恩啊,是全局变量的问题,我按20楼的写了还是没法得我要的结果,大家给个解决代码吧,^_^
[/Quote]不可能吧,你检查下数据库的数据是否变了.
whhitstudent 2009-10-20
  • 打赏
  • 举报
回复
Consumables类:
package com.hxsmelt.model.entity;

import java.io.Serializable;

public class Consumables implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private long id;
private String consumablesModel;
private String consumablesName;
private String unit;
private double price;
private String year;
private String month;
private String department;
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public Consumables() {
super();
// TODO Auto-generated constructor stub
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (id ^ (id >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Consumables other = (Consumables) obj;
if (id != other.id)
return false;
return true;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getConsumablesModel() {
return consumablesModel;
}
public void setConsumablesModel(String consumablesModel) {
this.consumablesModel = consumablesModel;
}
public String getConsumablesName() {
return consumablesName;
}
public void setConsumablesName(String consumablesName) {
this.consumablesName = consumablesName;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}

findByName方法:
private Consumables u = new Consumables();
public Consumables findByName(String dep, String cname,String year,String month) {
// TODO Auto-generated method stub
Session session = HbnUtil.getSession();
Connection connection = session.connection();
Statement stmt = null;
ResultSet rs = null;
// System.out.println("dep="+dep+" cname="+cname+" year="+year+" month="+month);
String sql = "select * from Consumables_t where z_dep='"+dep+"' and z_cname='"+cname+"' and z_year='"+year+"' and z_month='"+month+"' order by z_id desc";
try {
stmt = connection.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
u.setId(rs.getLong(1));
u.setConsumablesModel(rs.getString(2));
u.setConsumablesName(rs.getString(3));
u.setYear(rs.getString(4));
u.setMonth(rs.getString(5));
u.setDepartment(rs.getString(6));
u.setPrice(rs.getDouble(7));
u.setUnit(rs.getString(8));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
HbnUtil.release(rs, stmt, connection, session);
}
return u;}
whhitstudent 2009-10-20
  • 打赏
  • 举报
回复
恩啊,是全局变量的问题,我按20楼的写了还是没法得我要的结果,大家给个解决代码吧,^_^
AldisZhan 2009-10-19
  • 打赏
  • 举报
回复
代码都舍不得贴
我看结果应为
cyp.price 20
qyp.price 20

cyp.price 20
qyp.price 20

多贴点不就好了 运行结果也要是 Ctrl+C 所有的结果啊
yy357510564 2009-10-19
  • 打赏
  • 举报
回复
那么点代码误倒啊
swandragon 2009-10-19
  • 打赏
  • 举报
回复
Consumables 怎么定义的
findByName怎么写的?getPrice怎么写的?
gzbtiantian 2009-10-19
  • 打赏
  • 举报
回复
楼主给的代码 太少了。什么可能都有啊。最后多贴点。
zl3450341 2009-10-19
  • 打赏
  • 举报
回复
冰山一角难窥全貌
加载更多回复(10)

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧