我在List里面放入对象,可是取每个对象的Id属性得到的都是一样的值,怎么回事??以下是部分代码,请高手帮忙

2024-11-09 20:54:24
推荐回答(5个)
回答1:

rs = ps.executeQuery(sql);

你这括号里怎么还有sql呢,把他去掉.
给你个参考的代码
public List findAll() throws Exception {
List studentList=new ArrayList();
// 涓庢暟鎹簱寤虹珛杩炴帴
conn=UtilDatabase.getConnection();
// sql锻戒护
String sql="select * from stu_stuInfo";
stmt=conn.prepareStatement(sql);
// 镓цstmt骞惰繑锲沥st缁撴灉板?
ResultSet rst=stmt.executeQuery();
// 阆嶅巻srt缁撴灉板? while(rst.next()){
// 鎶婇亶铡嗗缑鍒扮殑鍊煎~鍏呭埌瀹炰綋绫讳腑
student=new StudentsInfo();
student.setId(rst.getInt("id"));
student.setStuAddress(rst.getString("stuAddress"));
student.setStuAge(rst.getInt("stuAge"));
student.setStuName(rst.getString("stuName"));
student.setStuNum(rst.getInt("stuNum"));
student.setStuPwd(rst.getString("stuPwd"));
student.setStuSex(rst.getString("stuSex"));
studentList.add(student);
}
UtilDatabase.close(conn);
return studentList;
}

回答2:

time_t t = 0;char day[20] = {0};t = time(0);//获取系统时间,此时t存放的是系统时间的秒值(从1970年1月1日0时开始到当前时间)strftime (day, sizeof(day), "%Y-%m-%d %H:%M:%S", gmtime (&t)); //转换为字符串格式,这里的例子是 年-月-日 时:分:秒这里给出的是Linux下的例子,需要包含头文件#include 。如果是在windows下,你可以自己找找相应的头文件即可下面是MSDN里关于时间函数的示例,仔细看几遍,相信你以后对时间操作的问题就不会抓瞎了#include #include #include #include #include void main(){ char tmpbuf[128], ampm[] = "AM"; time_t ltime; struct _timeb tstruct; struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 }; /* Set time zone from TZ environment variable. If TZ is not set, * the operating system is queried to obtain the default value * for the variable. */ _tzset(); /* Display operating system-style date and time. */ _strtime( tmpbuf ); printf( "OS time:\t\t\t\t%s\n", tmpbuf ); _strdate( tmpbuf ); printf( "OS date:\t\t\t\t%s\n", tmpbuf ); /* Get UNIX-style time and display as number and string. */ time( tm_hour > 12 ) { strcpy( ampm, "PM" ); today->tm_hour -= 12; } if( today->tm_hour == 0 ) /* Adjust if midnight hour. */ today->tm_hour = 12; /* Note how pointer addition is used to skip the first 11 * characters and printf is used to trim off terminating * characters. */ printf( "12-hour time:\t\t\t\t%.8s %s\n", asctime( today ) + 11, ampm ); /* Print additional time information. */ _ftime( &tstruct ); printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm ); printf( "Zone difference in seconds from UTC:\t%u\n", tstruct.timezone ); printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] ); printf( "Daylight savings:\t\t\t%s\n", tstruct.dstflag ? "YES" : "NO" ); /* Make time for noon on Christmas, 1993. */ if( mktime( &xmas ) != (time_t)-1 ) printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) ); /* Use time structure to build a customized time string. */ today = localtime(

回答3:

把Category c = new Category();写到循环里面去就好了!
while (rs.next()) {
Category c = new Category();

c.setcId(rs.getInt("cId"));
System.out.println("ci Dao===="+rs.getInt("cId"));
c.setName(rs.getString("name"));
c.setDescribes(rs.getString("describes"));
c.setContentTime(rs.getString("contentTime"));
categoryList.add(c);
}

回答4:

Category c = new Category();

这一句放到while循环里面去
否则只创建了一个对象
所有的操作都是对这一个对象进行的
所以只有最后一次循环生效了

必须在每次循环的时候new一个对象才可以

回答5:

怎么看只有一个对象 把这个 Category c = new Category();放到while里面吧