今天下午在软件工程课堂学习了许多关于软件工程专业的学习方法,并在课后由于开学测试不合格的原因,参加了老师组织的二次测试,测试题目完全完成,但完成速率有待提高,老师给的题目相当简单但我足足花了40分钟才完成要求,可以看出还是不够熟练,往后应注意多加练习。测试完成后帮助同学配置了web程序的环境,同时自己温习了环境的配置以及数据库的连接,并且解决了数据在录入数据库时乱码的问题。

测试代码:

Test.Java

  1 package test;
  2 
  3 import java.sql.*;
  4 
  5 public class Test {
  6     private String biao,zheng,tian;
  7 
  8     public String getTian() {
  9         return tian;
 10     }
 11 
 12     public void setTian(String tian) {
 13         this.tian = tian;
 14     }
 15 
 16     public String getZheng() {
 17         return zheng;
 18     }
 19 
 20     public void setZheng(String zheng) {
 21         this.zheng = zheng;
 22     }
 23 
 24     public String getBiao() {
 25         return biao;
 26     }
 27 
 28     public void setBiao(String biao) {
 29         this.biao = biao;
 30     }
 31     public Connection getConnection()//连接数据库
 32     {
 33         try{
 34             Class.forName("com.mysql.cj.jdbc.Driver");
 35             //System.out.println("加载驱动成功");
 36         }catch(ClassNotFoundException e)
 37         {
 38             e.printStackTrace();
 39         }
 40         String user="root";
 41         String password="352204";
 42         String url = "jdbc:mysql://localhost:3306/zong?useSSL=false&serverTimezone=GMT&characterEncoding=utf-8&autoReconnect=true";
 43         Connection con=null;
 44         try{
 45             con= DriverManager.getConnection(url,user,password);
 46             //System.out.println("数据库连接成功");
 47         }catch(SQLException e)
 48         {
 49             e.printStackTrace();
 50         }
 51         return con;
 52     }
 53     //**********************************************************************
 54     //关闭方法
 55     public  void close (Connection con)
 56     {
 57         try{
 58             if(con!=null)
 59             {
 60                 con.close();
 61             }
 62         }catch(SQLException e)
 63         {
 64             e.printStackTrace();
 65         }
 66     }
 67     public  void close (PreparedStatement preparedStatement)
 68     {
 69         try{
 70             if(preparedStatement!=null)
 71             {
 72                 preparedStatement.close();
 73             }
 74         }catch(SQLException e)
 75         {
 76             e.printStackTrace();
 77         }
 78     }
 79     public  void close(ResultSet resultSet)
 80     {
 81         try{
 82             if(resultSet!=null)
 83             {
 84                 resultSet.close();
 85             }
 86         }catch(SQLException e)
 87         {
 88             e.printStackTrace();
 89         }
 90     }
 91 
 92 
 93     public void adddata(String biao,String zheng,String tian) {
 94         Connection connection = getConnection();
 95         PreparedStatement preparedStatement = null;
 96         try {
 97 
 98             String sql = "insert into bao (标题, 内容, 坚持天数) values (?,?,?)";
 99             preparedStatement = connection.prepareStatement(sql);
100             preparedStatement.setString(1, biao);
101             preparedStatement.setString(2, zheng);
102             preparedStatement.setString(3, tian);
103             preparedStatement.executeUpdate();
104             //System.out.println("添加成功");
105 
106         } catch (SQLException e) {
107             e.printStackTrace();
108         } finally {
109             close(preparedStatement);
110             close(connection);
111         }
112     }
113 }

index.jsp

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3   <head>
 4     <title>总结</title>
 5   </head>
 6   <body>
 7   <form action="add.jsp" method="post">
 8     <br>
 9     标题:
10     <br>
11     <input type="text" name="biao">
12     <br>
13     <br>
14     正文:
15     <br>
16     <textarea  style="width: auto" name="zheng"></textarea>
17     <br>
18     天数:
19     <br>
20     <input type="text" name="tian">
21     <br style="text-align: center">
22     <input type="submit" value="发布">
23     <input type="button" value="查询" onclick="location.href='show.jsp'">
24     </br>
25   </form>
26   </body>
27 </html>

add.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2          pageEncoding="UTF-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6     <meta charset="UTF-8">
 7     <title></title>
 8 </head>
 9 <body>
10 <jsp:useBean id="util" class="test.Test" scope="page" />
11 <%
12     request.setCharacterEncoding("UTF-8");<%--解决中文乱码问题--%>
13     String biao=(String)request.getParameter("biao");
14     String zheng=(String)request.getParameter("zheng");
15     String tian=(String)request.getParameter("tian");
16         util.adddata(biao,zheng,tian);
17         out.print("<script language='javaScript'> alert('添加成功');</script>");
18         response.setHeader("refresh", "0;url=search.jsp");
19     %>
20 </body>
21 </html>

search.jsp

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>浏览</title>
 5 </head>
 6 <body>
 7 <jsp:useBean id="util" class="test.Test" scope="page" />
 8 <table border="1"style="text-align:center;">
 9   <tr>
10     <td align="center" width=6%>标题</td>
11     <td align="center" width=8%>正文</td>
12     <td align="center" width=10%>天数</td>
13   </tr>
14   <%
15     Connection connection = util.getConnection();
16     PreparedStatement preparedStatement=null;
17     ResultSet rs=null;
18     try {
19       String sql = "select * from bao";
20       preparedStatement=connection.prepareStatement(sql);
21       rs=preparedStatement.executeQuery();
22       while(rs.next()){
23   %>
24   <tr>
25     <td align="center"><%=rs.getObject(1) %></td>
26     <td align="center"><%=rs.getObject(2) %></td>
27     <td align="center"><%=rs.getObject(3) %></td>
28   </tr>
29   <%
30       }
31     } catch (SQLException e) {
32       e.printStackTrace();
33     }finally{
34       util.close(rs);
35       util.close(preparedStatement);
36       util.close(connection);
37     }
38   %>
39 </table>
40 <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px">
41   <input type="button" value="返回菜单" onclick="location.href='index.jsp'" /> <br>
42 </p>
43 
44 </body>
45 </html>

show.jsp

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>查询</title>
 5 </head>
 6 <body>
 7 <form action="showjudge.jsp" method="post">
 8     请输入要查询文章标题:
 9     <br>
10     <input type="text" name="biao">
11     <br>
12     <br>
13     <input type="submit" value="提交">
14     <input type="button" value="返回菜单" onclick="location.href='index.jsp'" />
15 </form>
16 </body>
17 </html>

showjudge.jsp

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>结果</title>
 5 </head>
 6 <body>
 7 <p>
 8     <jsp:useBean id="util" class="test.Test" scope="page" />
 9         <%
10 String biao=(String)request.getParameter("biao");
11 if(biao==""){
12     out.print("<script language='javaScript'> alert('输入为空'); window.history.back(-1); </script>");
13 }
14 else
15 {
16     %>
17 <table border="1"style="text-align:center;">
18     <tr>
19         <td align="center" width=6%>标题</td>
20         <td align="center" width=3%>正文</td>
21         <td align="center" width=3%>天数</td>
22     </tr>
23     <%
24         int i=0;
25         Connection connection = util.getConnection();
26         PreparedStatement preparedStatement=null;
27         ResultSet rs=null;
28         try {
29             String sql= " select * from bao where  标题 like ?";
30             preparedStatement=connection.prepareStatement(sql);
31             preparedStatement.setString(1,"%"+biao+"%");
32             rs=preparedStatement.executeQuery();
33             while(rs.next())
34             {
35                 i++;
36     %>
37     <tr>
38         <td align="center"><%=rs.getObject(1) %></td>
39         <td align="center"><%=rs.getObject(2) %></td>
40         <td align="center"><%=rs.getObject(3) %></td>
41     </tr>
42     <%
43                 }
44                 if(i==0)
45                 {
46                     out.print("<script language='javaScript'> alert('没有查询到有关信息'); window.history.back(-1); </script>");
47                 }
48             } catch (SQLException e) {
49                 e.printStackTrace();
50             }finally{
51                 util.close(rs);
52                 util.close(preparedStatement);
53                 util.close(connection);
54             }
55         }
56     %>
57 </table>
58 
59 <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px">
60     <br> <input type="button" value="返回菜单" onclick="location.href='index.jsp'" /> <br>
61 </p>
62 </body>
63 </html>