话说
前面1-3已经实现了Java与数据库的交互,好戏只是刚刚开始,所有的连接都是为了前段的“绽放”!
在前面BaseDao基础上,还需要三步走,就可以在网页显示数据库内容,而不仅仅只是在控制台打印。
先看下笔者Web项目结构:
一、创建News.java类(JavaBean)
Bean是豆子滴意思,JavaBean就是Java类中一粒神奇的“豆子”。
News.java类完全根据数据库中t_news数据表的字段类设置属性。这是JavaBean的核心,在下认为如此。代码如下:
package com.hmc.jdbc.model;
/**
* @Author Meice
* @Date 2017年8月5日
*/
public class News {
private int id;
private String title;
private String author;
public News() {}
public News(int id, String title, String author) {
super();
this.id = id;
this.title = title;
this.author = author;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", author=" + author
+ "]";
}
}
二、创建NewsDao类,写一个方法返回集合list,存放数据库中的新闻信息,为页面输出做准备。
package com.hmc.jdbc.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.hmc.jdbc.model.News;
/**
* @Author Meice
* @Date 2017年8月5日
*/
public class NewsDao extends BaseDao {
//定义list方法,目的就是把从数据库中取得的数据全部存放到list中,方便给页面传值
public List<News> list () {
List<News> list = new ArrayList<>();
this.getConn();
String sql = "select * from t_news";
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String author = rs.getString("author");
//下面这行代码仅仅是为了测试前面写的是否正确(获取第二列的内容,第二列为title)
//System.out.println(rs.getString(2));
/**
* 这样每次遍历,每条新闻就是一个对象,并且初始化了参数,从此以后,数据
* 成功从数据库和对象搭建起桥梁,JavaBean发挥至关重要的作用
*/
News news = new News(id, title, author);
list.add(news);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
//配合list方法中途测试
//new NewsDao().list();
}
}
三、创建前端页面,显示数据。
<%@page import="com.hmc.jdbc.model.News"%>
<%@page import="com.hmc.jdbc.dao.NewsDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>新闻页面显示</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
NewsDao newsDao = new NewsDao();
List<News> list = newsDao.list();
//以下代码是为了测试
/* for(News news:list) {
out.println(news);
}
*/
%>
<table border="1" width="80%" align="center">
<caption>新闻列表</caption>
<thead>
<tr>
<th>编号</th>
<th>标题</th>
<th>作者</th>
</tr>
</thead>
<tbody>
<%for(News news : list) { %>
<tr>
<td><%=news.getId() %></td>
<td><%=news.getTitle() %></td>
<td><%=news.getAuthor() %></td>
</tr>
<%} %>
</tbody>
<tfoot>
</tfoot>
</table>
</body>
</html>
最终效果如下:
总结
1 前端页面显示的过程是一个流程化的过程:从数据库获取数据–》构建业务类–》存放到list集合中–》页面从list集合中获取数据
2 在下认为里面最核心的代码就是News news = new News(id,title,author) 我们在new的时候,就把从数据库rs.get方法获取的数据全部传递给了News类,也就是说实现了数据库表到对象的一个跨时代的巨变!每一条新闻信息摇身一变成了一个个具体的对象。既然对象有了,对象又是根据表的属性来设置的,那么调用属性就易如反掌啦。
3 现在还是用古老的JSP在页面显示和遍历,后续再慢慢升级。有时候,一蹴而就并非是高效。
4 深刻体会了JSP在页面的不灵活,为后期的JSTL粉墨登场埋下伏笔。
5 接下来,在页面实现增删改查;实现分页,我们拭目以待!
本文介绍如何使用Java与数据库交互,通过创建News JavaBean类映射数据库表,NewsDao类实现数据查询并返回List集合,最后在JSP页面展示新闻列表。
1767

被折叠的 条评论
为什么被折叠?



