车位管理系统
前言
用JSP+Servlet+JS+JDBC实现简单的车位管理系统
一、工具使用
MyEclipse SqlServer
二、整体思路
三、数据库表
1.用户表
2.车位信息表
三、功能实现步骤(源码):
a:项目结构
b:书写bean dao util
1.书写bean
package cn.myharr.bean;
public class User {
private int id;
private String userName;
private String pwd;
private String sex;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(int id, String userName, String pwd, String sex, int age) {
super();
this.id = id;
this.userName = userName;
this.pwd = pwd;
this.sex = sex;
this.age = age;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
package cn.myharr.bean;
public class Carport {
private int id;
private String name;
private int number;
private String address;
private String occupy;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getOccupy() {
return occupy;
}
public void setOccupy(String ageoccupy) {
this.occupy = ageoccupy;
}
public Carport(int id, String name, int number, String address,
String occupy) {
super();
this.id = id;
this.name = name;
this.number = number;
this.address = address;
this.occupy = occupy;
}
public Carport() {
super();
// TODO Auto-generated constructor stub
}
}
2.UserDao代码
package cn.myharr.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import cn.myharr.bean.Carport;
import cn.myharr.bean.User;
import cn.myharr.util.DBUtil;
public class UserDao {
public static boolean login(String userName,String pwd){
boolean flag = false;
Connection conn = DBUtil.getConn();
String sql = "select * from users where user_name = ? and pwd = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setObject(1,userName);
ps.setObject(2,pwd);
rs = ps.executeQuery();
if(rs.next()){
flag = true;
}else{
flag = false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBUtil.close(conn, ps, null);
return flag;
}
public static ArrayList<Carport> getUsers(){
boolean flag = false;
Connection conn = DBUtil.getConn();
String sql = "select * from carport";
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList<Carport> carports = new ArrayList<Carport>();
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
Carport carport = new Carport();
carport.setId(rs.getInt("id"));
carport.setName(rs.getString("name"));
carport.setNumber(rs.getInt("number"));
carport.setAddress(rs.getString("address"));
carport.setOccupy(rs.getString("occupy"));
carports.add(carport);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBUtil.close(conn, ps, null);
return carports;
}
public static Carport getUserById(Integer id){
Connection conn = DBUtil.getConn();
String sql = "select * from carport where id = ?";
PreparedStatement ps = null;
ResultSet rs = null;
Carport carport = null;
try {
ps = conn.prepareStatement(sql);
ps.setObject(1, id);
rs = ps.executeQuery();
if(rs.next()){
carport = new Carport();
carport.setId(rs.getInt("id"));
carport.setName(rs.getString("name"));
carport.setNumber(rs.getInt("number"));
carport.setAddress(rs.getString("address"));
carport.setOccupy(rs.getString("occupy"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBUtil.close(conn, ps, null);
return carport;
}
}
3.DBUtil代码
package cn.myharr.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConn(){
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName = MyDB", "sa", "1");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn,PreparedStatement ps,ResultSet rs){
try {
if(conn!=null)
conn.close();
if(ps!=null)
ps.close();
if(rs!=null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int update(String sql,Object[] objs){
int count = 0;
Connection conn = getConn();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < objs.length; i++) {
ps.setObject(i+1, objs[i]);
}
count = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
close(conn, ps, null);
}
return count;
}
}
c:需要实现的功能:
1.注册功能( 表单验证)
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 'reg.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">
-->
</head>
<body>
<form action="regServlet" method="post" onsubmit="return pass()">
用户名:<input type="text" id ="userName" name="userName" οnblur="userNameCheck()"><span id = "userNameShow"></span><br>
密码:<input type="text" id="pwd" name="pwd" onblur="pwdCheck()"><span id = "pwdShow"></span><br>
性别:<input type="text" id = "sex" name="sex"οnblur="sexCheck()"><span id = "sexShow"></span><br>
年龄:<input type="text" id = "age" name="age" οnblur="ageCheck()"><span id = "ageShow"></span><br>
<input type="submit" value="注册">
</form>
<script>
var flag = false;
function check(value,show){
var value = document.getElementById(value).value;
var show = document.getElementById(show);
if(value.length = ""||value == undefined){
show.innerText = "不能为空";
show.style.color="reg";
flag = false;
}else{
show.innerText = "通过";
show.style.color="green";
flag = true;
}
}
function userNameCheck(){
return check('userName','userNameShow');
}
function pwdCheck(){
return check('pwd','pwdShow');
}
function sexCheck(){
return check('sex','sexShow');
}
function ageCheck(){
return check('age','ageShow');
}
function pass(){
if(userNameCheck()&&pwdCheck()&&sexCheck()&&ageCheck()){
flag = true;
}else{
flag = false;
}
}
</script>
</body>
</html>
Servlet代码
package cn.myharr.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.myharr.dao.UserDao;
import cn.myharr.util.DBUtil;
public class RegServlet 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 {
// 1.设置编码
request.setCharacterEncoding("utf-8");
// 2.获取参数 userName pwd
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
String sex = request.getParameter("sex");
String strAge = request.getParameter("age");
Integer age = Integer.parseInt(strAge);
// 3.sql语句
String sql = "insert into users values(?,?,?,?)";
Object[] objs = { userName, pwd, sex, age };
int count = DBUtil.update(sql, objs);
// 4.把参数放到数组objs
// 5.调用DBUtil.update(sql,objs);
// 4.响应
// 登录成功跳转到列表页面
// 登录失败跳转到本页面
if (count > 0) {
response.sendRedirect("login.jsp");
} else {
response.sendRedirect("reg.jsp");
}
}
}
2.登录功能
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">
-->
</head>
<body>
<form action="loginServlet" method="post">
用户名:
<input type="text" name="userName"><br/>
密码:
<input type="text" name="pwd"><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
Servlet代码
package cn.myharr.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.myharr.dao.UserDao;
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 {
// 1.设置编码
request.setCharacterEncoding("utf-8");
// 2.获取参数 userName pwd
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
// 3.调用UserDao.login(userName,pwd);
boolean flag = UserDao.login(userName, pwd);
// 4.响应
// 登录成功跳转到列表页面
// 登录失败跳转到本页面
if (flag) {
response.sendRedirect("userList");
} else {
response.sendRedirect("login.jsp");
}
}
}
3.列表功能
jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'reg.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">
table,tr,th,td{
border:1px solid #000;
}
</style>
</head>
<body>
<a href="insert.jsp">车位新增</a>
<table cellspacing="0">
<tr>
<th>
编号
</th>
<th>
名称
</th>
<th>
车位号
</th>
<th>
地址
</th>
<th>
是否被占用
</th>
<th>
操作
</th>
</tr>
<c:forEach items="${carports}" var="carport">
<tr>
<td>
${carport.id}
</td>
<td>
${carport.name}
</td>
<td>
${carport.number}
</td>
<td>
${carport.address}
</td>
<td>
${carport.occupy}
</td>
<td>
<a href="updateToServlet?id=${carport.id}">修改</a><a href="deleteServlet?id=${carport.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Servlet代码
package cn.myharr.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.myharr.bean.Carport;
import cn.myharr.bean.User;
import cn.myharr.dao.UserDao;
public class UserListServlet 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 {
ArrayList<Carport> carports = UserDao.getUsers();
request.setAttribute("carports", carports);
request.getRequestDispatcher("userList.jsp").forward(request, response);
}
}
4.新增功能(表单验证)
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 'reg.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">
-->
</head>
<body>
<form action="insertServlet" method="post">
名称:
<input type="text" name="name">
<br>
编号:
<input type="text" name="number">
<br>
地址:
<input type="text" name="address">
<br>
是否被占用:
<input type="text" name="occupy">
<br>
<input type="submit" value="注册">
</form>
</body>
</html>
Servlet代码
package cn.myharr.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.myharr.util.DBUtil;
public class InsertServlet 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 {
// 1.设置编码
request.setCharacterEncoding("utf-8");
// 2.获取参数 userName pwd
String name = request.getParameter("name");
String strNumber = request.getParameter("number");
Integer number = Integer.parseInt(strNumber);
String address = request.getParameter("address");
String occupy = request.getParameter("occupy");
// 3.sql语句
String sql = "insert into carport values(?,?,?,?)";
Object[] objs = { name, number, address, occupy };
int count = DBUtil.update(sql, objs);
// 4.把参数放到数组objs
// 5.调用DBUtil.update(sql,objs);
// 4.响应
// 登录成功跳转到列表页面
// 登录失败跳转到本页面
response.sendRedirect("userList");
}
}
5.详情功能
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 'reg.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">
-->
</head>
<body>
<form action="updateServlet" method="post">
编号
<input type="text" name="id" value = "${carport.id}">
<br/>
名称:
<input type="text" name="name" value = "${carport.name}">
<br>
车位号:
<input type="text" name="number" value = "${carport.number}">
<br>
地址:
<input type="text" name="address" value = "${carport.address}">
<br>
是否被占用:
<input type="text" name="occupy" value = "${carport.occupy}">
<br>
<input type="submit" value="确定">
</form>
</body>
</html>
6.修改功能
Servlet代码
package cn.myharr.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.myharr.bean.Carport;
import cn.myharr.bean.User;
import cn.myharr.dao.UserDao;
import cn.myharr.util.DBUtil;
public class UpdateToServlet 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 {
// 1.设置编码
request.setCharacterEncoding("utf-8");
// 2.获取参数 userName pwd
String strId = request.getParameter("id");
Integer id = Integer.parseInt(strId);
//3.
Carport carport = UserDao.getUserById(id);
request.setAttribute("carport", carport);
request.getRequestDispatcher("update.jsp").forward(request, response);
}
}
package cn.myharr.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.myharr.util.DBUtil;
public class UpdateServlet 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 {
// 1.设置编码
request.setCharacterEncoding("utf-8");
// 2.获取参数 userName pwd
String strId = request.getParameter("id");
Integer id = Integer.parseInt(strId);
String name = request.getParameter("name");
String strNumber = request.getParameter("number");
Integer number = Integer.parseInt(strNumber);
String address = request.getParameter("address");
String occupy = request.getParameter("occupy");
// 3.sql语句
String sql = "update carport set name = ?,number = ?,address = ?,occupy = ? where id = ?";
Object[] objs = { name, number, address, occupy, id };
int count = DBUtil.update(sql, objs);
// 4.把参数放到数组objs
// 5.调用DBUtil.update(sql,objs);
// 4.响应
// 登录成功跳转到列表页面
// 登录失败跳转到本页面
response.sendRedirect("userList");
}
}
7.删除功能
Servlet代码
package cn.myharr.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.myharr.dao.UserDao;
import cn.myharr.util.DBUtil;
public class DeleteServlet 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 {
// 1.设置编码
request.setCharacterEncoding("utf-8");
// 2.获取参数 userName pwd
String strId = request.getParameter("id");
Integer id = Integer.parseInt(strId);
// 3.sql语句
String sql = "delete from carport where id = ?";
Object[] objs = {id};
int count = DBUtil.update(sql, objs);
response.sendRedirect("userList");
}
}
四、成果