Android“校园失物招领”App(期末篇)(一)
作者:互联网
Android Studio+ IDEA+Navicat
目录
前言
随着学校在校生的增加和校园面积的扩大,学生和老师经常来往于不同的场所,加之教师和学生携带东西的数量和各种活动的增多,很容易发生物品丢失无法及时找回以及捡拾到的东西无处归还的情况。为此,建立一个高效、简捷、能缩短找回失物时间的失物招领手机APP对促进和谐校园文化的发展起着很大的作用。
App功能
一、普通用户:
- 普通用户登录;
- 失物找回模块:浏览被捡物品、发布丢失信息、查看物品详情;
- 捡到物品信息:浏览丢失物品、发布捡到信息息、查看物品详情;
- 个人信息:修改个人信息、密码修改;
- 制作一个本系统使用帮助功能。
二、管理员用户的功能如下:
- 管理员用户登录;
- 对用户的管理:增、删、改、查;
- 对失物找回信息的管理:增、删、查;
- 对捡到物品信息的管理:增、删、查;
- 个人信息:修改个人信息、密码修改;
效果图
用户主页
用户登录
用户丢失模块
用户丢失模块功能
发布丢失信息
查看丢失物品详情
用户个人信息
修改个人信息
App使用帮助
管理员后台管理丢失物品
添加操作
删除操作
查询操作
管理员个人信息
管理用户操作
添加用户
修改用户
删除用户
查询用户
服务端
controller
AdminGetServlet.java(获取管理员信息)
package controller;
import dao.AdminDao;
import dao.UserDao;
import entity.Admin;
import util.JsonTool;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/AdminGetServlet")
public class AdminGetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//创建一个集合
List<Admin> adminList=new ArrayList<Admin>();
//创建操作类对象
AdminDao adminDao=new AdminDao();
//调用findAllAdmin()获取信息
adminList=adminDao.findAllAdmin();
//转换成json数据
String json= JsonTool.javaToJson(adminList);
//输出json对象
out.print(json);
}
}
AdminLoginServlet.java(管理员登录)
package controller;
import dao.AdminDao;
import entity.Admin;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/AdminLoginServlet")
public class AdminLoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//1.获取安卓端的用户名和密码
String usernameString=request.getParameter("username");
String passwordString=request.getParameter("password");
String checkcodeString=request.getParameter("code").toUpperCase();
//2.取出session对象中保存的验证码
String piccodeString=(String) request.getSession().getAttribute("piccode");
//out.print("piccode"+piccodeString);
//创建一个集合
List<Admin> adminList=new ArrayList<Admin>();
//创建操作类对象
AdminDao bookDao=new AdminDao();
//调用loginAdmin()获取信息
adminList=bookDao.loginAdmin(usernameString,passwordString);
if (adminList != null && adminList.size() > 0 && piccodeString.equals(checkcodeString)){
out.print("登录成功");
}else{
out.print("登录失败");
}
}
}
AdminUpdateServlet.java(管理员修改信息)
package controller;
import dao.AdminDao;
import dao.UserDao;
import entity.Admin;
import entity.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/AdminUpdateServlet")
public class AdminUpdateServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取修改用户数据
String username=request.getParameter("username");
String password=request.getParameter("password");
String turename=request.getParameter("turename");
String birthday=request.getParameter("birthday");
String address=request.getParameter("address");
String telephone=request.getParameter("telephone");
//获取数据存入
Admin admin=new Admin();
admin.setUsername(username);
admin.setPassword(password);
admin.setTurename(turename);
admin.setBirthday(birthday);
admin.setAddress(address);
admin.setTelephone(telephone);
//创建admindao对象
AdminDao adminDao=new AdminDao();
//调用添加方法
int flag=adminDao.editAdmin(admin);
//判断是否添加成功
if (flag>0){
out.print("修改成功");
}else{
out.print("修改失败");
}
}
}
FoundAddServlet.java(寻到物品添加)
package controller;
import dao.FoundDao;
import entity.Found;
import org.apache.commons.fileupload.FileItem;
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;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;
@WebServlet("/FoundAddServlet")
public class FoundAddServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取添加学生数据
Found found=new Found();
//book.setId(Integer.parseInt(request.getParameter("id")));
found.setFoundname(request.getParameter("foundname"));
found.setFoundnumber(request.getParameter("foundnumber"));
found.setFoundplace(request.getParameter("foundplace"));
found.setFounddate(request.getParameter("founddate"));
found.setFoundtelephone(request.getParameter("foundtelephone"));
String root = request.getSession().getServletContext().getRealPath("upload");//获取upload包的真实路径
/*found.setFoundphoto(request.getParameter("foundphoto"));*/
try {
DiskFileItemFactory factory = new DiskFileItemFactory();//创建一个磁盘工厂
ServletFileUpload upload = new ServletFileUpload(factory);//创建一个文件上传对象
List<FileItem> list = upload.parseRequest(request);//将request对象转换成集合
for (FileItem item : list) {
if (item.isFormField() == false) {//判断是否属于普通表单数据
//生成一个新的文件名,不重复,数据库存储的就是这个文件名,不重复的
String filename = UUID.randomUUID().toString() + ".jpg";
File f = new File(root + "\\" + filename);//创建一个文件指定它的路径和文件名
item.write(f);//将数据写入文件中
//out.print(filename);//输出的数据文件名
found.setFoundphoto(root + "\\" + filename);
//创建founddao对象
FoundDao foundDao=new FoundDao();
//调用添加方法
int flag=foundDao.addFound(found);
//判断是否添加成功
if (flag > 0) {
out.print("添加成功");
} else {
out.print("添加失败");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
FoundDeleteServlet.java(寻到物品删除)
package controller;
import dao.FoundDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/FoundDeleteServlet")
public class FoundDeleteServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取删除学生数据
String foundname=request.getParameter("foundname");
//创建founddao对象
FoundDao foundDao=new FoundDao();
//调用添加方法
int flag=foundDao.deleteFound(foundname);
//判断是否添加成功
if (flag>0){
out.print("删除成功");
}else{
out.print("删除失败");
}
}
}
FoundDetaiilsAddServlet.java(寻到物品详情添加)
package controller;
import dao.FoundDao;
import entity.Found;
import org.apache.commons.fileupload.FileItem;
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;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;
@WebServlet("/FoundDetailsAddServlet")
public class FoundDetaiilsAddServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取添加学生数据
Found found=new Found();
//book.setId(Integer.parseInt(request.getParameter("id")));
found.setFoundname(request.getParameter("foundname"));
found.setFoundnumber(request.getParameter("foundnumber"));
found.setFoundplace(request.getParameter("foundplace"));
found.setFounddate(request.getParameter("founddate"));
found.setFoundtelephone(request.getParameter("foundtelephone"));
found.setFounddetails(request.getParameter("founddetails"));
String root = request.getSession().getServletContext().getRealPath("upload");//获取upload包的真实路径
/*found.setFoundphoto(request.getParameter("foundphoto"));*/
try {
DiskFileItemFactory factory = new DiskFileItemFactory();//创建一个磁盘工厂
ServletFileUpload upload = new ServletFileUpload(factory);//创建一个文件上传对象
List<FileItem> list = upload.parseRequest(request);//将request对象转换成集合
for (FileItem item : list) {
if (item.isFormField() == false) {//判断是否属于普通表单数据
//生成一个新的文件名,不重复,数据库存储的就是这个文件名,不重复的
String filename = UUID.randomUUID().toString() + ".jpg";
File f = new File(root + "\\" + filename);//创建一个文件指定它的路径和文件名
item.write(f);//将数据写入文件中
//out.print(filename);//输出的数据文件名
found.setFoundphoto(root + "\\" + filename);
//创建founddao对象
FoundDao foundDao=new FoundDao();
//调用添加方法
int flag=foundDao.addDetaiilsFound(found);
//判断是否添加成功
if (flag > 0) {
out.print("添加成功");
} else {
out.print("添加失败");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
FoundFindDetailsServlet.java(寻到物品详情查询)
package controller;
import dao.FoundDao;
import entity.Found;
import util.JsonTool;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/FoundFindDetailsServlet")
public class FoundFindDetailsServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取查询字段
String fieldname=request.getParameter("fieldname");
String fieldvalue=request.getParameter("fieldvalue");
//创建一个集合
List<Found> foundList=new ArrayList<Found>();
//创建操作类对象
FoundDao foundDao=new FoundDao();
foundList=foundDao.findGetDetailsFound(fieldname,fieldvalue);
//转换成json数据
String json= JsonTool.javaToJson(foundList);
//输出json对象
out.print(json);
}
}
FoundFindServlet.java(寻到物品查询)
package controller;
import dao.FoundDao;
import dao.LostDao;
import entity.Found;
import entity.Lost;
import util.JsonTool;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/FoundFindServlet")
public class FoundFindServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取查询字段
String fieldname=request.getParameter("fieldname");
String fieldvalue=request.getParameter("fieldvalue");
//创建一个集合
List<Found> foundList=new ArrayList<Found>();
//创建操作类对象
FoundDao foundDao=new FoundDao();
foundList=foundDao.findFound(fieldname,fieldvalue);
//转换成json数据
String json= JsonTool.javaToJson(foundList);
//输出json对象
out.print(json);
}
}
FoundGetDetailsServlet.java(寻到物品详情显示)
package controller;
import dao.FoundDao;
import entity.Found;
import util.JsonTool;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/FoundGetDetailsServlet")
public class FoundGetDetailsServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//创建一个集合
List<Found> foundList=new ArrayList<Found>();
//创建操作类对象
FoundDao foundDao=new FoundDao();
//调用findDetailsFound()获取信息
foundList=foundDao.findDetailsFound();
//转换成json数据
String json= JsonTool.javaToJson(foundList);
//输出json对象
out.print(json);
}
}
FoundGetServlet.java(寻到物品显示)
package controller;
import dao.FoundDao;
import dao.UserDao;
import entity.Found;
import entity.User;
import util.JsonTool;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/FoundGetServlet")
public class FoundGetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//创建一个集合
List<Found> foundList=new ArrayList<Found>();
//创建操作类对象
FoundDao foundDao=new FoundDao();
//调用findAllFound()获取信息
foundList=foundDao.findAllFound();
//转换成json数据
String json= JsonTool.javaToJson(foundList);
//输出json对象
out.print(json);
}
}
ImageCodeServlet.java(验证码)
package controller;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
@WebServlet("/ImageCodeServlet")
public class ImageCodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//这个方法实现验证码的生成
BufferedImage bi=new BufferedImage(90, 40,BufferedImage.TYPE_INT_RGB);//创建图像缓冲区
Graphics g=bi.getGraphics(); //通过缓冲区创建一个画布
Color c=new Color(200,150,255); //创建颜色
//根据背景画了一个矩形框
g.setColor(c);//为画布创建背景颜色
g.fillRect(0, 0, 90,40); //fillRect:填充指定的矩形
char[] ch="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();//转化为字符型的数组
Random r=new Random();
int len=ch.length;
int index; //index用于存放随机数字
StringBuffer sb=new StringBuffer();
//验证码长度
for(int i=0;i<4;i++) {
index=r.nextInt(len);//产生随机数字
g.setColor(new Color(r.nextInt(88),r.nextInt(188),r.nextInt(255))); //设置颜色
g.drawString(ch[index]+"",(i*22)+6, 25);//画数字以及数字的位置
sb.append(ch[index]);
}
request.getSession().setAttribute("piccode",sb.toString()); //将数字保留在session中,便于后续的使用
ImageIO.write(bi, "JPG", response.getOutputStream());
}
}
LostAddServlet.java(丢失物品添加)
package controller;
import dao.LostDao;
import entity.Lost;
import org.apache.commons.fileupload.FileItem;
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;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;
@WebServlet("/LostAddServlet")
public class LostAddServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取添加丢失数据
Lost lost = new Lost();
//book.setId(Integer.parseInt(request.getParameter("id")));
lost.setLostname(request.getParameter("lostname"));
lost.setLostnumber(request.getParameter("lostnumber"));
lost.setLostplace(request.getParameter("lostplace"));
lost.setLostdate(request.getParameter("lostdate"));
lost.setLosttelephone(request.getParameter("losttelephone"));
String root = request.getSession().getServletContext().getRealPath("upload");//获取upload包的真实路径
/*lost.setLostphoto(request.getParameter("lostphoto"));*/
try {
DiskFileItemFactory factory = new DiskFileItemFactory();//创建一个磁盘工厂
ServletFileUpload upload = new ServletFileUpload(factory);//创建一个文件上传对象
List<FileItem> list = upload.parseRequest(request);//将request对象转换成集合
for (FileItem item : list) {
if (item.isFormField() == false) {//判断是否属于普通表单数据
//生成一个新的文件名,不重复,数据库存储的就是这个文件名,不重复的
String filename = UUID.randomUUID().toString() + ".jpg";
File f = new File(root + "\\" + filename);//创建一个文件指定它的路径和文件名
item.write(f);//将数据写入文件中
//out.print(filename);//输出的数据文件名
lost.setLostphoto(root + "\\" + filename);
//创建lostdao对象
LostDao lostDao=new LostDao();
//调用添加方法
int flag=lostDao.addLost(lost);
//判断是否添加成功
if (flag > 0) {
out.print("添加成功");
} else {
out.print("添加失败");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
LostDeleteServlet.java(丢失物品删除)
package controller;
import dao.LostDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/LostDeleteServlet")
public class LostDeleteServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取删除丢失数据
String lostname=request.getParameter("lostname");
//创建lostdao对象
LostDao lostDao=new LostDao();
//调用添加方法
int flag=lostDao.deleteLost(lostname);
//判断是否添加成功
if (flag>0){
out.print("删除成功");
}else{
out.print("删除失败");
}
}
}
LostDetailsAddServlet.java(丢失物品详情添加)
package controller;
import dao.LostDao;
import entity.Lost;
import org.apache.commons.fileupload.FileItem;
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;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;
@WebServlet("/LostDetailsAddServlet")
public class LostDetailsAddServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取添加丢失详情数据
Lost lost = new Lost();
//book.setId(Integer.parseInt(request.getParameter("id")));
lost.setLostname(request.getParameter("lostname"));
lost.setLostnumber(request.getParameter("lostnumber"));
lost.setLostplace(request.getParameter("lostplace"));
lost.setLostdate(request.getParameter("lostdate"));
lost.setLosttelephone(request.getParameter("losttelephone"));
lost.setLostdetails(request.getParameter("lostdetails"));
String root = request.getSession().getServletContext().getRealPath("upload");//获取upload包的真实路径
/*lost.setLostphoto(request.getParameter("lostphoto"));*/
try {
DiskFileItemFactory factory = new DiskFileItemFactory();//创建一个磁盘工厂
ServletFileUpload upload = new ServletFileUpload(factory);//创建一个文件上传对象
List<FileItem> list = upload.parseRequest(request);//将request对象转换成集合
for (FileItem item : list) {
if (item.isFormField() == false) {//判断是否属于普通表单数据
//生成一个新的文件名,不重复,数据库存储的就是这个文件名,不重复的
String filename = UUID.randomUUID().toString() + ".jpg";
File f = new File(root + "\\" + filename);//创建一个文件指定它的路径和文件名
item.write(f);//将数据写入文件中
//out.print(filename);//输出的数据文件名
lost.setLostphoto(root + "\\" + filename);
//创建lostdao对象
LostDao lostDao=new LostDao();
//调用添加方法
int flag=lostDao.addDetaiilsLost(lost);
//判断是否添加成功
if (flag > 0) {
out.print("添加成功");
} else {
out.print("添加失败");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
LostFindDetailsServlet.java(丢失物品详情查询)
package controller;
import dao.LostDao;
import entity.Lost;
import util.JsonTool;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/LostFindDetailsServlet")
public class LostFindDetailsServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取查询字段
String fieldname=request.getParameter("fieldname");
String fieldvalue=request.getParameter("fieldvalue");
//创建一个集合
List<Lost> lostList=new ArrayList<Lost>();
//创建操作类对象
LostDao lostDao=new LostDao();
lostList=lostDao.findGetDetailsLost(fieldname,fieldvalue);
//转换成json数据
String json= JsonTool.javaToJson(lostList);
//输出json对象
out.print(json);
}
}
LostFindServlet.java(丢失物品查询)
package controller;
import dao.LostDao;
import dao.UserDao;
import entity.Lost;
import entity.User;
import util.JsonTool;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/LostFindServlet")
public class LostFindServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取查询字段
String fieldname=request.getParameter("fieldname");
String fieldvalue=request.getParameter("fieldvalue");
//创建一个集合
List<Lost> lostList=new ArrayList<Lost>();
//创建操作类对象
LostDao lostDao=new LostDao();
lostList=lostDao.findLost(fieldname,fieldvalue);
//转换成json数据
String json= JsonTool.javaToJson(lostList);
//输出json对象
out.print(json);
}
}
LostGetDetailsServlet.java(丢失物品详情显示)
package controller;
import dao.FoundDao;
import dao.LostDao;
import dao.UserDao;
import entity.Found;
import entity.Lost;
import entity.User;
import util.JsonTool;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/LostGetDetailsServlet")
public class LostGetDetailsServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//创建一个集合
List<Lost> lostList=new ArrayList<Lost>();
//创建操作类对象
LostDao lostDao=new LostDao();
//调用findDetailsLost()获取信息
lostList=lostDao.findDetailsLost();
//转换成json数据
String json= JsonTool.javaToJson(lostList);
//输出json对象
out.print(json);
}
}
LostGetServlet.java(丢失物品获取)
package controller;
import dao.LostDao;
import dao.UserDao;
import entity.Lost;
import entity.User;
import util.JsonTool;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/LostGetServlet")
public class LostGetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//创建一个集合
List<Lost> lostList=new ArrayList<Lost>();
//创建操作类对象
LostDao lostDao=new LostDao();
//调用findAllLost()获取信息
lostList=lostDao.findAllLost();
//转换成json数据
String json= JsonTool.javaToJson(lostList);
//输出json对象
out.print(json);
}
}
UserAddServlet.java(丢失物品添加)
package controller;
import dao.UserDao;
import entity.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/UserAddServlet")
public class UserAddServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
User user=new User();
user.setUsername(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
user.setTurename(request.getParameter("turename"));
user.setBirthday(request.getParameter("birthday"));
user.setAddress(request.getParameter("address"));
user.setTelephone(request.getParameter("telephone"));
//创建userdao对象
UserDao userDao=new UserDao();
//调用添加方法
int flag=userDao.addUser(user);
//判断是否添加成功
if (flag>0){
out.print("添加成功");
}else{
out.print("添加失败");
}
}
}
UserDeleteServlet.java(用户删除)
package controller;
import dao.UserDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/UserDeleteServlet")
public class UserDeleteServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取删除学生数据
String username=request.getParameter("username");
//创建userdao对象
UserDao userDao=new UserDao();
//调用添加方法
int flag=userDao.deleteUser(username);
//判断是否添加成功
if (flag>0){
out.print("删除成功");
}else{
out.print("删除失败");
}
}
}
UserFindServlet.java(用户查询)
package controller;
import dao.UserDao;
import entity.User;
import util.JsonTool;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/UserFindServlet")
public class UserFindServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取查询字段
String fieldname=request.getParameter("fieldname");
String fieldvalue=request.getParameter("fieldvalue");
//创建一个集合
List<User> userList=new ArrayList<User>();
//创建userdao对象
UserDao userDao=new UserDao();
userList=userDao.findUser(fieldname,fieldvalue);
//转换成json数据
String json= JsonTool.javaToJson(userList);
//输出json对象
out.print(json);
}
}
UserGetServlet.java(用户显示)
package controller;
import dao.UserDao;
import entity.User;
import util.JsonTool;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/UserGetServlet")
public class UserGetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//创建一个集合
List<User> userList=new ArrayList<User>();
//创建操作类对象
UserDao userDao=new UserDao();
//调用findAllUser()获取信息
userList=userDao.findAllUser();
//转换成json数据
String json= JsonTool.javaToJson(userList);
//输出json对象
out.print(json);
}
}
UserLoginServlet.java(用户登录)
package controller;
import dao.UserDao;
import entity.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/UserLoginServlet")
public class UserLoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//1.获取安卓端的用户名和密码
String usernameString=request.getParameter("username");
String passwordString=request.getParameter("password");
String checkcodeString=request.getParameter("code").toUpperCase();
//2.取出session对象中保存的验证码
String piccodeString=(String) request.getSession().getAttribute("piccode");
//out.print("piccode"+piccodeString);
//创建一个集合
List<User> userList=new ArrayList<User>();
//创建操作类对象
UserDao bookDao=new UserDao();
//调用loginUser()获取信息
userList=bookDao.loginUser(usernameString,passwordString);
if (userList != null && userList.size() > 0 && piccodeString.equals(checkcodeString)){
out.print("登录成功");
}else{
out.print("登录失败");
}
}
}
UserUpdateServlet.java(用户修改)
package controller;
import dao.UserDao;
import entity.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/UserUpdateServlet")
public class UserUpdateServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取修改用户数据
String username=request.getParameter("username");
String password=request.getParameter("password");
String turename=request.getParameter("turename");
String birthday=request.getParameter("birthday");
String address=request.getParameter("address");
String telephone=request.getParameter("telephone");
//获取数据存入
User user=new User();
user.setUsername(username);
user.setPassword(password);
user.setTurename(turename);
user.setBirthday(birthday);
user.setAddress(address);
user.setTelephone(telephone);
//创建userdao对象
UserDao userDao=new UserDao();
//调用添加方法
int flag=userDao.editUser(user);
//判断是否添加成功
if (flag>0){
out.print("修改成功");
}else{
out.print("修改失败");
}
}
}
dao
AdminDao.java
package dao;
import entity.Admin;
import entity.User;
import util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class AdminDao {
public List<Admin> loginAdmin(String username, String password) {
//创建一个集合,存放信息
List<Admin> adminList = new ArrayList<Admin>();
//创建连接对象
Connection connection = null;
//创建语句对象
PreparedStatement ps = null;
//创建结果集对象
ResultSet rs = null;
//sql语句
String sqlString = "select * from admin where username =? and password=?";//查询的SQL语句
//连接对象初始化
connection = DBUtil.getConnection();
try {
//语句对象ps初始化
ps = connection.prepareStatement(sqlString);
//给占位符赋值
ps.setString(1, username);
ps.setString(2, password);
//执行sql语句
rs = ps.executeQuery();
while (rs.next()){
Admin admin=new Admin();
adminList.add(admin);
}
} catch (SQLException e) {
e.printStackTrace();
}
return adminList;
}
//实现获取数据,返回集合
public List<Admin> findAllAdmin() {
//创建一个集合,存放信息
List<Admin> adminList=new ArrayList<Admin>();
//创建连接对象
Connection connection=null;
//创建语句对象
PreparedStatement ps=null;
//创建结果集对象
ResultSet rs=null;
//sql语句
String sqlString="select username,password,turename,birthday,address,telephone from admin";
//连接对象初始化
connection=DBUtil.getConnection();
try {
//语句对象ps初始化
ps=connection.prepareStatement(sqlString);
//执行sql语句
rs=ps.executeQuery();
//获取结果集对象中的信息
while (rs.next()){
Admin admin=new Admin();
admin.setUsername(rs.getString(1));
admin.setPassword(rs.getString(2));
admin.setTurename(rs.getString(3));
admin.setBirthday(rs.getString(4));
admin.setAddress(rs.getString(5));
admin.setTelephone(rs.getString(6));
//将admin对象存入
adminList.add(admin);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return adminList;
}
//修改数据
public int editAdmin(Admin admin){
int flag=0;//标识位,用来判断是否添加成功
//1.创建一个连接对象
Connection conn=null;
//2.创建语句对象,执行sql语句
PreparedStatement ps=null;
//3.构造要执行的sql语句
String sqlString="update admin set password=?,turename=?,birthday=?,address=?,telephone=? where username=?";
try {
//4.连接对象初始化
conn= DBUtil.getConnection();
//5.语句对象初始化
ps=conn.prepareStatement(sqlString);
//6.占位符赋值
ps.setString(1, admin.getPassword());
ps.setString(2, admin.getTurename());
ps.setString(3, admin.getBirthday());
ps.setString(4, admin.getAddress());
ps.setString(5, admin.getTelephone());
ps.setString(6, admin.getUsername());
//7.执行sql语句
flag=ps.executeUpdate();//insert update delete需要调用此方法
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
}
FoundDao.java
package dao;
import entity.Found;
import entity.Lost;
import util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class FoundDao {
//创建一个添加的方法
public int addFound(Found found) {
int flag=0;//标识位,用来判断是否添加成功
//1.创建一个连接对象
Connection conn=null;
//2.创建语句对象,执行sql语句
PreparedStatement ps=null;
//3.构造要执行的sql语句
String sqlString="insert into found(foundname,foundnumber,foundplace,founddate,foundtelephone,foundphoto) value (?,?,?,?,?,?)";
try {
//4.连接对象初始化
conn= DBUtil.getConnection();
//5.语句对象初始化
ps=conn.prepareStatement(sqlString);
//6.占位符赋值
//ps.setInt(1, book.getId());
ps.setString(1, found.getFoundname());
ps.setString(2, found.getFoundnumber());
ps.setString(3, found.getFoundplace());
ps.setString(4, found.getFounddate());
ps.setString(5, found.getFoundtelephone());
ps.setString(6, found.getFoundphoto());
//7.执行sql语句
flag=ps.executeUpdate();//insert update delete需要调用此方法
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
//创建一个添加的方法
public int addDetaiilsFound(Found found) {
int flag=0;//标识位,用来判断是否添加成功
//1.创建一个连接对象
Connection conn=null;
//2.创建语句对象,执行sql语句
PreparedStatement ps=null;
//3.构造要执行的sql语句
String sqlString="insert into found(foundname,foundnumber,foundplace,founddate,foundtelephone,foundphoto,founddetails) value (?,?,?,?,?,?,?)";
try {
//4.连接对象初始化
conn= DBUtil.getConnection();
//5.语句对象初始化
ps=conn.prepareStatement(sqlString);
//6.占位符赋值
//ps.setInt(1, book.getId());
ps.setString(1, found.getFoundname());
ps.setString(2, found.getFoundnumber());
ps.setString(3, found.getFoundplace());
ps.setString(4, found.getFounddate());
ps.setString(5, found.getFoundtelephone());
ps.setString(6, found.getFoundphoto());
ps.setString(7,found.getFounddetails());
//7.执行sql语句
flag=ps.executeUpdate();//insert update delete需要调用此方法
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
//实现获取数据,返回集合
public List<Found> findAllFound() {
//创建一个集合,存放信息
List<Found> foundList = new ArrayList<Found>();
//创建连接对象
Connection connection = null;
//创建语句对象
PreparedStatement ps = null;
//创建结果集对象
ResultSet rs = null;
//sql语句
String sqlString = "select foundname,foundnumber,foundplace,founddate,foundtelephone,foundphoto from found";
//连接对象初始化
connection = DBUtil.getConnection();
try {
//语句对象ps初始化
ps = connection.prepareStatement(sqlString);
//执行sql语句
rs = ps.executeQuery();
//获取结果集对象中的信息
while (rs.next()) {
Found found = new Found();
found.setFoundname(rs.getString(1));
found.setFoundnumber(rs.getString(2));
found.setFoundplace(rs.getString(3));
found.setFounddate(rs.getString(4));
found.setFoundtelephone(rs.getString(5));
found.setFoundphoto(rs.getString(6));
//将found对象存入
foundList.add(found);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return foundList;
}
//删除数据
public int deleteFound(String foundname){
int flag=0;//标识位,用来判断是否添加成功
//1.创建一个连接对象
Connection conn=null;
//2.创建语句对象,执行sql语句
PreparedStatement ps=null;
//3.构造要执行的sql语句
String sqlString="delete from found where foundname=?";
try {
//4.连接对象初始化
conn= DBUtil.getConnection();
//5.语句对象初始化
ps=conn.prepareStatement(sqlString);
//6.占位符赋值
ps.setString(1,foundname);
//7.执行sql语句
flag=ps.executeUpdate();//insert update delete需要调用此方法
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
//按条件查询数据
public List<Found> findFound(String fieldname,String fieldvalue) {
//创建一个集合,存放信息
List<Found> foundList=new ArrayList<Found>();
//创建连接对象
Connection connection=null;
//创建语句对象
PreparedStatement ps=null;
//创建结果集对象
ResultSet rs=null;
//sql语句
String sqlString="select foundname,foundnumber,foundplace,founddate,foundtelephone,foundphoto from found where "+fieldname+"=?";
//连接对象初始化
connection=DBUtil.getConnection();
try {
//语句对象ps初始化
ps=connection.prepareStatement(sqlString);
//给占位符赋值
ps.setString(1,fieldvalue);
//执行sql语句
rs=ps.executeQuery();
//获取结果集对象中的信息
while (rs.next()){
Found found = new Found();
found.setFoundname(rs.getString(1));
found.setFoundnumber(rs.getString(2));
found.setFoundplace(rs.getString(3));
found.setFounddate(rs.getString(4));
found.setFoundtelephone(rs.getString(5));
found.setFoundphoto(rs.getString(6));
//将found对象存入
foundList.add(found);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return foundList;
}
//实现获取数据,返回详情
public List<Found> findDetailsFound() {
//创建一个集合,存放信息
List<Found> foundList = new ArrayList<Found>();
//创建连接对象
Connection connection = null;
//创建语句对象
PreparedStatement ps = null;
//创建结果集对象
ResultSet rs = null;
//sql语句
String sqlString = "select foundname,foundtelephone,founddetails,foundphoto from found";
//连接对象初始化
connection = DBUtil.getConnection();
try {
//语句对象ps初始化
ps = connection.prepareStatement(sqlString);
//执行sql语句
rs = ps.executeQuery();
//获取结果集对象中的信息
while (rs.next()) {
Found found = new Found();
found.setFoundname(rs.getString(1));
found.setFoundtelephone(rs.getString(2));
found.setFounddetails(rs.getString(3));
found.setFoundphoto(rs.getString(4));
//将found对象存入
foundList.add(found);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return foundList;
}
//按条件查询详情数据
public List<Found> findGetDetailsFound(String fieldname,String fieldvalue) {
//创建一个集合,存放信息
List<Found> foundList=new ArrayList<Found>();
//创建连接对象
Connection connection=null;
//创建语句对象
PreparedStatement ps=null;
//创建结果集对象
ResultSet rs=null;
//sql语句
String sqlString="select foundname,foundtelephone,founddetails,foundphoto from found where "+fieldname+"=?";
//连接对象初始化
connection=DBUtil.getConnection();
try {
//语句对象ps初始化
ps=connection.prepareStatement(sqlString);
//给占位符赋值
ps.setString(1,fieldvalue);
//执行sql语句
rs=ps.executeQuery();
//获取结果集对象中的信息
while (rs.next()){
Found found = new Found();
found.setFoundname(rs.getString(1));
found.setFoundtelephone(rs.getString(2));
found.setFounddetails(rs.getString(3));
found.setFoundphoto(rs.getString(4));
//将found对象存入
foundList.add(found);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return foundList;
}
}
LostDao.java
package dao;
import entity.Lost;
import util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class LostDao {
//创建一个添加的方法
public int addLost(Lost lost) {
int flag=0;//标识位,用来判断是否添加成功
//1.创建一个连接对象
Connection conn=null;
//2.创建语句对象,执行sql语句
PreparedStatement ps=null;
//3.构造要执行的sql语句
String sqlString="insert into lost(lostname,lostnumber,lostplace,lostdate,losttelephone,lostphoto) value (?,?,?,?,?,?)";
try {
//4.连接对象初始化
conn= DBUtil.getConnection();
//5.语句对象初始化
ps=conn.prepareStatement(sqlString);
//6.占位符赋值
//ps.setInt(1, book.getId());
ps.setString(1, lost.getLostname());
ps.setString(2, lost.getLostnumber());
ps.setString(3, lost.getLostplace());
ps.setString(4, lost.getLostdate());
ps.setString(5, lost.getLosttelephone());
ps.setString(6, lost.getLostphoto());
//7.执行sql语句
flag=ps.executeUpdate();//insert update delete需要调用此方法
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
//创建一个添加的方法
public int addDetaiilsLost(Lost lost) {
int flag=0;//标识位,用来判断是否添加成功
//1.创建一个连接对象
Connection conn=null;
//2.创建语句对象,执行sql语句
PreparedStatement ps=null;
//3.构造要执行的sql语句
String sqlString="insert into lost(lostname,lostnumber,lostplace,lostdate,losttelephone,lostphoto,lostdetails) value (?,?,?,?,?,?,?)";
try {
//4.连接对象初始化
conn= DBUtil.getConnection();
//5.语句对象初始化
ps=conn.prepareStatement(sqlString);
//6.占位符赋值
//ps.setInt(1, book.getId());
ps.setString(1, lost.getLostname());
ps.setString(2, lost.getLostnumber());
ps.setString(3, lost.getLostplace());
ps.setString(4, lost.getLostdate());
ps.setString(5, lost.getLosttelephone());
ps.setString(6, lost.getLostphoto());
ps.setString(7, lost.getLostdetails());
//7.执行sql语句
flag=ps.executeUpdate();//insert update delete需要调用此方法
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
//实现获取数据,返回集合
public List<Lost> findAllLost() {
//创建一个集合,存放信息
List<Lost> lostList=new ArrayList<Lost>();
//创建连接对象
Connection connection=null;
//创建语句对象
PreparedStatement ps=null;
//创建结果集对象
ResultSet rs=null;
//sql语句
String sqlString="select lostname,lostnumber,lostplace,lostdate,losttelephone,lostphoto from lost";
//连接对象初始化
connection= DBUtil.getConnection();
try {
//语句对象ps初始化
ps=connection.prepareStatement(sqlString);
//执行sql语句
rs=ps.executeQuery();
//获取结果集对象中的信息
while (rs.next()){
Lost lost=new Lost();
lost.setLostname(rs.getString(1));
lost.setLostnumber(rs.getString(2));
lost.setLostplace(rs.getString(3));
lost.setLostdate(rs.getString(4));
lost.setLosttelephone(rs.getString(5));
lost.setLostphoto(rs.getString(6));
//将lost对象存入
lostList.add(lost);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return lostList;
}
//删除数据
public int deleteLost(String lostname){
int flag=0;//标识位,用来判断是否添加成功
//1.创建一个连接对象
Connection conn=null;
//2.创建语句对象,执行sql语句
PreparedStatement ps=null;
//3.构造要执行的sql语句
String sqlString="delete from lost where lostname=?";
try {
//4.连接对象初始化
conn= DBUtil.getConnection();
//5.语句对象初始化
ps=conn.prepareStatement(sqlString);
//6.占位符赋值
ps.setString(1,lostname);
//7.执行sql语句
flag=ps.executeUpdate();//insert update delete需要调用此方法
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
//按条件查询数据
public List<Lost> findLost(String fieldname,String fieldvalue) {
//创建一个集合,存放信息
List<Lost> lostList=new ArrayList<Lost>();
//创建连接对象
Connection connection=null;
//创建语句对象
PreparedStatement ps=null;
//创建结果集对象
ResultSet rs=null;
//sql语句
String sqlString="select lostname,lostnumber,lostplace,lostdate,losttelephone,lostphoto from lost where "+fieldname+"=?";
//连接对象初始化
connection=DBUtil.getConnection();
try {
//语句对象ps初始化
ps=connection.prepareStatement(sqlString);
//给占位符赋值
ps.setString(1,fieldvalue);
//执行sql语句
rs=ps.executeQuery();
//获取结果集对象中的信息
while (rs.next()){
Lost lost=new Lost();
lost.setLostname(rs.getString(1));
lost.setLostnumber(rs.getString(2));
lost.setLostplace(rs.getString(3));
lost.setLostdate(rs.getString(4));
lost.setLosttelephone(rs.getString(5));
lost.setLostphoto(rs.getString(6));
//将lost对象存入
lostList.add(lost);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return lostList;
}
//实现获取数据,返回详情
public List<Lost> findDetailsLost() {
//创建一个集合,存放信息
List<Lost> lostList=new ArrayList<Lost>();
//创建连接对象
Connection connection=null;
//创建语句对象
PreparedStatement ps=null;
//创建结果集对象
ResultSet rs=null;
//sql语句
String sqlString="select lostname,losttelephone,lostdetails,lostphoto from lost";
//连接对象初始化
connection= DBUtil.getConnection();
try {
//语句对象ps初始化
ps=connection.prepareStatement(sqlString);
//执行sql语句
rs=ps.executeQuery();
//获取结果集对象中的信息
while (rs.next()){
Lost lost=new Lost();
lost.setLostname(rs.getString(1));
lost.setLosttelephone(rs.getString(2));
lost.setLostdetails(rs.getString(3));
lost.setLostphoto(rs.getString(4));
//将lost对象存入
lostList.add(lost);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return lostList;
}
//按条件查询详情数据
public List<Lost> findGetDetailsLost(String fieldname,String fieldvalue) {
//创建一个集合,存放信息
List<Lost> lostList=new ArrayList<Lost>();
//创建连接对象
Connection connection=null;
//创建语句对象
PreparedStatement ps=null;
//创建结果集对象
ResultSet rs=null;
//sql语句
String sqlString="select lostname,losttelephone,lostdetails,lostphoto from lost where "+fieldname+"=?";
//连接对象初始化
connection=DBUtil.getConnection();
try {
//语句对象ps初始化
ps=connection.prepareStatement(sqlString);
//给占位符赋值
ps.setString(1,fieldvalue);
//执行sql语句
rs=ps.executeQuery();
//获取结果集对象中的信息
while (rs.next()){
Lost lost=new Lost();
lost.setLostname(rs.getString(1));
lost.setLosttelephone(rs.getString(2));
lost.setLostdetails(rs.getString(3));
lost.setLostphoto(rs.getString(4));
//将lost对象存入
lostList.add(lost);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return lostList;
}
}
UserDao.java
package dao;
import entity.User;
import util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
//账户密码登录
public List<User> loginUser(String username, String password) {
//创建一个集合,存放信息
List<User> userList = new ArrayList<User>();
//创建连接对象
Connection connection = null;
//创建语句对象
PreparedStatement ps = null;
//创建结果集对象
ResultSet rs = null;
//sql语句
String sqlString = "select * from user where username =? and password=?";//查询的SQL语句
//连接对象初始化
connection = DBUtil.getConnection();
try {
//语句对象ps初始化
ps = connection.prepareStatement(sqlString);
//给占位符赋值
ps.setString(1, username);
ps.setString(2, password);
//执行sql语句
rs = ps.executeQuery();
while (rs.next()){
User user=new User();
userList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
return userList;
}
//实现获取数据,返回集合
public List<User> findAllUser() {
//创建一个集合,存放信息
List<User> userList=new ArrayList<User>();
//创建连接对象
Connection connection=null;
//创建语句对象
PreparedStatement ps=null;
//创建结果集对象
ResultSet rs=null;
//sql语句
String sqlString="select username,password,turename,birthday,address,telephone from user";
//连接对象初始化
connection=DBUtil.getConnection();
try {
//语句对象ps初始化
ps=connection.prepareStatement(sqlString);
//执行sql语句
rs=ps.executeQuery();
//获取结果集对象中的信息
while (rs.next()){
User user=new User();
user.setUsername(rs.getString(1));
user.setPassword(rs.getString(2));
user.setTurename(rs.getString(3));
user.setBirthday(rs.getString(4));
user.setAddress(rs.getString(5));
user.setTelephone(rs.getString(6));
//将user对象存入
userList.add(user);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return userList;
}
//修改数据
public int editUser(User user){
int flag=0;//标识位,用来判断是否添加成功
//1.创建一个连接对象
Connection conn=null;
//2.创建语句对象,执行sql语句
PreparedStatement ps=null;
//3.构造要执行的sql语句
String sqlString="update user set password=?,turename=?,birthday=?,address=?,telephone=? where username=?";
try {
//4.连接对象初始化
conn= DBUtil.getConnection();
//5.语句对象初始化
ps=conn.prepareStatement(sqlString);
//6.占位符赋值
ps.setString(1, user.getPassword());
ps.setString(2, user.getTurename());
ps.setString(3, user.getBirthday());
ps.setString(4, user.getAddress());
ps.setString(5, user.getTelephone());
ps.setString(6, user.getUsername());
//7.执行sql语句
flag=ps.executeUpdate();//insert update delete需要调用此方法
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
//创建一个添加的方法
public int addUser(User user) {
int flag=0;//标识位,用来判断是否添加成功
//1.创建一个连接对象
Connection conn=null;
//2.创建语句对象,执行sql语句
PreparedStatement ps=null;
//3.构造要执行的sql语句
String sqlString="insert into user(username,password,turename,birthday,address,telephone) value (?,?,?,?,?,?)";
try {
//4.连接对象初始化
conn= DBUtil.getConnection();
//5.语句对象初始化
ps=conn.prepareStatement(sqlString);
//6.占位符赋值
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
ps.setString(3, user.getTurename());
ps.setString(4, user.getBirthday());
ps.setString(5, user.getAddress());
ps.setString(6, user.getTelephone());
//7.执行sql语句
flag=ps.executeUpdate();//insert update delete需要调用此方法
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
//删除数据
public int deleteUser(String username){
int flag=0;//标识位,用来判断是否添加成功
//1.创建一个连接对象
Connection conn=null;
//2.创建语句对象,执行sql语句
PreparedStatement ps=null;
//3.构造要执行的sql语句
String sqlString="delete from user where username=?";
try {
//4.连接对象初始化
conn= DBUtil.getConnection();
//5.语句对象初始化
ps=conn.prepareStatement(sqlString);
//6.占位符赋值
ps.setString(1,username);
//7.执行sql语句
flag=ps.executeUpdate();//insert update delete需要调用此方法
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
//按条件查询数据
public List<User> findUser(String fieldname,String fieldvalue) {
//创建一个集合,存放信息
List<User> userList=new ArrayList<User>();
//创建连接对象
Connection connection=null;
//创建语句对象
PreparedStatement ps=null;
//创建结果集对象
ResultSet rs=null;
//sql语句
String sqlString="select username,password,turename,birthday,address,telephone from user where "+fieldname+"=?";
//连接对象初始化
connection=DBUtil.getConnection();
try {
//语句对象ps初始化
ps=connection.prepareStatement(sqlString);
//给占位符赋值
ps.setString(1,fieldvalue);
//执行sql语句
rs=ps.executeQuery();
//获取结果集对象中的信息
while (rs.next()){
User user=new User();
user.setUsername(rs.getString(1));
user.setPassword(rs.getString(2));
user.setTurename(rs.getString(3));
user.setBirthday(rs.getString(4));
user.setAddress(rs.getString(5));
user.setTelephone(rs.getString(6));
//将user对象存入
userList.add(user);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return userList;
}
}
entity
Admin.java
package entity;
public class Admin {
private String username;
private String password;
private String turename;
private String birthday;
private String address;
private String telephone;
public Admin() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTurename() {
return turename;
}
public void setTurename(String turename) {
this.turename = turename;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Admin(String username, String password, String turename, String birthday, String address, String telephone) {
this.username = username;
this.password = password;
this.turename = turename;
this.birthday = birthday;
this.address = address;
this.telephone = telephone;
}
@Override
public String toString() {
return "Admin{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", turename='" + turename + '\'' +
", birthday='" + birthday + '\'' +
", address='" + address + '\'' +
", telephone='" + telephone + '\'' +
'}';
}
}
Found.java
package entity;
public class Found {
private String foundname;
private String foundnumber;
private String foundplace;
private String founddate;
private String foundtelephone;
private String foundphoto;
private String founddetails;
public Found(){
}
public String getFoundname() {
return foundname;
}
public void setFoundname(String foundname) {
this.foundname = foundname;
}
public String getFoundnumber() {
return foundnumber;
}
public void setFoundnumber(String foundnumber) {
this.foundnumber = foundnumber;
}
public String getFoundplace() {
return foundplace;
}
public void setFoundplace(String foundplace) {
this.foundplace = foundplace;
}
public String getFounddate() {
return founddate;
}
public void setFounddate(String founddate) {
this.founddate = founddate;
}
public String getFoundtelephone() {
return foundtelephone;
}
public void setFoundtelephone(String foundtelephone) {
this.foundtelephone = foundtelephone;
}
public String getFoundphoto() {
return foundphoto;
}
public void setFoundphoto(String foundphoto) {
this.foundphoto = foundphoto;
}
public String getFounddetails() {
return founddetails;
}
public void setFounddetails(String founddetails) {
this.founddetails = founddetails;
}
public Found(String foundname, String foundnumber, String foundplace, String founddate, String foundtelephone, String foundphoto, String founddetails) {
this.foundname = foundname;
this.foundnumber = foundnumber;
this.foundplace = foundplace;
this.founddate = founddate;
this.foundtelephone = foundtelephone;
this.foundphoto = foundphoto;
this.founddetails = founddetails;
}
@Override
public String toString() {
return "Found{" +
"foundname='" + foundname + '\'' +
", foundnumber='" + foundnumber + '\'' +
", foundplace='" + foundplace + '\'' +
", founddate='" + founddate + '\'' +
", foundtelephone='" + foundtelephone + '\'' +
", foundphoto='" + foundphoto + '\'' +
", founddetails='" + founddetails + '\'' +
'}';
}
}
Lost.java
package entity;
public class Lost {
private String lostname;
private String lostnumber;
private String lostplace;
private String lostdate;
private String losttelephone;
private String lostphoto;
private String lostdetails;
public Lost() {
}
public String getLostname() {
return lostname;
}
public void setLostname(String lostname) {
this.lostname = lostname;
}
public String getLostnumber() {
return lostnumber;
}
public void setLostnumber(String lostnumber) {
this.lostnumber = lostnumber;
}
public String getLostplace() {
return lostplace;
}
public void setLostplace(String lostplace) {
this.lostplace = lostplace;
}
public String getLostdate() {
return lostdate;
}
public void setLostdate(String lostdate) {
this.lostdate = lostdate;
}
public String getLosttelephone() {
return losttelephone;
}
public void setLosttelephone(String losttelephone) {
this.losttelephone = losttelephone;
}
public String getLostphoto() {
return lostphoto;
}
public void setLostphoto(String lostphoto) {
this.lostphoto = lostphoto;
}
public String getLostdetails() {
return lostdetails;
}
public void setLostdetails(String lostdetails) {
this.lostdetails = lostdetails;
}
public Lost(String lostname, String lostnumber, String lostplace, String lostdate, String losttelephone, String lostphoto, String lostdetails) {
this.lostname = lostname;
this.lostnumber = lostnumber;
this.lostplace = lostplace;
this.lostdate = lostdate;
this.losttelephone = losttelephone;
this.lostphoto = lostphoto;
this.lostdetails = lostdetails;
}
@Override
public String toString() {
return "Lost{" +
"lostname='" + lostname + '\'' +
", lostnumber='" + lostnumber + '\'' +
", lostplace='" + lostplace + '\'' +
", lostdate='" + lostdate + '\'' +
", losttelephone='" + losttelephone + '\'' +
", lostphoto='" + lostphoto + '\'' +
", lostdetails='" + lostdetails + '\'' +
'}';
}
}
User.java
package entity;
public class User {
private String username;
private String password;
private String turename;
private String birthday;
private String address;
private String telephone;
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTurename() {
return turename;
}
public void setTurename(String turename) {
this.turename = turename;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public User(String username, String password, String turename, String birthday, String address, String telephone) {
this.username = username;
this.password = password;
this.turename = turename;
this.birthday = birthday;
this.address = address;
this.telephone = telephone;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", turename='" + turename + '\'' +
", birthday='" + birthday + '\'' +
", address='" + address + '\'' +
", telephone='" + telephone + '\'' +
'}';
}
}
util
DBUtil.java
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
//创建一个连接到数据库的静态方法
public static Connection getConnection(){
Connection conn=null;
try{
//加载MySql的驱动类
Class.forName("com.mysql.jdbc.Driver") ;
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/guanruixian","root","");
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
}
return conn;
}
}
JsonTool.java
package util;
import com.google.gson.Gson;
//工具类,将java对象转换成Json数据格式
public class JsonTool {
public static String javaToJson(Object value) {
//创建json对象
Gson gson=new Gson();
//转换
String jsonString=gson.toJson(value);
return jsonString;
}
}
lib
标签:ps,java,String,App,request,import,Android,servlet,失物招领 来源: https://blog.csdn.net/Guanruixian2000/article/details/118436340