在java中将数据库里面的数据r如何封装到一个集合里面,菜鸟求大神指点迷津(集合的元素是dao对象 )
为什么在java中将数据库里面的数据这样封装到一个集合里面,集合里面的元素都是数据库最后一行的数据呢?菜鸟求大神指点迷津(集合的元素是dao对象)publicList<S...
为什么在java中将数据库里面的数据这样封装到一个集合里面,集合里面的元素都是数据库最后一行的数据呢?菜鸟求大神指点迷津(集合的元素是dao对象 )
public List<Subject> subjectList() {
Subject subject = new Subject();
List<Subject> subjectList = new ArrayList<Subject>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 建立连接;
conn = JdbcUtilsSingle.getInstance().getConnection();
// 声明语句;
String sql = "select * from subjects ";
ps = conn.prepareStatement(sql);
// 执行语句;
rs = ps.executeQuery();
while (rs.next()) {
subject.setSubjectID(rs.getString("subjectID"));
subject.setSubjectTitle(rs.getString("subjectTitle"));
subject.setSubjectOptionA(rs.getString("subjectOptionA"));
subject.setSubjectOptionB(rs.getString("subjectOptionB"));
subject.setSubjectOptionC(rs.getString("subjectOptionC"));
subject.setSubjectOptionD(rs.getString("subjectOptionD"));
subject.setSubjectAnswer(rs.getString("subjectAnswer"));
subject.setSubjectParse(rs.getString("subjectParse"));
subjectList.add(subject);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException("数据库异常!可能是读出异常!");
} finally {
JdbcUtilsSingle.getInstance().free(conn, ps, rs);
}
return subjectList;
} 展开
public List<Subject> subjectList() {
Subject subject = new Subject();
List<Subject> subjectList = new ArrayList<Subject>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 建立连接;
conn = JdbcUtilsSingle.getInstance().getConnection();
// 声明语句;
String sql = "select * from subjects ";
ps = conn.prepareStatement(sql);
// 执行语句;
rs = ps.executeQuery();
while (rs.next()) {
subject.setSubjectID(rs.getString("subjectID"));
subject.setSubjectTitle(rs.getString("subjectTitle"));
subject.setSubjectOptionA(rs.getString("subjectOptionA"));
subject.setSubjectOptionB(rs.getString("subjectOptionB"));
subject.setSubjectOptionC(rs.getString("subjectOptionC"));
subject.setSubjectOptionD(rs.getString("subjectOptionD"));
subject.setSubjectAnswer(rs.getString("subjectAnswer"));
subject.setSubjectParse(rs.getString("subjectParse"));
subjectList.add(subject);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException("数据库异常!可能是读出异常!");
} finally {
JdbcUtilsSingle.getInstance().free(conn, ps, rs);
}
return subjectList;
} 展开
2个回答
展开全部
while (rs.next()) {
Subject subject = new Subject();
subject.setSubjectID(rs.getString("subjectID"));
subject.setSubjectTitle(rs.getString("subjectTitle"));
subject.setSubjectOptionA(rs.getString("subjectOptionA"));
subject.setSubjectOptionB(rs.getString("subjectOptionB"));
subject.setSubjectOptionC(rs.getString("subjectOptionC"));
subject.setSubjectOptionD(rs.getString("subjectOptionD"));
subject.setSubjectAnswer(rs.getString("subjectAnswer"));
subject.setSubjectParse(rs.getString("subjectParse"));
subjectList.add(subject);
}
这样就可以了。你应该能明白吧。要每回new一个新的。
追问
public List subjectList() {
Subject subject = new Subject();
List subjectList = new ArrayList();
刚才是不该。。。可是移上去了结果也一样,怎么回事?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询