Statement stmt = conn.createStatement();
Statement stmt1 = conn.createStatement();
String sql = "select * from topic order by Producedtime DESC";
ResultSet rs = stmt.executeQuery(sql);
List list = new ArrayList();
while (rs.next()) {
topic_index = rs.getInt("topic_index");
Topic topic = new Topic();
topic.setTopic_index(topic_index);
String sql1="select * from news where news_id="+topic_index;
ResultSet rs1 = stmt.executeQuery(sql1);
String news_content=rs1.getString("news_content");
topic.setTopic_news_content(news_content);
list.add(topic);
}
request.setAttribute("list", list);
rs.close();
stmt.close();
conn.close();
ResultSet rs1 = stmt.executeQuery(sql1);会关闭前面的rs
你是不是掉了个1,改成stmt1应该可以吧
你应该这样写:
Statement stmt = null;
ResultSet rs = null;
String sql = "select t.topic_index as tindex,n.news_content as content from topic t inner join news n on t.topic_index = n.news_id order by t.Producedtime DESC";
List list = new ArrayList();
try{
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
Topic topic = new Topic();
int index = rs.getInt("tindex");
String content=rs1.getString("content");
topic.setTopic_index(topic_index);
topic.setTopic_news_content(content);
list.add(topic);
}
}catch(Exception e){
System.out.print("excute sql error")
}finally{
rs.close();
stmt.close();
conn.close();
}
request.setAttribute("list", list);
上面写得有问题,这个才是对的:
Statement stmt = null;
ResultSet rs = null;
String sql = "select t.topic_index as tindex,n.news_content as content from topic t inner join news n on t.topic_index = n.news_id order by t.Producedtime DESC";
List list = new ArrayList();
try{
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
Topic topic = new Topic();
int index = rs.getInt("tindex");
String content=rs.getString("content");
topic.setTopic_index(topic_index);
topic.setTopic_news_content(content);
list.add(topic);
}
}catch(Exception e){
System.out.print("excute sql error")
}finally{
rs.close();
stmt.close();
conn.close();
}
request.setAttribute("list", list);