思路
如何通过网页查询数据库中的数据
1 在各个层创建查询方法
2jsp页调用该方法
web端
<tbody>
<%
List<News> newsList=newsService.getNewsList();
int i=0;
for(News news : newsList){
i++;
%>
<tr <%if(i%2==0){
%>class="admin-list-td-h2"<% }%>>
<td><a href='newsDetailView.jsp?id=<%=news.getId()%>'><%= news.getTitle()%></a></td>
<td><%=news.getAuthor() %></td>
<td><%=news.getCreateDate() %></td>
<td><a href='update.jsp?id=2'>修改</a>
<a href="javascript:if(confirm('确认是否删除此新闻?')) location='adminNewsDel.jsp?id=2'">删除</a>
</td>
</tr>
<% }
%>
</tbody>
Service层
public interface NewsService {
//
public List<News> getList();
service impl 层
//实现 查询 返回对象是集合
@Override
public List<News> getList() {
// TODO Auto-generated method stub
//调用dao层的方法 ,要想到创建对象
//NewsDao newsDao=new NewsDaoImpl();
return newsDao.getList() ;
}
Dao层
//查询全部信息
public List<News> getList();
Dao的实现层
//查询全部信息
@Override
public List <News> getList() {
List<News> list=new ArrayList<News>();
// TODO Auto-generated method stub
String sql=”select id, categoryId,title,summary, content,picPath,author,createDate,modifyDate from news_detail”;
//初始化 数组 ,避免空指针
Object[] params={};
rs=this.ExecuteQuery(sql, params);
//在控制台输出 rs的结果集
try {
while(rs.next()){
int id=rs.getInt(1);
int categoryId=rs.getInt(2);
String title=rs.getString(3);
String summary=rs.getString(4);
String content=rs.getString(5);
String picPath=rs.getString(6);
String author=rs.getString(7);
Date createDate=rs.getDate(8);
Date modifyDate=rs.getDate(9);
//System.out.println(id+”\t”+categoryId+”\t”+title+”\t”+summary+”\t”+content+”\t”+picPath+”\t”+author+”\t”+createDate+”\t”+modifyDate );
//将获得的值 放入 对象 News中
News news=new News();
news.setId(id);
news.setCategoryId(categoryId);
news.setTitle(title);
news.setSummary(summary);
news.setContent(content);
news.setPicPath(picPath);
news.setAuthor(author);
news.setCreateDate(createDate);
//将对象放入集合中
list.add(news);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeResource();
}
return list;
}