一、数据库与表

人工智能微博(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 (\'1\', \'微软建立新人工智能实验室 开发多用途学习系统\', \'微软公司凤凰科技讯据彭博社北京时间7月12日报道,微软公司将建立一个新研究实验室,专注于人工智能(AI),目标是开发出更为多用途的学习系统\');
INSERT INTO `wb` VALUES (\'2\', \'人工智能来袭 金融行业迎来下岗潮\', \'“在金融圈和用户教育上,对人工智能还未完全信任之前,我们只能采取人肉智能(HI)和人工智能(AI)相结合的方式”,朱明杰称,他们会机器先出一些模型,在专业的风控从\');
INSERT INTO `wb` VALUES (\'3\', \'即便取代脑力劳动AI还不能“打动”人心\', \'但AI革命不同, AI取代脑力劳动,将让人类转向创造性、情感性劳动,这些素质是每一个体都具有的。 事实上,人工智能导致失业,还属于一种弱人工智能观念。有人煞有介\');
INSERT INTO `wb` VALUES (\'4\', \'Google 设立风投公司扶持 AI 新创团队\', \'Google 在 AI 上的野心人尽皆知,但要让行业更好地发展,光有他们自己的力量是不够的。因此在最近他们新成立了一间名为 Gradient\');
INSERT INTO `wb` VALUES (\'5\', \'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;
    }
}

View Code

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;
    }
}

View Code

 

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号将统一检查打分,作为平时成绩。

 

面向对象4、5章示例下载

版权声明:本文为best原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/best/p/7158731.html