企业管理系统
此次项目是为了毕业论文而写,项目的主要内容是企业各部门之间的一些任务完成情况,主要通过SQLServer MyEclipse jquery+CSS等来实现的下面是实现的效果图。
登录页面,用户买登录的时候运用jQuery可以判断用户名密码是否非空或没定义,如果非空或未定义则弹框用户名或密码错误。
代码展示
JSP代码展示
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
form{
margin-right: 400px;
margin-top: 400px;
margin-left: 600px;
}
form{
background-color: rgba(255,255,255,0.55);
width:300px;
height: 150px;
text-align: center;
margin: 0 auto;
top:50%;
margin-top: 380px;
border-radius:10px;
}
body{
background-image: url("image/dl1.jpg");
background-repeat:no-repeat;
background-size:100% 100%;
}
.s1{
margin-right: 20dp;
margin-top: 15px;
margin-bottom: 20px;
}
#s2{
color:purple;
margin: 0px 100px;
}
</style>
<script src="jquery-3.3.1.js"></script>
</head>
<body>
<form action="login" method="post">
用户名:<input type="text" name="name" class="s1" id="name" /><br/>
密码:<input type="password" name="pwd" class="s1" id="pwd" /><br/>
<input type="submit" id="s2" value="登陆"/><a href="register.jsp"><input type="button" value="注册"/></a>
</form>
<script type="text/javascript">
$(function(){
$("form").submit(function(){
var name=$("#name").val();
var pwd=$("#pwd").val();
var nameFlag = false;
var pwdFlag = false;
if(name=="" || name==undefined){
nameFlag = false;
alert("用户名不能为空!");
}else{
nameFlag = true;
}
if(pwd=="" || pwd==undefined){
pwdFlag = false;
alert("密码不能为空!");
}else{
pwdFlag = true;
}
if(nameFlag ==false && pwdFlag==false){
return false;
}else{
return true;
}
});
});
</script>
</body>
</html>
Servlet代码展示
package cn.hnpi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.hnpi.bean.User;
import cn.hnpi.util.DButil;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
boolean flag = false;
Connection conn = DButil.getConn();
PreparedStatement ps = null;
ResultSet rs = null;
List<User> users = new ArrayList<User>();
String sql ="select * from users where name = ? and pwd = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, pwd);
rs = ps.executeQuery();
if (rs.next()) {
User user = new User(rs.getInt(1), rs.getString(2), rs.getString(3));
users.add(user);
flag = true;
}else{
flag = false;
}
} catch (Exception e) {
// TODO: handle exception
}finally{
DButil.connClose(conn, rs, ps);
}
if (flag) {
response.sendRedirect("taskList");
}else{
HttpSession session = request.getSession();
session.setAttribute("users", users);
response.sendRedirect("login.jsp");
}
}
}
项目主页面效果图,增删改查都可在此页面点击
JSP页面展示
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="cn.hnpi.bean.Project"%>
<%@page import="cn.hnpi.bean.Task"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'taskList.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
body{
background-image: url("image/hh.jpg");
background-repeat: no-repeat;
background-size:100% 100%;
}
body{
margin-left: 280px;
margin-top: 270px;
}
table{
border-color: balck;
font-size: 25px;
}
form {
font-size: 25px;
}
</style>
</head>
<body>
<form action="taskList">
所属部门:<select name="ProjectId">
<option value="*" selected="selected">所有部门</option>
<%
List<Project> projects = (ArrayList) session
.getAttribute("projects");
for (Project project : projects) {
%>
<option value="<%=project.getProjectId()%>"><%=project.getProjectName()%></option>
<%
}
%>
</select> 任务内容:
<input type="text" name="content" />
任务状态:
<input type="radio" name="TaskState" value="1" />未完成
<input type="radio" name="TaskState" value="2" />已完成
<input type="radio" name="TaskState" checked="checked" value="*" />全部 <input type="submit" value="查询" />
<a href="addTask.jsp"><input type="button" value="添加" /></a>
</form>
<table>
<tr>
<td>所属部门</td>
<td>任务难度</td>
<td>任务内容</td>
<td>任务添加时间</td>
<td>任务状态</td>
<td>任务完成时间</td>
<td>操作</td>
</tr>
<tbody>
<%
List<Task> tasks = (List) session.getAttribute("tasks");
Map<Integer, String> map = (HashMap) session.getAttribute("map");
for (Task task : tasks) {
%>
<tr>
<td><%=map.get(task.getProjectID())%></td>
<td>
<%
if (task.getTaskPriority() == 1) {
%> 简单 <%
} else if (task.getTaskPriority() == 2) {
%> 一般 <%
} else if (task.getTaskPriority() == 3) {
%> 困难 <%
}
%>
</td>
<td><%=task.getContent()%></td>
<td><%=task.getAddTime()%></td>
<td>
<%
if (task.getTaskState() == 1) {
%> 完成任务 <%
} else if (task.getTaskState() == 2) {
%> 未完成 <%
}
%>
</td>
<td><%=task.getFinishedTime()%></td>
<td>
<%
if (task.getTaskState() == 1) {
%> <a></a> <%
} else if (task.getTaskState() == 2) {
%> <a href="taskUp?taskId=<%=task.getTaskID()%> "">完成任务</a> <%
}
%>
<a href="del?taskId=<%=task.getTaskID()%> ">删除</a>
</td>
</tr>
<%
}
%>
</tbody>
</table>
</body>
</html>
Servlet代码展示
package cn.hnpi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.hnpi.bean.Project;
import cn.hnpi.bean.Task;
import cn.hnpi.util.DButil;
public class TaskListServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
String ProjectId = request.getParameter("ProjectId");
String content = request.getParameter("content");
String TaskState = request.getParameter("TaskState");
Connection conn = DButil.getConn();
String sql = "select * from task where 1=1";
PreparedStatement ps = null;
ResultSet rs = null;
//下拉框
List<Project> projects = new ArrayList<Project>();
//任务列表
List<Task> tasks = new ArrayList<Task>();
//项目名称
Map<Integer, String> map =new HashMap<Integer, String>();
List<Object> prams = new ArrayList<Object>();
//判断页面传递过来是否有内容
boolean ProjectIdFlag =(ProjectId != null && !"".equals(ProjectId) && !"*".equals(ProjectId));
boolean contentFlag =(content != null && !"".equals(content));
boolean TaskStateFlag =(TaskState != null && !"".equals(TaskState) && !"*".equals(TaskState));
//如果有内容拼接相关sql语句存入所需参数
if(ProjectIdFlag){
sql+="and projectId=?";
prams.add(Integer.parseInt(ProjectId));
}
if(contentFlag){
sql+="and content like ?";
prams.add("%" + content + "%");
}
if(TaskStateFlag){
sql+="and TaskState=?";
prams.add(Integer.parseInt(TaskState));
}
try {
ps=conn.prepareStatement(sql);
//使用instanceof 判断列表中的参数类型,根据不同类型使用不同的set方法,可扩展其他类型
for(int i = 1;i<=prams.size();i++){
Object obj =prams.get(i-1);
if(obj instanceof Integer){
ps.setInt(i, (Integer) obj);
}
if(obj instanceof String){
ps.setString(i, (String) obj);
}
}
rs=ps.executeQuery();
while(rs.next()){
Task task = new Task(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getTimestamp(4),rs.getInt(5),rs.getTimestamp(6),rs.getInt(7));
tasks.add(task);
}
sql="select * from project";
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
Project project = new Project(rs.getInt(1),rs.getString(2)) ;
map.put(project.getProjectId(),project.getProjectName());
projects.add(project);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DButil.connClose(conn, rs, ps);
}
HttpSession session = request.getSession();
session.setAttribute("tasks", tasks);
session.setAttribute("map", map);
session.setAttribute("projects", projects);
response.sendRedirect("taskList.jsp");
}
}
添加部门信息页面效果图
JSP代码展示
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="cn.hnpi.bean.Project"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'TaskAdd.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
body{
background-image: url("image/lyh.jpg");
background-repeat: no-repeat;
background-size:100% 100%;
}
form{
background-color:pink;
width:600px;
height: 300px;
text-align: center;
margin-left:150px;
top:50%;
margin-top: 280px;
}
table{
width: 600px;
border-color:pink;
height: 300px;
border-radius:10px;
}
</style>
</head>
<body>
<form action="addTask" method="post">
<table border="1" cellspacing="0">
<tr>
<td colspan="2" align="center">添加任务</td>
</tr>
<tr>
<td>所属部门</td>
<td>
<select name="ProjectId">
<option>--请选择--</option>
<%
List<Project> projects = (List)session.getAttribute("projects");
for(Project project : projects){
%>
<option value="<%=project.getProjectId() %>"><%=project.getProjectName() %></option>
<%} %>
</select>
</td>
</tr>
<tr>
<td>任务优先级</td>
<td>
<input type="radio" name="TaskPriority" value="1"/>简单
<input type="radio" name="TaskPriority" value="2"/>一般
<input type="radio" name="TaskPriority" value="3"/>困难
</td>
</tr>
<tr>
<td>任务内容</td>
<td><textarea rows="3" cols="25" name="content"></textarea></td>
</tr>
<tr>
<td></td>
<td><a href="taskList"><input type="submit" value="添加"/></a>
<a href="taskList"><input type="button" value="返回到列表"/></a></td>
</tr>
</table>
</form>
</body>
</html>
Servlet代码展示
package cn.hnpi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.hnpi.util.DButil;
public class AddTaskServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String ProjectId = request.getParameter("ProjectId");
String TaskPriority = request.getParameter("TaskPriority");
String content = request.getParameter("content");
Timestamp addTime =new Timestamp(System.currentTimeMillis());
Connection conn = DButil.getConn();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "insert into task(ProjectId,TaskPriority,TaskState,content,addTime) values(?,?,?,?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1,ProjectId );
ps.setInt(2, Integer.parseInt(TaskPriority));
ps.setInt(3, 2);
ps.setString(4, content);
ps.setTimestamp(5, addTime);
ps.executeUpdate();
response.sendRedirect("taskList");
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}finally{
DButil.connClose(conn, rs, ps);
}
}
}
更新页面效果图
Servlet代码展示
package cn.hnpi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.hnpi.util.DButil;
public class taskUpServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
String taskId = request.getParameter("taskId");
Connection conn = DButil.getConn();
PreparedStatement ps= null;
String sql="update task set TaskState=1,FinishedTime=? where taskId=?";
try {
ps=conn.prepareStatement(sql);
ps.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
ps.setInt(2, Integer.parseInt(taskId));
ps.executeUpdate();
response.sendRedirect("taskList");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DButil.connClose(conn,null, ps);
}
}
}
删除页面效果图
Servlet代码展示
package cn.hnpi.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBaseIterators.ParentIterator;
import cn.hnpi.util.DButil;
public class delServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
String taskId1 = request.getParameter("taskId");
Connection conn = DButil.getConn();
PreparedStatement ps= null;
String sql="delete from task where taskId=?";
try {
ps=conn.prepareStatement(sql);
ps.setInt(1, Integer.parseInt(taskId1));
ps.executeUpdate();
response.sendRedirect("taskList");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DButil.connClose(conn,null, ps);
}
}
}
DBUtil代码展示
package cn.hnpi.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.print.DocFlavor.STRING;
public class DButil {
public static Connection getConn(){
String url="jdbc:sqlserver://localhost:1433;databaseName=MYDB";
String user="sa";
String pwd="1";
Connection conn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
try {
conn=DriverManager.getConnection(url,user,pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void connClose(Connection conn,ResultSet rs,PreparedStatement ps){
try {
if(conn!=null){
conn.close();
}
} catch (Exception e) {
// TODO: handle exception
}
try {
if(rs!=null){
rs.close();
}
} catch (Exception e) {
// TODO: handle exception
}try {
if(ps!=null){
ps.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
JavaBean代码展示
package cn.hnpi.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.print.DocFlavor.STRING;
public class DButil {
public static Connection getConn(){
String url="jdbc:sqlserver://localhost:1433;databaseName=MYDB";
String user="sa";
String pwd="1";
Connection conn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
try {
conn=DriverManager.getConnection(url,user,pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void connClose(Connection conn,ResultSet rs,PreparedStatement ps){
try {
if(conn!=null){
conn.close();
}
} catch (Exception e) {
// TODO: handle exception
}
try {
if(rs!=null){
rs.close();
}
} catch (Exception e) {
// TODO: handle exception
}try {
if(ps!=null){
ps.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
package cn.hnpi.bean;
import java.sql.Timestamp;
public class Task {
private Integer TaskID;
private Integer TaskPriority;
private String Content;
private Timestamp AddTime;
private Integer TaskState;
private Timestamp FinishedTime;
private Integer projectID;
public Task(Integer taskID, Integer taskPriority, String content,
Timestamp addTime, Integer taskState, Timestamp finishedTime,
Integer projectID) {
super();
TaskID = taskID;
TaskPriority = taskPriority;
Content = content;
AddTime = addTime;
TaskState = taskState;
FinishedTime = finishedTime;
this.projectID = projectID;
}
public Task() {
super();
// TODO Auto-generated constructor stub
}
public Integer getTaskID() {
return TaskID;
}
public void setTaskID(Integer taskID) {
TaskID = taskID;
}
public Integer getTaskPriority() {
return TaskPriority;
}
public void setTaskPriority(Integer taskPriority) {
TaskPriority = taskPriority;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public Timestamp getAddTime() {
return AddTime;
}
public void setAddTime(Timestamp addTime) {
AddTime = addTime;
}
public Integer getTaskState() {
return TaskState;
}
public void setTaskState(Integer taskState) {
TaskState = taskState;
}
public Timestamp getFinishedTime() {
return FinishedTime;
}
public void setFinishedTime(Timestamp finishedTime) {
FinishedTime = finishedTime;
}
public Integer getProjectID() {
return projectID;
}
public void setProjectID(Integer projectID) {
this.projectID = projectID;
}
}
package cn.hnpi.bean;
public class User {
private Integer id ;
private String name;
private String pwd;
public User(Integer id, String name, String pwd) {
super();
this.id = id;
this.name = name;
this.pwd = pwd;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}