JavaWeb项目缺陷管理系统DEMO-javaweb项目缺陷管理系统demo.md

焦虑烧麦 137 2022-05-02

项目演示

项目缺陷管理系统DEMO演示.gif

想把这个项目上传到服务器上,但是服务器上的MySQL有问题,死活连不上....Tomcat的配置也有问题.....导致现在无法实现,待我学习学习,择日上传/(ㄒoㄒ)/~~

开发环境&工具

首先需要新建一个JavaEE的项目,参考这篇文章IDEA创建JavaWeb项目

  • jdk1.8
  • IDEA企业版
  • MySQL8.0.21
  • 数据库:Navicat Premium

项目结构

项目结构.jpg

新建数据库

这里我使用的是Navicat Premium,新建一个数据库,然后建立如下table
data.jpg

建表数据

CREATE TABLE `issue_list`  (
  `id` varchar(255) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NOT NULL COMMENT '序号',
  `name` varchar(255) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NOT NULL COMMENT '名称',
  `type` varchar(255) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NOT NULL COMMENT '类型',
  `remark` varchar(255) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NOT NULL COMMENT '备注',
  `state` varchar(255) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NOT NULL COMMENT '状态',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

插入数据

INSERT INTO `issue_list` VALUES ('1002', 'DataErr', '数据库类型', '数据有误', '已修复');
INSERT INTO `issue_list` VALUES ('1003', 'DataEdit', '数据库类型', '编辑数据不成功', '未修复');
INSERT INTO `issue_list` VALUES ('1004', 'DataAdd', '数据库类型', '新增数据不成功', '已修复');
INSERT INTO `issue_list` VALUES ('1005', 'demo', '其他类型', '其他错误', '未修复');
INSERT INTO `issue_list` VALUES ('2021010416001704', 'demo1', 'demo1', 'demo2', '');

修改pom.xml

在pom.xml中加入MySQL的依赖
sqlcon.jpg
增加如下代码

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
</dependency>

注: version标签中需填写自己的MySQL版本

修改resources

在resources目录下,增加application.properties
application.jpg

后端代码

dao包

Dao.java

这一部分代码主要是与MySQL数据库连接,通过sql查询返回List对象
批量执行,实现插入修改,成功返回TRUE,失败返回FALSE

第一步 导包

import javax.naming.*;
import javax.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.*;

代码块

public class Dao {
    public static String flag = "";
    public static ResourceBundle rb = ResourceBundle.getBundle("application");

    private static Connection getConnection() {
        Connection con = null;
        String type = rb.getString("TYPE");
        String jndiName = rb.getString("JNDI");
        if (type.equals("JNDI")) {
            con = getConnectionForJNDI(jndiName);
        }
        if (type.equals("JDBC")) {
            con = getConnectionForJDBC();
        }
        return con;
    }

    /**
     * 取得数据库连接(JNDI)
     */
    private static Connection getConnectionForJNDI(String jndiName) {

        DataSource ds = null;

        InitialContext ic;

        try {
            ic = new InitialContext();

            ds = (DataSource) ic.lookup(jndiName);

            return (Connection) ds.getConnection();

        } catch (SQLException e) {

            flag = "数据库连接失败!";

            return null;
        } catch (NamingException e) {

            flag = "数据库连接失败,数据库连接池参数配置错误!";

            return null;
        }
    }

    /**
     * 取得数据库连接(JDBC)
     */
    private static Connection getConnectionForJDBC() {
        Connection conn = null;
        String jdbcUrl = rb.getString("JDBC_URL");
        String jdbcDriver = rb.getString("JDBC_DRIVER");
        String user = rb.getString("USER");
        String pwd = rb.getString("PASSWORD");
        try {
            Class.forName(jdbcDriver);
            conn = (Connection) DriverManager.getConnection(jdbcUrl, user, pwd);
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
            flag = "数据库连接失败,JDBC连接参数配置不正确!";
        } catch (SQLException e) {
            e.printStackTrace();
            flag = "数据库连接失败!";
        }
        return conn;
    }

    public static Map<String, String> queryForMap(String SQL) {
        Map<String, String> resultData = new HashMap<String, String>();
        Connection con = null;
        try {
            con = getConnection();
            PreparedStatement ps = con.prepareStatement(SQL);
            ResultSet rs = ps.executeQuery();
            ResultSetMetaData rsm = ps.getMetaData();
            int ColCount = rsm.getColumnCount();
            boolean state = true;
            while (rs.next()) {
                if (state) {
                    for (int i = 1; i <= ColCount; i++) {
                        String key = rsm.getColumnName(i).trim();
                        String val = rs.getString(i) == null ? "" : rs.getString(i).trim();
                        resultData.put(key, val);
                    }
                    state = false;
                } else break;
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception e) {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception ignored) {
                }
            }
            e.printStackTrace();
            resultData = null;
        }
        return resultData;
    }

    /**
     * 通过sql查询返回List<map>对象
     */
    public static List<Map<String, String>> queryForList(String SQL) {
        List<Map<String, String>> resultData = new ArrayList<Map<String, String>>();
        Connection con = null;
        try {
            con = getConnection();
            PreparedStatement ps = con.prepareStatement(SQL);
            ResultSet rs = ps.executeQuery();
            ResultSetMetaData rsm = ps.getMetaData();
            int ColCount = rsm.getColumnCount();  //列数.
            int k = 0;
            while (rs.next()) {
                Map<String, String> hm = new HashMap<String, String>();
                for (int i = 1; i <= ColCount; i++) {
                    String key = "";
                    String val = "";
                    key = rsm.getColumnName(i).trim();
                    val = rs.getString(i) == null ? "" : rs.getString(i).trim();
                    hm.put(key, val);
                }
                resultData.add(k, hm);
                k++;
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception e) {
            if (con != null) {
                try {
                    con.close();
                } catch (Exception ignored) {
                }
            }
            e.printStackTrace();
            resultData = null;
        }
        return resultData;
    }

    /**
     * 批量执行,实现插入修改,成功返回TRUE,失败返回FALSE
     */
    public static Boolean updateBatch(String... SQL) {
        Connection con = null;
        try { con = getConnection();
            con.setAutoCommit(false);
            Statement stmt = con.createStatement();
            for (String sql : SQL) { if (sql != null && !sql.equals("")) stmt.addBatch(sql); }
            stmt.executeBatch();
            stmt.close();
            con.commit();
            con.setAutoCommit(true);
            con.close();
        } catch (SQLException e) { e.printStackTrace();
            try { con.rollback();
                con.close();
                return false;
            } catch (Exception e2) { return false; }
        }
        return true;
    }

}

IssueDao.java

插入mysql时遇到问题 java.sql.BatchUpdateException: Incorrect string value: '\xC3\xA6\xC2\x98\xC2
多半是创建数据库时是默认的编码
尝试一下把数据库编码改成UTF-8

import com.zcloud.issue.entity.Issue;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class IssueDao {
    /**
     *添加缺陷
     * @return
     */
    public void create(Issue issue){
        String[] sql = new String[1];

        sql[0]= "insert  into issue_list "
                +" ( id, name , type , remark , state )"
                +" values ( "
                +"'"+issue.getId()+"',"
                +"'"+issue.getName()+"',"
                +"'"+issue.getType()+"',"
                +"'"+issue.getRemark()+"',"
                +"'"+issue.getState()+"'"
                +" ) ";

        Dao.updateBatch(sql[0]);
    }

    /**
     *更新缺陷
     * @return
     */
    public void update(Issue issue){
        String[] sql = new String[1];
        sql[0]= "update issue_list set"
                +" name = "+"'"+issue.getName()+"',"
                +" type = "+"'"+issue.getType()+"',"
                +" remark = "+"'"+issue.getRemark()+"',"
                +" state = "+"'"+issue.getState()+"'"
                +" where id = "+issue.getId();
        Dao.updateBatch(sql[0]);
    }

    /**
     * 删除缺陷
     */
    public void delete(String id){
        String[] sql = new String[1];
        sql[0]= " delete from issue_list where id="+id;
        Dao.updateBatch(sql);
    }

    /**
     * 查询缺陷列表
     * @param
     * @return
     */
    public List<Issue> findIssueList(){
        List<Issue> issues =new ArrayList<>();
        String  sql= " select * from  issue_list order by id";
        for (Map<String, String> map:Dao.queryForList(sql)){
            Issue issue =new Issue();
            issue.setId(map.get("id"));
            issue.setName(map.get("name"));
            issue.setType(map.get("type"));
            issue.setRemark(map.get("remark"));
            issue.setState(map.get("state"));
            issues.add(issue);
        }
        return issues;
    }

    /**
     * 查询缺陷详情
     * @param id
     * @return
     */
    public Issue findIssueOne(String id){
        String  sql= " select * from issue_list where id="+id;
        Map<String, String> map = Dao.queryForMap(sql);

        Issue issue =new Issue();
        issue.setId(map.get("id"));
        issue.setName(map.get("name"));
        issue.setType(map.get("type"));
        issue.setRemark(map.get("remark"));
        issue.setState(map.get("state"));

        return issue;
    }

    /**
     * 查询缺陷列表
     * @param
     * @return
     */
    public List<Issue> findByName(String name){
        List<Issue> issues =new ArrayList<>();
        String  sql= " select * from  issue_list " + "where name like '%" +name+"%'"+ " order by id";
        for (Map<String, String> map:Dao.queryForList(sql)){
            Issue issue =new Issue();
            issue.setId(map.get("id"));
            issue.setName(map.get("name"));
            issue.setType(map.get("type"));
            issue.setRemark(map.get("remark"));
            issue.setState(map.get("state"));
            issues.add(issue);
        }
        return issues;
    }

}

entity包

这一部分主要是构造方法

Issue.java

public class Issue {
    private String id = "";
    private String name = "";
    private String type = "";
    private String remark = "";
    private String state = "";

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}

service包

做一些判断

IssueManager.java

import com.zcloud.issue.dao.IssueDao;
import com.zcloud.issue.entity.Issue;
import com.zcloud.issue.util.Random;

import java.util.List;

public class IssueManager {

    private final IssueDao issueDao = new IssueDao();

    public Boolean saveIssue(Issue issue) throws Exception {
        try {
            //判断issue是新增还是修改
            if (issue.getId().equals("")) {
                //执行新增
                issue.setId(Random.next());
                issueDao.create(issue);
            } else {
                //执行更新
                issueDao.update(issue);
            }
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    public List<Issue> find(String name) {
        if (name == null) return issueDao.findIssueList();
        return issueDao.findByName(name);
    }

}

util包

Random.java

import javax.xml.crypto.Data;
import java.util.Date;

public class Random {

    private static final StringBuilder buf = new StringBuilder();

    private static int seq = 0;

    private static final int ROTATION = 99;

    public static String next() {
        if (seq > ROTATION) seq = 0;
        buf.delete(0,buf.length());
        Date date = new Date();

        date.setTime(System.currentTimeMillis());

        return String.format("%1$tY%1$tm%1$td%1$tH%1$tM%1$tS%2$02d",date,seq++);
    }
}

JSP代码

在webapp下创建这么几个.jsp文件
20210105_151521.jpg

webapp

这里就是一些前端加后端的代码了

Hello.jsp

欢迎页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>项目缺陷管理DEMO</title>
    <style type="text/css">
        body{
           background: #646b7a;
        }
    </style>
</head>
<body>
    <div class="wall" align="center">
        <div align="center">
            <h1 style="color: #ffffff">
                点击进入项目缺陷管理系统
            </h1>
        </div>
        <form action="<%=request.getContextPath()%>/issueList.jsp" method="post">
            <input type="button" name="return" value="进入缺陷列表" onclick="loginIssueList()"/>
        </form>
        <div style="color: #007ec2">
            <h1>DEMO</h1>
        </div>
    </div>
</body>
<script>
    loginIssueList = () => {
        window.location.href = "issueList.jsp"
    }
</script>
</html>

issueList.jsp

缺陷列表

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.zcloud.issue.service.*" %>
<%@ page import="com.zcloud.issue.entity.Issue" %>
<%@ page import="java.util.List" %>
<%
    String name= request.getParameter("name");
    IssueManager issueManager = new IssueManager();
    List<Issue> issueList = issueManager.find(name);
%>
<html>
<head>
    <title>项目缺陷管理</title>
</head>
<body>
<Form action="issueList.jsp" method="post">
    请输入缺陷名称:
    <input name="name" value="<%=name==null?"":name%>">
    <input type="submit" value="查询">
    <input type="button" value="新增" onclick="onAdd()">
</Form>
<table style="border-collapse:collapse;border:none">
    <thead>
    <tr>
        <th style="border:solid #000 1px;">编号</th>
        <th style="border:solid #000 1px;">名称</th>
        <th style="border:solid #000 1px;">类型</th>
        <th style="border:solid #000 1px;">描述</th>
        <th style="border:solid #000 1px;">状态</th>
        <th style="border:solid #000 1px;">操作</th>
    </tr>
    </thead>
    <tbody>
    <% for (Issue issue : issueList) { %>
    <tr>
        <td style="border:solid #000 1px;"><%=issue.getId()%>
        </td>
        <td style="border:solid #000 1px;"><%=issue.getName()%>
        </td>
        <td style="border:solid #000 1px;"><%=issue.getType()%>
        </td>
        <td style="border:solid #000 1px;"><%=issue.getRemark()%>
        </td>
        <td style="border:solid #000 1px;"><%=issue.getState()%>
        </td>
        <td style="border:solid #000 1px;">
            <input type="button" name="delete" value="删除"
                   onclick="deleteById('<%=issue.getId()%>')"/>
            <input type="button" name="edit" value="编辑"
                   onclick="onEdit('<%=issue.getId()%>')"/>
        </td>
    </tr>
    <% } %>
    </tbody>
</table>
<span>共有:<%=issueList.size()%>个缺陷</span>

</body>
<script>
    deleteById = (id) => {
        window.location.href = "issueDelete.jsp?id=" + id
    }
    onAdd = () => {
        window.location.href = "issueEdit.jsp"
    }
    onEdit = (id) => {
        window.location.href = "issueEdit.jsp?id=" + id
    }
</script>
</html>

issueDelete.jsp

删除缺陷

<%@ page import="com.zcloud.issue.dao.IssueDao" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>删除缺陷</title>
</head>
<body>
<%
    String id = request.getParameter("id");
    IssueDao issueDao = new IssueDao();
    String state = "";
    try {
        issueDao.delete(id);
        state = "删除成功!";
    } catch (Exception exception) {
        state = "删除失败!";
    }

%>
<H1><%=state%></H1>
<input type="button" name="return" value="返回缺陷列表"
       onclick="returnIssueList()"/>
</body>

<script>
    returnIssueList = () => {
        window.location.href = "issueList.jsp"
    }
</script>
</html>

issueEdit.jsp

缺陷编辑

<%@ page import="com.zcloud.issue.entity.Issue" %>
<%@ page import="com.zcloud.issue.dao.IssueDao" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>提交缺陷</title>
</head>
<%
    String id = request.getParameter("id")==null?"":request.getParameter("id");
    IssueDao issueDao = new IssueDao();
    Issue issue = new Issue();
    if( ! id.equals("")) issue=issueDao.findIssueOne(id);
%>
<body>
<form action="IssueSave.jsp" method="post">
    <label>缺陷编号:<input name="id" value="<%=id%>" disabled/></label><br/>
    <label>缺陷名称:<input name="name" value="<%=issue.getName()%>"/></label><br/>
    <label>缺陷类型:<input name="type" value="<%=issue.getType()%>"/></label><br/>
    <label>缺陷描述:<input name="remark" value="<%=issue.getRemark()%>"/></label><br/>
    <label>缺陷状态:<input name="state" value="<%=issue.getState()%>"/></label><br/>
    <input type="submit" value="提交">&nbsp;&nbsp;
    <input type="reset" value="重置">
</form>
</body>
</html>

issueSave.jsp

保存成功页面

<%@ page import="com.zcloud.issue.entity.Issue" %>
<%@ page import="com.zcloud.issue.service.IssueManager" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>提交缺陷</title>
</head>
<body>
<%
    Issue issue = new Issue();
    issue.setName(request.getParameter("name"));
    issue.setType(request.getParameter("type"));
    issue.setRemark(request.getParameter("remark"));
    issue.setRemark(request.getParameter("state"));

    String state = "";
    IssueManager issueManager = new IssueManager();
    try {
        issueManager.saveIssue(issue);
        state = "保存成功!";
    } catch (Exception e) {
        e.printStackTrace();
        state = "保存失败!异常信息:" + e.getMessage();
    }
%>
<H1><%=state%></H1>
<input type="button" name="return" value="返回缺陷列表"
       onclick="returnIssueList()"/>
</body>

<script>
    returnIssueList = () => {
        window.location.href = "issueList.jsp"
    }
</script>
</html>

到这里就可以启动服务查看效果了.

Github源码地址:https://github.com/Jaolvv/Java-WebLearningPlan/tree/master/JavaWeb/%E9%A1%B9%E7%9B%AE%E7%BC%BA%E9%99%B7%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9FDEMO