JavaEE学习总结(十四)— 人工智能微博
一、数据库与表
人工智能微博(blog)
note(id,bt,nr);
微博信息(编号,标题,内容)
列表
添加
数据库脚本
- /*
- Navicat MySQL Data Transfer
- Source Server : localhost
- Source Server Version : 50506
- Source Host : localhost:3306
- Source Database : blog
- Target Server Type : MYSQL
- Target Server Version : 50506
- File Encoding : 65001
- Date: 2017-07-13 08:58:01
- */
- SET FOREIGN_KEY_CHECKS=0;
- -- ----------------------------
- -- Table structure for `wb`
- -- ----------------------------
- DROP TABLE IF EXISTS `wb`;
- CREATE TABLE `wb` (
- `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
- `bt` varchar(128) NOT NULL COMMENT '标题',
- `nr` text COMMENT '内容',
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of wb
- -- ----------------------------
- INSERT INTO `wb` VALUES ('', '微软建立新人工智能实验室 开发多用途学习系统', '微软公司凤凰科技讯据彭博社北京时间7月12日报道,微软公司将建立一个新研究实验室,专注于人工智能(AI),目标是开发出更为多用途的学习系统');
- INSERT INTO `wb` VALUES ('', '人工智能来袭 金融行业迎来下岗潮', '“在金融圈和用户教育上,对人工智能还未完全信任之前,我们只能采取人肉智能(HI)和人工智能(AI)相结合的方式”,朱明杰称,他们会机器先出一些模型,在专业的风控从');
- INSERT INTO `wb` VALUES ('', '即便取代脑力劳动AI还不能“打动”人心', '但AI革命不同, AI取代脑力劳动,将让人类转向创造性、情感性劳动,这些素质是每一个体都具有的。 事实上,人工智能导致失业,还属于一种弱人工智能观念。有人煞有介');
- INSERT INTO `wb` VALUES ('', 'Google 设立风投公司扶持 AI 新创团队', 'Google 在 AI 上的野心人尽皆知,但要让行业更好地发展,光有他们自己的力量是不够的。因此在最近他们新成立了一间名为 Gradient');
- INSERT INTO `wb` VALUES ('', 'AI来临,失业大势难以避免', 'AI来临,我们需要担心失业吗? 据斯坦福大学人工智能与伦理学教授卡普兰的一项统计,美国注册在案的720个职业中,将有47%被人工智能取代。未来10年机器人将取代1500万');
二、创建项目
三、添加驱动
四、创建实体层(Bean)
- package com.weibo;
- /**微博Bean*/
- public class Wb {
- /**编号*/
- private int id;
- /**标题*/
- private String bt;
- /**内容*/
- private String nr;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getBt() {
- return bt;
- }
- public void setBt(String bt) {
- this.bt = bt;
- }
- public String getNr() {
- return nr;
- }
- public void setNr(String nr) {
- this.nr = nr;
- }
- }
五、创建数据访问层(Dao)
5.1、JDBCUtil辅助类
- package com.dao;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class JDBCUtils {
- public static String DRIVER="com.mysql.jdbc.Driver";
- public static String URL="jdbc:mysql://127.0.0.1:3306/blog?useUnicode=true&characterEncoding=UTF-8";
- public static String USER_NAME="root";
- public static String PASSWORD="";
- //加载驱动
- static {
- try {
- Class.forName(DRIVER);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- private JDBCUtils() {
- }
- /**
- * 获得连接
- *
- * @return
- */
- public static Connection getconnnection() {
- Connection con = null;
- try {
- con = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return con;
- }
- /**
- * 关闭连接
- *
- * @param rs
- * @param st
- * @param con
- */
- public static void close(ResultSet rs, Statement st, Connection con) {
- try {
- try {
- if (rs != null) {
- rs.close();
- }
- } finally {
- try {
- if (st != null) {
- st.close();
- }
- } finally {
- if (con != null)
- con.close();
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- /**
- * 关闭连接
- *
- * @param rs
- */
- public static void close(ResultSet rs) {
- Statement st = null;
- Connection con = null;
- try {
- try {
- if (rs != null) {
- st = rs.getStatement();
- rs.close();
- }
- } finally {
- try {
- if (st != null) {
- con = st.getConnection();
- st.close();
- }
- } finally {
- if (con != null) {
- con.close();
- }
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- /**
- * 关闭连接
- *
- * @param st
- * @param con
- */
- public static void close(Statement st, Connection con) {
- try {
- try {
- if (st != null) {
- st.close();
- }
- } finally {
- if (con != null)
- con.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- /**
- * insert/update/delete
- * 增加/更新/删除
- *
- * @param sql 数据库语句
- * @param args 可变参数(可以不带参数,可以带0-n个参数)
- * @return
- */
- public static int update(String sql, Object... args) {
- int result = 0;
- Connection con = getconnnection();
- PreparedStatement ps = null;
- try {
- ps = con.prepareStatement(sql);
- if (args != null) {
- for (int i = 0; i < args.length; i++) {
- ps.setObject((i + 1), args[i]);
- }
- }
- result = ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- close(ps, con);
- }
- return result;
- }
- /**
- * query, because need to manually close the resource, so not recommended
- * for use it
- *
- * @param sql
- * @param args
- * @return ResultSet
- */
- @Deprecated //注解
- public static ResultSet query(String sql, Object... args) {
- ResultSet result = null;
- Connection con = getconnnection();
- PreparedStatement ps = null;
- try {
- ps = con.prepareStatement(sql);
- if (args != null) {
- for (int i = 0; i < args.length; i++) {
- ps.setObject((i + 1), args[i]);
- }
- }
- result = ps.executeQuery();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return result;
- }
- /**
- * Query a single record
- * 查询单个记录
- * @param sql
- * @param args
- * @return Map<String,Object>
- */
- public static Map<String, Object> queryForMap(String sql, Object... args) {
- Map<String, Object> result = new HashMap<String, Object>();
- List<Map<String, Object>> list = queryForList(sql, args);
- if (list.size() > 0) {
- result = list.get(0);
- }
- return result;
- }
- /**
- * Query a single record
- * 查询单个记录返回强类型对象
- * @param sql
- * @param args
- * @return <T> //泛型
- */
- public static <T> T queryForObject(String sql, Class<T> clz, Object... args) {
- T result = null;
- List<T> list = queryForList(sql, clz, args);
- if (list.size() > 0) {
- result = list.get(0);
- }
- return result;
- }
- /**
- * Query a single record
- *
- * @param sql
- * @param args
- * @return List<Map<String,Object>>
- */
- public static List<Map<String, Object>> queryForList(String sql, Object... args) {
- List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
- Connection con = null;
- ResultSet rs = null;
- PreparedStatement ps = null;
- try {
- con = getconnnection();
- ps = con.prepareStatement(sql);
- if (args != null) {
- for (int i = 0; i < args.length; i++) {
- ps.setObject((i + 1), args[i]);
- }
- }
- rs = ps.executeQuery();
- ResultSetMetaData rsmd = rs.getMetaData();
- int columnCount = rsmd.getColumnCount();
- while (rs.next()) {
- Map<String, Object> map = new HashMap<String, Object>();
- for (int i = 1; i <= columnCount; i++) {
- map.put(rsmd.getColumnLabel(i), rs.getObject(i));
- }
- result.add(map);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- close(rs, ps, con);
- }
- return result;
- }
- /**
- * Query records
- * 查询多个对象,返回强类型集合
- * @param sql
- * @param args
- * @return List<T>
- */
- public static <T> List<T> queryForList(String sql, Class<T> clz, Object... args) {
- List<T> result = new ArrayList<T>();
- Connection con = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- con = getconnnection();
- ps = con.prepareStatement(sql);
- if (args != null) {
- for (int i = 0; i < args.length; i++) {
- ps.setObject((i + 1), args[i]);
- }
- }
- rs = ps.executeQuery();
- ResultSetMetaData rsmd = rs.getMetaData();
- int columnCount = rsmd.getColumnCount();
- while (rs.next()) {
- T obj = clz.newInstance();
- for (int i = 1; i <= columnCount; i++) {
- String columnName = rsmd.getColumnName(i);
- String methodName = "set" + columnName.substring(0, 1).toUpperCase()
- + columnName.substring(1, columnName.length());
- Method method[] = clz.getMethods();
- for (Method meth : method) {
- if (methodName.equals(meth.getName())) {
- meth.invoke(obj, rs.getObject(i));
- }
- }
- }
- result.add(obj);
- }
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- } finally {
- close(rs, ps, con);
- }
- return result;
- }
- }
5.2、WbDao
- package com.dao;
- import java.util.List;
- import com.weibo.Wb;
- /**微博数据访问*/
- public class WbDao {
- /**获得所有的微博信息*/
- public List<Wb> all(){
- return JDBCUtils.queryForList("select * from wb;", Wb.class);
- }
- /**添加单条微博信息*/
- public int add(String bt,String nr){
- return JDBCUtils.update("insert into wb(bt,nr) values(?,?);", bt,nr);
- }
- }
六、展示微博列表
index.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@page import="com.weibo.Wb"%>
- <%@page import="com.dao.WbDao"%>
- <%
- //实例化数据访问对象
- WbDao dao=new WbDao();
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>人工智能微博</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- </head>
- <body>
- <h2>人工智能微博</h2>
- <ul>
- <%for(Wb obj : dao.all()){ %>
- <li><%=obj.getBt() %> <%=obj.getNr() %></li>
- <%} %>
- </ul>
- </body>
- </html>
运行效果:
七、添加新内容
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>人工智能微博</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- </head>
- <body>
- <h2>人工智能微博 - 发布</h2>
- <form action="Add" method="Post">
- <p>
- 标题:<input name="bt" />
- </p>
- <p>
- 内容:<textarea name="bt" cols="50" rows="5"></textarea>
- </p>
- <p>
- <input type="submit" value="保存" />
- </p>
- </form>
- </body>
- </html>
效果:
Add Servlet
- package com.action;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.dao.WbDao;
- public class Add extends HttpServlet {
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- //设置响应编码为utf-8
- response.setCharacterEncoding("utf-8");
- //设置请求编码为utf-8
- request.setCharacterEncoding("utf-8");
- //实例化数据访问对象
- WbDao dao=new WbDao();
- //执行添加
- dao.add(request.getParameter("bt"), request.getParameter("nr"));
- //转到首页
- response.sendRedirect("index.jsp");
- }
- }
八、资料与练习
电子书管理系统(rebot) 120分钟
要求:请使用JavaEE实现一个电子书管理系统,电子书的属性主要包含:标题(title)、页数(page)、类型(category),系统可以选择人工智能、计算机、文学、神话、自然科学。
1、 创建数据库Lib与表book,添加3行以上的数据,导出sql脚本。20分
2、 创建动态Java Web项目,添加数据库驱动程序。20分
3、 实现电子书展示功能,index.jsp。20分
4、 实现电子书添加功能,add.jsp。30分
5、 实现展示与添加功能间的跳转。5分
6、 代码规范,注释完整,命名合理,分层开发,界面美观。5分
将数据库脚本(导出)、JavaWeb项目、已实现功能的截图、项目截图打包成压缩文件,命名:班级_姓名,如:S2SR136_张三
示例下载:http://files.cnblogs.com/files/best/weibo.zip
九、人工智能微博二
9.1、wz bean
- package com.weibo.bean;
- /***
- * 文章
- */
- public class Wz {
- /** 编号 */
- private int bh;
- /** 标题 */
- private String bt;
- /** 内容 */
- private String nr;
- public int getBh() {
- return bh;
- }
- public void setBh(int bh) {
- this.bh = bh;
- }
- public String getBt() {
- return bt;
- }
- public void setBt(String bt) {
- this.bt = bt;
- }
- public String getNr() {
- return nr;
- }
- public void setNr(String nr) {
- this.nr = nr;
- }
- }
9.2、JDBCUtil
- package com.weibo.dao;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class JDBCUtils {
- public static String DRIVER="com.mysql.jdbc.Driver";
- public static String URL="jdbc:mysql://127.0.0.1:3306/weibo?useUnicode=true&characterEncoding=UTF-8";
- public static String USER_NAME="root";
- public static String PASSWORD="uchr@123";
- //加载驱动
- static {
- try {
- Class.forName(DRIVER);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- private JDBCUtils() {
- }
- /**
- * 获得连接
- *
- * @return
- */
- public static Connection getconnnection() {
- Connection con = null;
- try {
- con = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return con;
- }
- /**
- * 关闭连接
- *
- * @param rs
- * @param st
- * @param con
- */
- public static void close(ResultSet rs, Statement st, Connection con) {
- try {
- try {
- if (rs != null) {
- rs.close();
- }
- } finally {
- try {
- if (st != null) {
- st.close();
- }
- } finally {
- if (con != null)
- con.close();
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- /**
- * 关闭连接
- *
- * @param rs
- */
- public static void close(ResultSet rs) {
- Statement st = null;
- Connection con = null;
- try {
- try {
- if (rs != null) {
- st = rs.getStatement();
- rs.close();
- }
- } finally {
- try {
- if (st != null) {
- con = st.getConnection();
- st.close();
- }
- } finally {
- if (con != null) {
- con.close();
- }
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- /**
- * 关闭连接
- *
- * @param st
- * @param con
- */
- public static void close(Statement st, Connection con) {
- try {
- try {
- if (st != null) {
- st.close();
- }
- } finally {
- if (con != null)
- con.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- /**
- * insert/update/delete
- * 增加/更新/删除
- *
- * @param sql 数据库语句
- * @param args 可变参数(可以不带参数,可以带0-n个参数)
- * @return
- */
- public static int update(String sql, Object... args) {
- int result = 0;
- Connection con = getconnnection();
- PreparedStatement ps = null;
- try {
- ps = con.prepareStatement(sql);
- if (args != null) {
- for (int i = 0; i < args.length; i++) {
- ps.setObject((i + 1), args[i]);
- }
- }
- result = ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- close(ps, con);
- }
- return result;
- }
- /**
- * query, because need to manually close the resource, so not recommended
- * for use it
- *
- * @param sql
- * @param args
- * @return ResultSet
- */
- @Deprecated //注解
- public static ResultSet query(String sql, Object... args) {
- ResultSet result = null;
- Connection con = getconnnection();
- PreparedStatement ps = null;
- try {
- ps = con.prepareStatement(sql);
- if (args != null) {
- for (int i = 0; i < args.length; i++) {
- ps.setObject((i + 1), args[i]);
- }
- }
- result = ps.executeQuery();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return result;
- }
- /**
- * Query a single record
- * 查询单个记录
- * @param sql
- * @param args
- * @return Map<String,Object>
- */
- public static Map<String, Object> queryForMap(String sql, Object... args) {
- Map<String, Object> result = new HashMap<String, Object>();
- List<Map<String, Object>> list = queryForList(sql, args);
- if (list.size() > 0) {
- result = list.get(0);
- }
- return result;
- }
- /**
- * Query a single record
- * 查询单个记录返回强类型对象
- * @param sql
- * @param args
- * @return <T> //泛型
- */
- public static <T> T queryForObject(String sql, Class<T> clz, Object... args) {
- T result = null;
- List<T> list = queryForList(sql, clz, args);
- if (list.size() > 0) {
- result = list.get(0);
- }
- return result;
- }
- /**
- * Query a single record
- *
- * @param sql
- * @param args
- * @return List<Map<String,Object>>
- */
- public static List<Map<String, Object>> queryForList(String sql, Object... args) {
- List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
- Connection con = null;
- ResultSet rs = null;
- PreparedStatement ps = null;
- try {
- con = getconnnection();
- ps = con.prepareStatement(sql);
- if (args != null) {
- for (int i = 0; i < args.length; i++) {
- ps.setObject((i + 1), args[i]);
- }
- }
- rs = ps.executeQuery();
- ResultSetMetaData rsmd = rs.getMetaData();
- int columnCount = rsmd.getColumnCount();
- while (rs.next()) {
- Map<String, Object> map = new HashMap<String, Object>();
- for (int i = 1; i <= columnCount; i++) {
- map.put(rsmd.getColumnLabel(i), rs.getObject(i));
- }
- result.add(map);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- close(rs, ps, con);
- }
- return result;
- }
- /**
- * Query records
- * 查询多个对象,返回强类型集合
- * @param sql
- * @param args
- * @return List<T>
- */
- public static <T> List<T> queryForList(String sql, Class<T> clz, Object... args) {
- List<T> result = new ArrayList<T>();
- Connection con = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- con = getconnnection();
- ps = con.prepareStatement(sql);
- if (args != null) {
- for (int i = 0; i < args.length; i++) {
- ps.setObject((i + 1), args[i]);
- }
- }
- rs = ps.executeQuery();
- ResultSetMetaData rsmd = rs.getMetaData();
- int columnCount = rsmd.getColumnCount();
- while (rs.next()) {
- T obj = clz.newInstance();
- for (int i = 1; i <= columnCount; i++) {
- String columnName = rsmd.getColumnName(i);
- String methodName = "set" + columnName.substring(0, 1).toUpperCase()
- + columnName.substring(1, columnName.length());
- Method method[] = clz.getMethods();
- for (Method meth : method) {
- if (methodName.equals(meth.getName())) {
- meth.invoke(obj, rs.getObject(i));
- }
- }
- }
- result.add(obj);
- }
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- } finally {
- close(rs, ps, con);
- }
- return result;
- }
- }
9.3、WzDao
- package com.weibo.dao;
- import java.util.List;
- import com.weibo.bean.Wz;
- /**
- * 文章服务
- * 数据访问
- *
- */
- public class WzDao {
- /**获得所有的文章信息*/
- public List<Wz> all(){
- return JDBCUtils.queryForList("select bh,bt,nr from wz", Wz.class);
- }
- /**新增文章*/
- public int add(String bt,String nr){
- return JDBCUtils.update("insert into wz(bt,nr) values(?,?)", bt,nr);
- }
- }
9.4、index.jsp页面
- <%@page import="com.weibo.bean.Wz"%>
- <%@page import="com.weibo.dao.WzDao"%>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%
- WzDao wzdao=new WzDao();
- %>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>人工智能微博</title>
- </head>
- <body>
- <h2>人工智能微博</h2>
- <ul>
- <%for(Wz wz : wzdao.all()) {%>
- <li><%=wz.getBh() %></li>
- <li><%=wz.getBt() %></li>
- <li><%=wz.getNr() %></li>
- <li><a href="Delwz?bh=<%=wz.getBh() %>">删除</a></li>
- <hr/>
- <%} %>
- </ul>
- <a href="add.jsp">发布</a>
- </body>
- </html>
9.5、add.jsp页面
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>人工智能微博 - 发布</title>
- </head>
- <body>
- <h2>人工智能微博 - 发布</h2>
- <form action="Addwz" method="post">
- <p>
- 标题:<input type="text" name="bt" />
- </p>
- <p>
- 内容:<textarea type="text" name="nr" cols="60" rows="6" ></textarea>
- </p>
- <p>
- <input type="submit" value="提交" />
- </p>
- </form>
- <a href="index.jsp">列表</a>
- </body>
- </html>
9.6、Addwz Servlet
- package com.weibo.action;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.weibo.dao.WzDao;
- /**
- * Servlet implementation class Addwz
- */
- @WebServlet("/Addwz")
- public class Addwz extends HttpServlet {
- private static final long serialVersionUID = 1L;
- /**
- * @see HttpServlet#HttpServlet()
- */
- public Addwz() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //设置编码
- request.setCharacterEncoding("utf-8");
- response.setCharacterEncoding("utf-8");
- //获得参数
- String bt=request.getParameter("bt");
- String nr=request.getParameter("nr");
- //执行添加到数据库
- WzDao dao=new WzDao();
- dao.add(bt, nr);
- //跳转到指定页面
- response.sendRedirect("index.jsp");
- }
- }
9.7、运行结果
列表:
发布:
9.8、删除与样式
index.jsp
- <%@page import="com.weibo.bean.Wz"%>
- <%@page import="com.weibo.dao.WzDao"%>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%
- WzDao wzdao=new WzDao();
- %>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>人工智能微博</title>
- <style>
- .nr{
- border:1px dashed #666;
- color:#666;
- width:70%;
- line-height:26px;
- margin:8px;
- }
- .nr:hover{
- color:red;
- box-shadow:5px 5px 3px #999;
- border-radius:10px;
- }
- </style>
- </head>
- <body>
- <h2>人工智能微博</h2>
- <ul>
- <%for(Wz wz : wzdao.all()) {%>
- <li><%=wz.getBh() %></li>
- <li><%=wz.getBt() %></li>
- <li class="nr"><%=wz.getNr() %></li>
- <hr/>
- <%} %>
- </ul>
- <table border="1" width="100%">
- <tr>
- <th>编号</th> <th>标题</th> <th>内容</th> <th>操作</th>
- </tr>
- <%for(Wz wz : wzdao.all()) {%>
- <tr>
- <td><%=wz.getBh() %></td>
- <td><%=wz.getBt() %></td>
- <td><%=wz.getNr().length()>20?wz.getNr().substring(0,20)+"...":wz.getNr() %></td>
- <td><a href="Del?id=<%=wz.getBh() %>" onclick="return confirm('您确定要删除吗?');">删除</a></td>
- </tr>
- <%} %>
- </table>
- <a href="add.jsp">发布</a>
- </body>
- </html>
Del Servlet
- package com.weibo.action;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.weibo.dao.WzDao;
- /**
- * Servlet implementation class Del
- */
- @WebServlet("/Del")
- public class Del extends HttpServlet {
- private static final long serialVersionUID = 1L;
- /**
- * @see HttpServlet#HttpServlet()
- */
- public Del() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //取得客户端名为id的参数转成int
- int id=Integer.parseInt(request.getParameter("id"));
- //执行删除
- WzDao dao=new WzDao();
- dao.del(id);
- //跳转到首页
- response.sendRedirect("index.jsp");
- }
- }
WzDao.java
- package com.weibo.dao;
- import java.util.List;
- import com.weibo.bean.Wz;
- /**
- * 文章服务
- * 数据访问
- *
- */
- public class WzDao {
- /**获得所有的文章信息*/
- public List<Wz> all(){
- return JDBCUtils.queryForList("select bh,bt,nr from wz", Wz.class);
- }
- /**新增文章*/
- public int add(String bt,String nr){
- return JDBCUtils.update("insert into wz(bt,nr) values(?,?)", bt,nr);
- }
- /**删除文章*/
- public int del(int bh){
- return JDBCUtils.update("delete from wz where bh=?", bh);
- }
- }
运行效果:
9.9、编辑
9.10、移动端
示例下载:http://files.cnblogs.com/files/best/Weibo_m.zip
9.11、详细
9.12、添加
示例下载:http://files.cnblogs.com/files/best/Weibo_3.zip
9.13、内部测试
题目:智能电子书管理系统(libs)
要求:请使用JavaEE实现一个智能电子书管理系统,智能电子书的属性主要包含:标题(title)、页数(page)、类型(booktype),系统可以选择科学、人文、宗教、经济、法律。
步骤与得分:
1、创建数据库libs与表books,添加5行以上的数据,导出sql脚本。10分
2、创建动态Java Web项目,添加数据库驱动程序。10分
3、实现智能电子书展示功能,index.jsp。20分
4、实现智能电子书添加功能,new.jsp。10分
5、实现智能电子书删除功能。10分
6、实现智能电子书编辑功能,edit.jsp。20分
7、实现智能电子书详细功能,details.jsp。10分
8、实现手机端展示功能,m.jsp。5分
9、实现手机端详细功能,d.jsp。5分
10、实现手机端添加功能,a.jsp。5分
11、实现手机端编辑功能,e.jsp。5分
提交:不提交,利用两周时间完成,28号将统一检查打分,作为平时成绩。
JavaEE学习总结(十四)— 人工智能微博的更多相关文章
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...
- Linux学习之十四、管线命令
Linux学习之十四.管线命令 地址:http://vbird.dic.ksu.edu.tw/linux_basic/0320bash_6.php
- 风炫安全WEB安全学习第二十四节课 利用XSS钓鱼攻击
风炫安全WEB安全学习第二十四节课 利用XSS钓鱼攻击 XSS钓鱼攻击 HTTP Basic Authentication认证 大家在登录网站的时候,大部分时候是通过一个表单提交登录信息. 但是有时候 ...
- (C/C++学习笔记) 十四. 动态分配
十四. 动态分配 ● C语言实现动态数组 C语言实现动态数组,克服静态数组大小固定的缺陷 C语言中,数组长度必须在创建数组时指定,并且只能是一个常数,不能是变量.一旦定义了一个数组,系统将为它分配一个 ...
- Spring学习记录(十四)---JDBC基本操作
先看一些定义: 在Spring JDBC模块中,所有的类可以被分到四个单独的包:1.core即核心包,它包含了JDBC的核心功能.此包内有很多重要的类,包括:JdbcTemplate类.SimpleJ ...
- javascript基础学习(十四)
javascript之表单对象 学习要点: 表单对象 文本框 按钮 单选框和复选框 一.表单对象 在HTML文档中可能会出现多个表单,也就是说,一个HTML文档中可能出现多个<form>标 ...
- JMeter学习(十四)JMeter函数学习(转载)
转载自 http://www.cnblogs.com/yangxia-test JMeter函数是一些能够转化在测试树中取样器或者其他配置元件的域的特殊值.一个函数的调用就像这样:${_functio ...
- Python3.5 学习二十四
本节课程大纲: -------------------------------------------------------------------------------------------- ...
- Java第三阶段学习(十四、JSP动态页面、EL表达式、JSTL标签库)
一.JSP技术 1.jsp脚本和注释 jap脚本: 1)<%java代码%> ----- 内部的java代码翻译到service方法的内部,比如写在doget.dopost 内的代码 2) ...
- JAVA学习第十四课(接口:implements及其基本应用)
接口: 我们知道抽象类中能够定义抽象方法,也能够定义非抽象方法.当一个抽象类中的方法都是抽象方法的时候,我们就能够定义还有一种表现方式:接口(interface),所以接口是一种特殊的抽象类 接口的出 ...
随机推荐
- 去掉ambiguous expansion of macro警告
查看原文:http://www.heyuan110.com/?p=1221 用pod install后,pod工程里出现ambiguous expansion of macro的warning,对于有 ...
- git 的安装及使用
一.Git的安装和使用 1.1 Linux下版本库的创建 1.1.1 创建一个版本库 repository,在一个合适的地方创建一个空目录: root@zengyue:/# mkdir -p /hom ...
- pom.xml mevan 的 配置文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- logback基本入门
1. logback的定义 Logback是由log4j创始人设计的另一个开源日志组件,官方网站: http://logback.qos.ch.它当前分为下面下个模块: logback-core:其它 ...
- 使用kindeditor来替换ecshop的fckeditor编辑器,让ecshop可以批量上传图片
老杨原创 kindeditor此编辑器可以让ecshop批量上传图片,可以插入代码,可以全屏编辑,可以插入地图.视频,进行更多word操作,设置字体. 步骤一:进入kindeditor的官网,http ...
- doc.update
db.collection('todos').doc('todo-identifiant-aleatoire').update({ // data 传入需要局部更新的数据 data: { // 表示将 ...
- k8s 1.9 安装
测试环境 主机 系统 master CentOS 7.3 node CentOS 7.3 2.关闭selinux(所有节点都执行) [root@matser ~]# getenforce Disabl ...
- PostgreSQL、SQL Server数据库中的数据类型的映射关系
PostgreSQL 8.1 轰动发布,我也打算将原来使用 SQL Server 的一些应用迁移到 PostgreSQL 上,首先需要迁移的是表,那么这就必须要先搞清楚这两个数据库中的数据类型的映射关 ...
- js語句
js語句就是告訴瀏覽器要做什麼: js代碼就是js語句序列: js代碼塊就是{}包括的,函數就是一個代碼塊的典型例子: js注釋:單行注釋://,多行注釋:/**/ js對大小寫敏感: js語句可以不 ...
- SpringMVC @SessionAttributes 使用
@SessionAttributes 只能作用在类上,作用是将指定的Model中的键值对添加至session中,方便在下一次请求中使用. 简单示例 目标是通过 @SessionAttributes 注 ...