一、功能介绍
1、程序运行时,首先显示最近的5条新闻的标题(按照新闻的发布时间排序),然后提示操作菜单,运行效果图如下:
2、如果选择菜单A能显示所有的新闻标题(按照新闻的发布时间排序)后提示操作菜单,运行效果图如下:
3、如果选择菜单B则根据用户输入的新闻的编号来显示新闻的详细信息(新闻详细内容的显示格式不做要求)以及所有的新闻评论,然后提示操作菜单,运行效果图如下:
4、如果选择菜单C,则根据用户输入的新闻编号来添加评论,然后提示操作菜单,运行效果图如下:
5、选择D则结束程序。
数据库创建表
CREATE TABLE news(
nId INT PRIMARY KEY auto_increment,
nName VARCHAR(30),
nDate DATETIME,
nContent VARCHAR(100)
)
CREATE TABLE newcomment(
cId INT PRIMARY KEY auto_increment,
nComment VARCHAR(20),
cDate DATETIME,
nId INT,
FOREIGN KEY(nId) references news(nId)
)
MVC结构如下图所示:注意要先导入架包
Beans目录下的News类:
package newsTest.beans;
public class News {
private String nId;
private String nName;
private String nDate;
private String nContent;
public String getnId() {
return nId;
}
public void setnId(String nId) {
this.nId = nId;
}
public String getnName() {
return nName;
}
public void setnName(String nName) {
this.nName = nName;
}
public String getnDate() {
return nDate;
}
public void setnDate(String nDate) {
this.nDate = nDate;
}
public String getnContent() {
return nContent;
}
public void setnContent(String nContent) {
this.nContent = nContent;
}
@Override
public String toString() {
return "News{" +
"nId='" + nId + '\'' +
", nName='" + nName + '\'' +
", nDate='" + nDate + '\'' +
", nContent='" + nContent + '\'' +
'}';
}
}
NewsComment类:
package newsTest.beans;
public class NewsComment {
private int cId;
private String nComment;
private String cDate;
public int getcId() {
return cId;
}
public void setcId(int cId) {
this.cId = cId;
}
public String getnComment() {
return nComment;
}
public void setnComment(String nComment) {
this.nComment = nComment;
}
public String getcDate() {
return cDate;
}
public void setcDate(String cDate) {
this.cDate = cDate;
}
@Override
public String toString() {
return "NewsComment{" +
"cId=" + cId +
", nComment='" + nComment + '\'' +
", cDate='" + cDate + '\'' +
'}';
}
}
controller层内容:
package newsTest.controller;
import newsTest.beans.News;
import newsTest.beans.NewsComment;
import newsTest.model.NewsModel;
import newsTest.view.NewsView;
import java.util.List;
public class NewsController {
private static NewsView view = new NewsView();
private static NewsModel model = new NewsModel();
public static void main(String[] args) {
List<News> allNews = model.findFiveNews();
view.listNews(allNews);
while (true) {
//显示菜单
String result = view.showMenu();
//浏览所有新闻
if (result.equals("A")) {
allNews = model.findAllNews();
view.showAllNews(allNews);
}
//浏览新闻详细信息
if (result.equals("B")) {
String s = view.showOneNews();
for (News n:allNews) {
if (n.getnId().equals(s)) {
view.showDetail(n);
List<NewsComment> comm = model.showComment(n.getnId());
view.showComment(comm);
}
}
}
//发表评论
if (result.equals("C")) {
String num = view.addCommentNum();
String content = view.addComment();
model.addComment(content,num);
}
}
}
}
model层内容:
package newsTest.model;
import newsTest.beans.News;
import newsTest.beans.NewsComment;
import newsTest.utils.DBUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class NewsModel {
//查询新闻的方法
public List<News> findFiveNews() {
String sql = "select * from news where nId between 1 and 5";
return DBUtils.queryAllList(sql, News.class);
}
//显示所有新闻的方法
public List<News> findAllNews() {
String sql = "select nId,nName,nContent,nDate from news";
return DBUtils.queryAllList(sql, News.class);
}
//显示评论的方法
public List<NewsComment> showComment(String nId) {
String sql = "select * from newComment where nid =? ";
return DBUtils.queryAllList(sql, NewsComment.class,nId);
}
//添加评论
public int addComment(String s,String num) {
//获取系统时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置日期格式
String nowDate = df.format(new Date());
String sql = "insert into newComment values(null,?,?,?)";
return DBUtils.update(sql,s,nowDate,num);
}
}
view层内容:
package newsTest.model;
import newsTest.beans.News;
import newsTest.beans.NewsComment;
import newsTest.utils.DBUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class NewsModel {
//查询新闻的方法
public List<News> findFiveNews() {
String sql = "select * from news where nId between 1 and 5";
return DBUtils.queryAllList(sql, News.class);
}
//显示所有新闻的方法
public List<News> findAllNews() {
String sql = "select nId,nName,nContent,nDate from news";
return DBUtils.queryAllList(sql, News.class);
}
//显示评论的方法
public List<NewsComment> showComment(String nId) {
String sql = "select * from newComment where nid =? ";
return DBUtils.queryAllList(sql, NewsComment.class,nId);
}
//添加评论
public int addComment(String s,String num) {
//获取系统时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置日期格式
String nowDate = df.format(new Date());
String sql = "insert into newComment values(null,?,?,?)";
return DBUtils.update(sql,s,nowDate,num);
}
}
utils包下的DBUtils工具类:
package newsTest.utils;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public class DBUtils {
// 要连接的驱动类型
private static final String DRIVER = "com.mysql.jdbc.Driver";
// 要连接的数据库名
private static final String DATABASE = "news" ;
// 要连接的数据库的用户名
private static final String USER = "root";
// 要连接的数据库的密码
private static final String PWD = "root";
//要连接的URL
private static final String URL = "jdbc:mysql://127.0.0.1:3306/"+DATABASE+"?useUnicode=true&characterEncoding=UTF-8";
//dbutils中查询对象
private static QueryRunner runner ;
static{
DruidDataSource ds = new DruidDataSource();
ds.setUrl(URL);
ds.setUsername(USER);
ds.setPassword(PWD);
ds.setDriverClassName(DRIVER);
runner = new QueryRunner(ds);
}
/**
* 进行新增、修改、删除操作
* @param sql 进行操作的SQL语句
* @param param 操作对应的参数,替代SQL中的占位符
* @return SQL执行后受影响的行
*/
public static int update(String sql ,Object... param ){
try {
return runner.update(sql, param);
} catch (SQLException e) {
e.printStackTrace();
}
return 0 ;
}
/**
* 查询1个对象封装成Bean对象
* @param sql 进行操作的SQL语句
* @param clazz 结果封装的Bean类型
* @param param 操作对应的参数,替代SQL中的占位符
* @return SQL操作后的Bean对象,如果异常则返回null
*/
public static<T> T queryOneToBean(String sql, Class<T> clazz,Object... param ){
try {
BasicRowProcessor brp = new BasicRowProcessor(new GenerousBeanProcessor());
return runner.query(sql, new BeanHandler<T>(clazz,brp),param);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 查询只有1行结果的操作,结果封装成Map
* @param sql 进行操作的SQL语句
* @param param 操作对应的参数,替代SQL中的占位符
* @return SQL操作后的1行内容封装到Map中,如果异常则返回null
*/
public static Map<String,Object> queryOneToMap(String sql, Object... param ){
try {
return runner.query(sql,new MapHandler(),param);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 有多行结果的查询操作,结果封装成List,元素为bean类型
* @param sql sql 进行操作的SQL语句
* @param clazz 结果封装的Bean类型
* @param param 操作对应的参数,替代SQL中的占位符
* @return SQL操作后的多行内容封装到List中,如果异常则返回null
*/
public static<T> List<T> queryAllList(String sql, Class<T> clazz,Object... param ){
try {
BasicRowProcessor brp = new BasicRowProcessor(new GenerousBeanProcessor());
return runner.query(sql, new BeanListHandler<T>(clazz,brp),param);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 查询有多行结果的操作,结果封装成List中,元素为map类型
* @param sql 进行操作的SQL语句
* @param param 操作对应的参数,替代SQL中的占位符
* @return SQL操作后的多行内容封装到List中,如果异常则返回null
*/
public static List<Map<String,Object>> queryAllMap(String sql, Object... param ){
try {
return runner.query(sql,new MapListHandler(),param );
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}