话说
“ JavaWeb-JDBC-1~4-连接是页面显示的前奏”实现了从后台取数据,在前端显示,还差前端的增、删、改。
这里先做个总结。
之前写好的1-4再次做个梳理如下:
先看下整体布局
BaseDao(封装好的几个方法)代码如下:
package com.hmc.jdbc.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @Author Meice
* @Date 2017年8月6日
* 这个BaseDao版本也就是我们前面实现的BaseDaoV2版本
* 封装了这样几个功能:1 加载驱动 2 获取连接 3 释放资源 4 查 5 增、改、删
* 它是我们后期继续升级更新新闻系统的坚固基石,感谢它默默滴支持!
*/
public class BaseDao {
//这三个变量都要用,设置为全局变量
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
//定义加载驱动为静态语句块
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//封装获取连接方法
public Connection getConn() {
String url = "jdbc:mysql://localhost:3306/news_db";
String user = "root";
String password = "119913";
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("恭喜你,连接成功!");
return conn;
}
//封装关闭资源的方法
public void closeJDBC(ResultSet rs, PreparedStatement ps, Connection conn ) {
try {
if(rs != null) rs.close();
if(ps != null) ps.close();
if(conn != null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//定义整合增、改、删的封装方法executeUpdate()
public int executeUpdate(String sql, Object[] params) {
int result = 0;
conn = this.getConn();
try {
//预编译DQL语句
ps = conn.prepareStatement(sql);
//实际过程中,语句可能不设置参数,所以要做个判断 ,避免NullPointException错误
if(params != null && params.length>0) {
//遍历参数,并为其赋值
for(int i=0;i<params.length;i++) {
ps.setObject((i+1), params[i]);
}
}
//执行语句
result = ps.executeUpdate();
//[判断执行结果是否奏效,可写可不写]
if(result>0) {
System.out.println("执行CUD操作之一成功!");
} else {
System.out.println("执行CUD操作之一失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//因为在executeUpdate()方法中不存在ResultSet(),所以为null值。
this.closeJDBC(null, ps, conn);
}
return result;
}
//main方法测试
public static void main(String[] args) {
//测试连接数据库是否成功 测试成功!
//System.out.println(new BaseDao().getConn());
//[测试executeUpdate()方法] 测试成功!
/*String sql = "delete from t_news where id = ?";
Object[] params = {43};
new BaseDao().executeUpdate(sql, params);*/
}
}
JavaBean封装的业务代码如下:
package com.hmc.jdbc.model;
/**
* @Author Meice
* @Date 2017年8月6日
* 封装业务数据,也就是JavaBean的运用,映射数据库中数据表t_news
*/
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
+ "]";
}
}
NewDao如下(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月6日
* 在这个类里面,我们会实现由数据库到页面的跨越时空的转变。
*/
//继承BaseDao类,就可以方便调用我们已经封装好的方法
public class NewsDao extends BaseDao{
//定义集合list,存放由后台取出的数据,方便前段调用。
public List<News> list() {
//创建list集合
List<News> list = new ArrayList<>();
//连接数据库,从数据库获取数据
conn = this.getConn();
String sql = "select * from t_news";
try {
//执行预编译
ps = conn.prepareStatement(sql);
//执行SQL语句
rs = ps.executeQuery();
//遍历结果集
while(rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String author = rs.getString("author");
//把从数据库获取到的属性值用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集合取值是否成功] 成功!
//System.out.println(new NewsDao().list());
}
}
页面news_Show.jsp核心代码如下:
<%@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>
<%
//调用list方法获取list集合,便于在页面显示
NewsDao nd = new NewsDao();
List<News> list = nd.list();
%>
<!--这里暂且使用JSP方式显示,后期优化 -->
<body>
<table border="1" width="80%" align="center">
<caption><h2>Meice的新闻列表</h2></caption>
<thead>
<tr>
<th>编号</th>
<th>标题</th>
<th>作者</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<!-- JSP遍历集合,把每个news对象放到对应页面位置 -->
<%
for(News news : list) {
%>
<tr>
<td align="center"><%= news.getId() %></td>
<td><%= news.getTitle() %></td>
<td align="center"><%= news.getAuthor() %></td>
<td align="center">
<a href="#">修改</a> |
<a href="#">删除</a>
</td>
</tr>
<%
}
%>
</tbody>
<tfoot></tfoot>
</table>
</body>
</html>
最终效果如下:
总结
1 这里仅仅是梳理之前的1-4内容,并未增加新内容;
2 编写边测试,是个良好的习惯;
本文介绍了使用 JavaWeb 和 JDBC 实现数据的增删改查功能。通过 BaseDao 类封装了数据库连接、资源释放等通用操作,NewsDao 类实现了具体的数据查询逻辑。最后通过 JSP 页面展示了从数据库获取的新闻列表。
1791

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



