软件工程学习进度第二周暨暑期学习进度之第二周汇总

今天是7月14号,暑假刚刚好过去两周,最近的一周里收获也是蛮大的。下面就先来总结一下:

首先,java的核心技术如接口、继承、多态以及集合,I/O流和swing程序设计已完成,另外数据库操作也已经用的比较熟练。javaweb方面已经能通过jsp实现基本的网站功能,同时对上周的登录和注册功能做了处理,并实现了网站首页,但是对servlet和javabean使用并不熟练,而且在目前学生管理系统的开发过程中几乎没有用到。python爬虫以及对数据库的连接基本掌握。机器学习方面,基本了解了机器学习的基本任务如分类、回归、去噪、转录、机器翻译、异常检测和结构化输出,但是对计算图、张量和TensorFlow的运行模型理解不深,另外还掌握了线性回归的基本原理,并能通过程序实现简单的梯度下降。

下面首先展示一下python爬虫爬取的数据存入MySQL的程序代码即截图

爬虫与数据库对接源码如下:

 1 from bs4 import BeautifulSoup
 2 from urllib3 import *
 3 from re import *
 4 from pymysql import *
 5 import json
 6 url="http://www.stdu.edu.cn/"
 7 http=PoolManager()
 8 
 9 #连接数据库的方法
10 def connectDB():
11     try:
12         db=connect(host='localhost',port=3306,user='root',password='010218',db='people_information_db')
13         return db
14     except Exception as e:
15         print(e)
16     return NULL
17 
18 db=connectDB()
19 
20 #向数据库中插入数据的方法
21 def insertInformation(db,table,title,href,titleinf,numinf):
22     cursor=db.cursor()
23     try:
24         cursor.execute("insert into %s(%s,%s) values('%s','%s')" % (table,title,href,titleinf,numinf))
25         print("插入成功")
26         db.commit()
27         cursor.close()
28         return True
29     except Exception as e:
30         print(e)
31         db.rollback()
32     return False
33 #下载网页HTML
34 def download(url):
35     html=http.request('GET',url)
36     htmlstr=html.data.decode('utf-8')
37     soup=BeautifulSoup(htmlstr,'lxml')
38     return soup
39 
40 #解析网页结构,筛选需要的数据存入数据库
41 def analyse_and_insert(soup):
42     newstitle = "newstitle"
43     newshref = "newshref"
44     noticetitle = "noticetitle"
45     noticehref = "noticehref"
46     str=soup.findAll('li',{"class":"latestnewsnews"})
47     for s in str:
48         insertInformation(db,'std_news',newstitle,newshref,s.text,s.a['href'])
49         print(s.a.text)
50         print(s.a['href'])
51     str1=soup.findAll('li',{"class":"latestnewsnotice"})
52     for s in str1:
53         insertInformation(db, 'std_notice', noticetitle, noticehref, s.text, s.a['href'])
54         print(s.a.text)
55         print(s.a['href'])
56 
57 if __name__=='__main__':
58     analyse_and_insert(download(url))

该程序爬取目标石家庄铁道大学官网首页的新闻与公告模块内容

存入MySQL数据库后MySQLworkbench的截图:

接下来就是学生管理系统的进度

下面是学生管理系统首页的jsp源码:

  1 <%@page import="com.mysql.cj.jdbc.Driver"%>
  2 <%@page import="java.sql.Statement"%>
  3 <%@page import="java.sql.ResultSet"%>
  4 <%@page import="javax.naming.spi.DirStateFactory.Result"%>
  5 <%@page import="java.sql.DriverManager"%>
  6 <%@page import="java.sql.Connection"%>
  7 <%@ page language="java" contentType="text/html; charset=GB18030"
  8     pageEncoding="GB18030"%>
  9 <!DOCTYPE html>
 10 <html>
 11 <head>
 12 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 13 <title>首页</title>
 14 
 15 <style>
 16     #head {
 17     background-color: #66CCCC;
 18     text-align: center;
 19     position: relative;
 20     height: 100px;
 21     width: 100;
 22     text-shadow: 5px 5px 4px Black;
 23     }
 24     #wrap{
 25     width:100;
 26     height:764px;
 27     }
 28     #wrap .na{
 29     position:relative;
 30     height:764px;
 31     width:15%;
 32     float:left;
 33     background-size:cover;
 34     background-repeat:no-repeat;
 35     }
 36     #wrap .newsshow{
 37     position:relative;
 38     height:764px;
 39     width:85%;
 40     float:right;
 41     text-align:center;
 42     background-size:cover;
 43     overflow-y :auto;
 44     }
 45     #foot {
 46     width: 100;
 47     height:200px;
 48     background-color:#CC9933;
 49     position: relative;
 50     text-align:center;
 51     }
 52     .title {    
 53     font-family: "宋体";    
 54     color: #FFFFFF;    
 55     position: absolute;    
 56     top: 50%;    
 57     left: 50%;    
 58     transform: translate(-50%, -50%);  /* 使用css3的transform来实现 */    
 59     font-size: 36px;    
 60     height: 40px;    
 61     width: 30%;
 62     } 
 63     .power {    
 64     font-family: "宋体";
 65     color: #FFFFFF;    
 66     position: absolute;    
 67     top: 50%;
 68     left: 50%;    
 69     transform: translate(-50%, -50%);  
 70     height: 60px;    
 71     width: 40%;    
 72     align-content:center;
 73     }     
 74     #foot .power .information {    
 75     width: 100%;    
 76     height: 24px;    
 77     position: relative;
 78     }
 79     #foot .power p {    
 80     height: 24px;    
 81     width: 100%;
 82     }
 83     #wrap #nav .navbar{
 84     text-align:center;
 85     text-size:10px;
 86     }
 87     nav { float: left; clear: both; width: 15%; margin: auto; height:764px; background: #eee; }
 88     nav ul { list-style: none; margin: 0px; padding: 0px; }
 89     nav li { float: none; width: 100%; }
 90     nav li a { display: block; width: 100%; padding: 20px; border-left: 5px solid; position: relative; z-index: 2; text-decoration: none; color: #444; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; }
 91     nav li a:hover { border-bottom: 0px; color: #fff; }
 92     nav li:first-child a { border-left: 10px solid #3498db; }
 93     nav li:nth-child(2) a { border-left: 10px solid #ffd071; }
 94     nav li:nth-child(3) a { border-left: 10px solid #f0776c; }
 95     nav li:nth-child(4) a { border-left: 10px solid #9370db; }
 96     nav li:nth-child(5) a { border-left: 10px solid #9acd32; }
 97     nav li:nth-child(6) a { border-left: 10px solid #888888; }
 98     nav li:last-child a { border-left: 10px solid #1abc9c; }
 99     nav li a:after { content: ""; height: 100%; left: 0; top: 0; width: 0px; position: absolute; transition: all 0.3s ease 0s; -webkit-transition: all 0.3s ease 0s; z-index: -1; }
100     nav li a:hover:after { width: 100%; }
101     nav li:first-child a:after { background: #3498db; }
102     nav li:nth-child(2) a:after { background: #ffd071; }
103     nav li:nth-child(3) a:after { background: #f0776c; }
104     nav li:nth-child(4) a:after { background: #9370db; }
105     nav li:nth-child(5) a:after { background: #9acd32; }
106     nav li:nth-child(6) a:after { background: #888888; }
107     nav li:last-child a:after { background: #1abc9c; }
108     
109     
110     #wrap #show #planediv{
111     width:100;
112     height:100px;
113     position:relative;
114     margin-left:35%;
115     }
116 
117     #wrap #show #planediv .navbtn{
118     height:80%;
119     font-size:20px;
120     border:none;
121     padding-top:10px;
122     padding-bottom:10px;
123     padding-left:60px;
124     padding-right:60px;
125     margin-left:0px;
126     cursor:pointer;
127     border-radius:10px;
128     background-color:#A8A8A8 ;
129     }
130 
131     #wrap .newsshow #titleimg{
132     width:100%;
133     height:170px;
134     background-repeat:no-repeat;
135     background-size:100% 100%;
136     position:relative;
137     background-image:url(stdtitle.jpg);
138     }
139     
140     #wrap .newsshow #newstitle{
141     padding-top:10px;
142     width:100%;
143     height:30px;
144     font-family:"宋体";
145     font-size:20px;
146     text-align:center;
147     position:relative;
148     }
149     
150     #wrap .newsshow #newslist{
151     width:50%;
152     height:100%;
153     float:left;
154     position:relative;
155     background-color: rgba(200, 200, 200, 0.5);     
156     border-radius: 10px;
157     }
158     #wrap .newsshow #noticelist{
159     width:50%;
160     height:100%;
161     float:right;
162     position:relative;
163     background-color: rgba(200, 200, 200, 0.5);     
164     border-radius: 10px;
165     }
166     
167     #wrap .newsshow #newslist ul li{
168     padding:10px;
169     text-align:left;
170     border-bottom: 1px solid #ee7700;
171     }
172     #wrap .newsshow #noticelist ul li{
173     padding:10px;
174     text-align:left;
175     border-bottom: 1px solid #ee7700;
176     }
177     #wrap .newsshow #newslist ul li a{
178     text-decoration:none;
179     }
180     #wrap .newsshow #noticelist ul li a{
181     text-decoration:none;
182     }
183     
184 </style>
185 
186 </head>
187 <body>
188     <%! 
189         String str_newstitle[]=new String[20];
190         String str_noticetitle[]=new String[20];
191         String str_newshref[]=new String[20];
192         String str_noticehref[]=new String[20];
193         String str_more[]=new String[2];
194     %>
195     <%
196         Connection con=null;
197         Statement stmt=null;
198         ResultSet rs_news=null;
199         ResultSet rs_notice=null;
200         ResultSet rs_more=null;
201         try{
202             Class.forName("com.mysql.cj.jdbc.Driver");
203             String url="jdbc:mysql://localhost:3306/people_information_db?useUnicode=true&characterEncoding=GB18030&useSSL=false&serverTimezone=GMT";
204             String username="root";
205             String password="010218";
206             con=DriverManager.getConnection(url, username, password);
207             if(con!=null)
208             {
209                 stmt=con.createStatement();
210                 rs_news=stmt.executeQuery("select * from std_news");
211                 int i=0;
212                 while(rs_news.next())
213                 {
214                     str_newstitle[i]=rs_news.getString(2);
215                     str_newshref[i]=rs_news.getString(3);
216                     i++;
217                 }
218                 rs_notice=stmt.executeQuery("select * from std_notice");
219                 i=0;
220                 while(rs_notice.next())
221                 {
222                     str_noticetitle[i]=rs_notice.getString(2);
223                     str_noticehref[i]=rs_notice.getString(3);
224                     i++;
225                 }
226                 rs_more=stmt.executeQuery("select * from more_news");
227                 i=0;
228                 while(rs_more.next())
229                 {
230                     str_more[i]=rs_more.getString(2);
231                     i++;
232                 }
233             }
234         }
235         catch(Exception e)
236         {
237             e.printStackTrace();
238         }
239     %>
240     <div class="header" id="head">
241       <div class="title">小赵的学生信息管理系统</div>
242     </div>
243     <div class="wrap" id="wrap">
244         <nav class="nav" id="nav">
245             <ul class="navbar">
246                 <li><a href="mainpage.jsp">首页</a></li>
247                 <li><a href="usermag.jsp">用户管理</a></li>
248                 <li><a href="#">学籍管理</a></li>
249                 <li><a href="#">选课管理</a></li>
250                 <li><a href="#">成绩管理</a></li>
251                 <li><a href="#">课程管理</a></li>
252                 <li><a href="#">密码管理</a></li>
253             </ul>
254         </nav>
255         <div id="stdtitle"></div>
256         <div class="newsshow">
257             <div id="titleimg"></div>
258             <div id="newstitle">友情新闻链接</div>
259             <div id="newslist">
260             <h3>学校新闻</h3>
261                 <ul>
262                     <li><a href='<%=str_newshref[0] %>'><%=str_newstitle[0] %></a></li>
263                     <li><a href='<%=str_newshref[1] %>'><%=str_newstitle[1] %></a></li>
264                     <li><a href='<%=str_newshref[2] %>'><%=str_newstitle[2] %></a></li>
265                     <li><a href='<%=str_newshref[3] %>'><%=str_newstitle[3] %></a></li>
266                     <li><a href='<%=str_newshref[4] %>'><%=str_newstitle[4] %></a></li>
267                     <li><a href='<%=str_newshref[5] %>'><%=str_newstitle[5] %></a></li>
268                     <li><a href='<%=str_newshref[6] %>'><%=str_newstitle[6] %></a></li>
269                     <li><a href='<%=str_newshref[7] %>'><%=str_newstitle[7] %></a></li>
270                     <li><a href='<%=str_newshref[8] %>'><%=str_newstitle[8] %></a></li>
271                     <li><a href='<%=str_newshref[9] %>'><%=str_newstitle[9] %></a></li>
272                     <li><a href='<%=str_newshref[10] %>'><%=str_newstitle[10] %></a></li>
273                     <li><a href='<%=str_more[1] %>'>>查看更多</a></li>
274                 </ul>
275                 
276             </div>
277             
278             <div id="noticelist">
279             <h3>校内公告</h3>
280                 <ul>
281                     <li><a href='<%=str_noticehref[0] %>'><%=str_noticetitle[0] %></a></li>
282                     <li><a href='<%=str_noticehref[1] %>'><%=str_noticetitle[1] %></a></li>
283                     <li><a href='<%=str_noticehref[2] %>'><%=str_noticetitle[2] %></a></li>
284                     <li><a href='<%=str_noticehref[3] %>'><%=str_noticetitle[3] %></a></li>
285                     <li><a href='<%=str_noticehref[4] %>'><%=str_noticetitle[4] %></a></li>
286                     <li><a href='<%=str_noticehref[5] %>'><%=str_noticetitle[5] %></a></li>
287                     <li><a href='<%=str_noticehref[6] %>'><%=str_noticetitle[6] %></a></li>
288                     <li><a href='<%=str_noticehref[7] %>'><%=str_noticetitle[7] %></a></li>
289                     <li><a href='<%=str_noticehref[8] %>'><%=str_noticetitle[8] %></a></li>
290                     <li><a href='<%=str_noticehref[9] %>'><%=str_noticetitle[9] %></a></li>
291                     <li><a href='<%=str_noticehref[10] %>'><%=str_noticetitle[10] %></a></li>
292                     <li><a href='<%=str_more[0] %>'>>查看更多</a></li>
293                 </ul>
294             </div>
295         </div>
296         
297     </div>
298     <div class="footer" id="foot">  
299         <div class="power">    
300             Copyright © 2019 All Rights Reserved.   
301             <div class="information">        
302             <span>联系邮箱:[email protected]</span>    
303             </div>
304             <div class="information">        
305             <span>联系地址:石家庄铁道大学</span>    
306             </div>
307             <div class="information">      
308             <span>联系电话:15716888392</span>    
309             </div>    
310         </div>    
311     </div>    
312 </body>
313 </html>

运行效果图如下:

左边导航栏的动画做的很用心,花了一个上午(可能是因为我太菜了哈哈)

这里通过数据库获取了通过spider爬到的内容,通过列表展示出来,通过点击列表项即可快速查看相关新闻或者公告内容,这里不做具体演示

接下来是登录和注册处理部分,通过一个工具类DBUtil实现对数据库的操作以及数据比对,从而验证登录与注册过程

DBUtil类源码如下:

  1 package com.stumag.util;
  2 
  3 import java.sql.Connection;
  4 import java.sql.DriverManager;
  5 import java.sql.PreparedStatement;
  6 import java.sql.ResultSet;
  7 import java.sql.SQLException;
  8 
  9 
 10 public class DBUtil {
 11     //数据库URL和账号密码
 12     public static final String connectionURL="jdbc:mysql://localhost:3306/people_information_db?useUnicode=true&characterEncoding=GB18030&useSSL=false&serverTimezone=GMT";
 13     public static final String username="root";
 14     public static final String password="010218";
 15     
 16     //数据库连接
 17     public static Connection getConnection()
 18     {
 19         try {
 20             Class.forName("com.mysql.cj.jdbc.Driver");
 21             System.out.println("数据库连接成功");
 22             return DriverManager.getConnection(connectionURL, username, password);
 23         } catch (Exception e) {
 24             // TODO: handle exception
 25             e.printStackTrace();
 26         }
 27         return null;
 28     }
 29     
 30     //注册
 31     @SuppressWarnings("resource")
 32     public static boolean addUser(int ID,String username,String password,String email)
 33     {
 34         Connection con=null;
 35         PreparedStatement pstmt=null;
 36         ResultSet rs=null;
 37         try {
 38             con=getConnection();
 39             String sql_query="select * from normalusers where id ="+ID;
 40             pstmt=con.prepareStatement(sql_query);
 41             rs=pstmt.executeQuery();
 42             if(rs.next()==false)
 43             {
 44                 String sql="insert into normalusers(id,username,password,email) values(?,?,?,?)";
 45                 pstmt=con.prepareStatement(sql);
 46                 pstmt.setInt(1, ID);
 47                 pstmt.setString(2, username);
 48                 pstmt.setString(3, password);
 49                 pstmt.setString(4, email);
 50             }
 51             pstmt.executeUpdate();
 52             return true;
 53         }
 54         catch (Exception e) {
 55             System.out.println("注册失败");
 56         }
 57         finally {
 58             try {
 59                 if(con!=null)
 60                     con.close();
 61             } catch (SQLException e) {
 62                 // TODO 自动生成的 catch 块
 63                 e.printStackTrace();
 64             }
 65         
 66             try {
 67                 if(pstmt!=null)
 68                     pstmt.close();
 69             } catch (SQLException e) {
 70                 // TODO 自动生成的 catch 块
 71                 e.printStackTrace();
 72             }
 73         
 74             try {
 75                 if(rs!=null)
 76                     rs.close();
 77             } catch (SQLException e) {
 78                 // TODO 自动生成的 catch 块
 79                 e.printStackTrace();
 80             }
 81         }
 82         return false;
 83     }
 84     
 85     //登录时验证数据库中账户是否存在
 86     public static boolean isExist(String username,String password)
 87     {
 88         Connection con=null;
 89         PreparedStatement pstmt=null;
 90         ResultSet rs=null;
 91         try {
 92             con=getConnection();
 93             String sql_query="select * from normalusers where username = '"+username+"' and password = '"+password+"'";
 94             System.out.println(sql_query);
 95             pstmt=con.prepareStatement(sql_query);
 96             rs=pstmt.executeQuery();
 97             if(rs.next()==false)
 98             {
 99                 System.out.println("用户名或密码错误");
100                 return false;
101                 
102             }
103             else
104             {
105                 System.out.println("用户名或密码正确");
106                 return true;
107             }
108         }
109         catch (Exception e) {
110             System.out.println("未连接");
111         }
112         finally {
113             try {
114                 if(con!=null)
115                     con.close();
116             } catch (SQLException e) {
117                 // TODO 自动生成的 catch 块
118                 e.printStackTrace();
119             }
120         
121             try {
122                 if(pstmt!=null)
123                     pstmt.close();
124             } catch (SQLException e) {
125                 // TODO 自动生成的 catch 块
126                 e.printStackTrace();
127             }
128         
129             try {
130                 if(rs!=null)
131                     rs.close();
132             } catch (SQLException e) {
133                 // TODO 自动生成的 catch 块
134                 e.printStackTrace();
135             }
136         }
137         return false;
138     }
139 }

下面是处理登录的jsp源码:

 1 <%@page import="com.stumag.util.DBUtil"%>
 2 <%@ page language="java" contentType="text/html; charset=GB18030"
 3     pageEncoding="GB18030"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 8 <title>login_do</title>
 9 </head>
10 <body>
11     <%
12         //处理登录请求
13         String username=request.getParameter("username");
14         
15         String password=request.getParameter("password");
16         
17         if(DBUtil.isExist(username, password))
18         {
19             request.getRequestDispatcher("mainpage.jsp").forward(request, response);
20         }
21         else
22         {
23             request.getRequestDispatcher("login.jsp").forward(request,response);
24         }
25     %>
26 </body>
27 </html>

如果信息输入正确,登录成功后页面会进入上述系统首页,若信息输入错误则不跳转。

以上就是本周的所有进度,接下来的几天将会完成 java核心技术的其他剩余模块如反射、线程、网络通信,javaweb将会继续servlet和javabean的学习使用,管理系统将再进行两个模块,并实现动态分页显示所需要的数据库的数据,同时实现分类查询功能。另外机器学习的代码练习将从下周一开始进行,争取下一周实现部分数据集和python的对接!

加油!!!

猜你喜欢

转载自www.cnblogs.com/zdm-code/p/11183914.html