202309-发际线与你们作队 实验四:软件开发案例(1)(团队作业)
项目 | 内容 |
---|---|
课程班级博客链接 | 2020卓越工程师班 |
这个作业要求链接 | 实验四 软件开发案例(1) |
团队名称 | 发际线与你们作队 |
我的课程学习目标 | 完成软件开发案例(1) |
这个作业在哪些方面帮助我实现学习目标 | 1.软件开发环境部署。 2.练习mysql数据库创建和连接访问技术。 3.掌握数据库应用程序开发技术。 4.通过团队内部交流感受完成软件开发过程,总结团队未注意到的细节。 |
团队博客链接 | 发际线与你们作队 |
一、用户综合管理软件案例资源包
-
(1)创建用户综合管理软件的Java web项目,项目名称自拟。
-
在IDEA上创建JavaWeb项目
-
(2)在项目中添加mysql的驱动支持包。
-
由于在创建项目时使用了Maven仓库,故直接导入依赖即可
-
-
(3)分别用手工或脚本方式创建mysql数据库及表。
-
使用工具Navicat创建数据库及表test4
-
在表test4中添加数据
-
-
(4)将用户综合管理软件案例代码导入到项目中。
-
将案例代码导入至项目中
-
修改代码连接数据库
-
-
(5)在Tomcat服务器中部署项目。
-
在Tomcat11上部署项目
-
-
(6)测试运行用户信息的查询、更新、删除、显示等操作。
-
用户的全部查询功能
-
根据用户ID查询用户信息
-
添加用户信息
-
修改用户数据
-
删除用户数据
-
二、用户综合管理软件增量开发
-
(1)为案例软件开发软件入口主界面,作为查询、更新、删除、显示的功能导航页面。
1. 实现界面
2. 修改jsp代码
<!DOCTYPE html>
<html>
<head>
<title>简易的增删改查</title>
</head>
<body>
<h1><%= "请选择您的操作" %>
</h1>
<br/>
<table>
<tbody>
<tr>
<td>
更新操作
</td>
<td><a href="dele.jsp">删除用户</a></td><td> </td>
<td><a href="insert.jsp">添加用户</a></td><td> </td>
<td><a href="update.jsp">修改用户数据</a></td>
</tr>
<tr><td> </td></tr>
<tr>
<td>
查询操作
</td>
<td><a href="search.jsp">查询用户数据</a></td><td> </td>
<td><a href="allShow.jsp">查看全部用户数据</a></td>
</tr>
<tr>
<form id="file_form" action="UpdFile" enctype="multipart/form-data"
method="post">
<input type="file" name="file" id="file_input" />
<input type="submit" value="上传Excel" id='upFile-btn'>
</form>
</tr>
</tbody>
</table>
</body>
</html>
-
(2)为案例软件开发一个新功能,可将excel文件的用户信息批量导入数据库。
-
建立如下Excel文件test4,文件中有数据如下![]
-
将Excel文件数据导入数据库
-
-
未加入前:
-
上传Excel
-
批量添加后重定向到查询所有界面
-
加入poi相关依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-contrib</artifactId>
<version>3.5-beta5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.5-beta5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.5-beta5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.5-beta5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.5</version>
</dependency>
-
创建servlet接收UpdFile请求
package com.example.test4.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import com.example.test4.dbutil.ExcelHelper;
import com.example.test4.entity.User;
import com.example.test4.model.Model;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/UpdFile")
public class UpdFile extends HttpServlet {
/**
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
process(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
process(request, response);
}
private void process(HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("application/json");
PrintWriter out = response.getWriter();
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding(request.getCharacterEncoding());
ExcelHelper helper = new ExcelHelper();
Model model = new Model();
try {
List<FileItem> list = upload.parseRequest( request);
for (int i = 0; i < list.size(); i++) {
FileItem item = list.get(i);
if (item.getName().endsWith(".xls") || item.getName().endsWith(".xlsx")) {
// 说明是文件,不过这里最好限制一下
//ExcelHelper.importXls(item.getInputStream());
ArrayList<User> users = helper.importXlsx(item.getInputStream());
out.write("{\"result\":\"OK\"}");
for (User u:users) {
//System.out.println(users.toString());
model.insert(u.getId(),u.getName(),u.getPassword());
}
response.sendRedirect("allShow.jsp");
} else {
// 说明文件格式不符合要求
out.write("{\"result\":\"Invalid\"}");
}
}
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
-
创建ExcelHelper类打开Excel并处理数据
package com.example.test4.dbutil;
import com.example.test4.entity.User;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.*;
import java.io.InputStream;
import java.util.ArrayList;
public class ExcelHelper {
// 读取单元格的值
private String getValue(Cell cell) {
String result = "";
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue() + "";
break;
case Cell.CELL_TYPE_STRING:
result = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
result = cell.getCellFormula();
break;
case Cell.CELL_TYPE_NUMERIC:
// 可能是普通数字,也可能是日期
if (HSSFDateUtil.isCellDateFormatted(cell)) {
result = DateUtil.getJavaDate(cell.getNumericCellValue())
.toString();
} else {
result = cell.getNumericCellValue() + "";
}
break;
}
return result;
}
/***
* 这种方法支持03,和07版本的excel读取
* 但是对于合并的单元格,除了第一行第一列之外,其他部分读取的值为空
* @param is
*/
public ArrayList<User> importXlsx(InputStream is) {
ArrayList<User> users = new ArrayList<>();
try {
Workbook wb = WorkbookFactory.create(is);
for (int i = 0, len = wb.getNumberOfSheets(); i < len; i++) {
Sheet sheet = wb.getSheetAt(i);
for (int j = 0; j <= sheet.getLastRowNum(); j++) {
User user = new User();
if (sheet == null) {
return null;
}
Row row = sheet.getRow(j);
if (row == null) {
return null;
}
//user.setId(getValue(row.getCell(0)));
user.setName(getValue(row.getCell(1)));
user.setPassword(getValue(row.getCell(2)));
users.add(user);
//user=null;
System.out.println(users);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(users);
return users;
}
}
-
(3)将任务1与任务2完成的用户综合管理软件源码上传到团队github仓库。
-
上传至GitHub
-
三、实验总结
1. 任务时间列表(单位:分钟)
任务内容 | 预计花费时长 | 实际花费时长 |
---|---|---|
创建用户综合管理软件的Java web项目,项目名称自拟 | 1 | 1 |
在项目中添加mysql的驱动支持包 | 2 | 4 |
分别用手工或脚本方式创建mysql数据库及表 | 10 | 15 |
将用户综合管理软件案例代码导入到项目中 | 20 | 30 |
在Tomcat服务器中部署项目 | 10 | 10 |
测试运行用户信息的查询、更新、删除、显示等操作 | 20 | 15 |
为案例软件开发软件入口主界面,作为查询、更新、删除、显示的功能导航页面 | 30 | 40 |
为案例软件开发一个新功能,可将excel文件的用户信息批量导入数据库 | 200 | 200 |
将任务1与任务2完成的用户综合管理软件源码上传到团队github仓库 | 10 | 90 |
2. 成员分工
成员 | 分工 |
---|---|
张玉国 | 根据实体类映射创建数据库以及修改Sql语句,创建导航界面 |
邓思超 | 实现将包导入IDEA以及运行 |
马全财 | 完成增量开发内容 |
潘成荣 | 博客编写 |
3. 本次实验心得
成员 | 心得 |
---|---|
张玉国 | 本次实验我负责实体类映射到数据库并创建数据表,修改原有的sql语句使之符合自己创建的表结构及表名的分工任务,完成导航功能页面的实现。回顾了数据库原理的相关知识,体会到了团队合作在开发软件过程中的重要作用,提升了遇到问题-解决问题过程中的能力 |
邓思超 | 本次实验中,我负责将源程序导入工程以及配置相应设置确保程序的正常运行,本次实验我采用了Maven技术直接进行项目管理,在配置文件中直接写入相应依赖,在实验过程中回顾了相应知识。 |
马全财 | 本次实验我负责增量开发的功能实现,实现将Excel文件中的数据批量导入数据库,在将项目源码上传至GitHub时出现了访问权限的问题,最后使用我的账号上传。本次实验是第一次团队协作研究项目,体会到了出现问题多人协作的快捷性,更加体会到了团队的重要性 |
潘成荣 | 本次实验中我负责博文的撰写,我们完成了软件开发环境部署,练习了mysql数据库创建和连接访问技术,对于之前所学的数据库开发技术进行了再次认识,协助小组其他成员完成本次作业任务,本次实验也是第一次开始初步的软件开发,希望后续任务一步步跟进完成。 |