项目结构目录:
项目环境:
IDEA
JDK 1.8
Tomcat 9
Mysql5.7
MysqlWorkBench 6.3
数据库相关配置如下:
1 create tablet_user2 (3 uid int(10) not nullauto_increment,4 uname varchar(100) not null,5 pwd varchar(100) not null,6 primary key(uid)7 );
源代码:
负责与数据库交互DAO层
packageweb.dao;importweb.entity.Admin;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.util.ArrayList;importjava.util.List;/***adminDao.java
* 直接和数据库交互*/
public classadminDao {public ListgetAllAdmin(){
List list = new ArrayList<>();
Connection conn=Dbhelper.getConnection();
String sql="select * from t_user";try{
PreparedStatement pst=conn.prepareStatement(sql);
ResultSet rs=pst.executeQuery();while(rs.next())
{
Admin admin= newAdmin();
admin.setUid(rs.getInt("uid"));
admin.setUname(rs.getString("uname"));
admin.setPwd(rs.getString("pwd"));
list.add(admin);
}
rs.close();
pst.close();
}catch(Exception e)
{
e.printStackTrace();
}returnlist;
}public booleanaddAdmin(Admin admin)
{
String sql= "insert into t_user(uid,uname,pwd) values(default ,?,?)";
Connection connection=Dbhelper.getConnection();try{
PreparedStatement pst=connection.prepareStatement(sql);
pst.setString(1,admin.getUname());
pst.setString(2,admin.getPwd());int count =pst.executeUpdate();
pst.close();return count>0?true:false;
}catch(Exception e){
e.printStackTrace();
}return false;
}public booleanupdateAdmin(Admin admin)
{
String sql= "update t_user set uname=?,pwd=? where id=?";
Connection connection=Dbhelper.getConnection();try{
PreparedStatement pst=connection.prepareStatement(sql);
pst.setString(1,admin.getUname());
pst.setString(2,admin.getPwd());
pst.setInt(3,admin.getUid());int count =pst.executeUpdate();
pst.close();return count>0?true:false;
}catch(Exception e)
{
e.printStackTrace();
}return false;
}public boolean deleteAdmin(intuid)
{
String sql= "delete from t_user where uid = ?";
Connection connection=Dbhelper.getConnection();try{
PreparedStatement pst=connection.prepareStatement(sql);
pst.setInt(1,uid);int count = 0;
count=pst.executeUpdate();
pst.close();return count>0?true:false;
}catch(Exception e)
{
e.printStackTrace();
}return false;
}public Admin selectAdminByUid(intuid)
{
Connection connection=Dbhelper.getConnection();
String sql= "select * from t_user where uid = "+uid;
Admin admin=null;try{
PreparedStatement pst=connection.prepareStatement(sql);
ResultSet resultSet=pst.executeQuery();while(resultSet.next())
{
admin= newAdmin();
admin.setUname(resultSet.getString("uname"));
admin.setUid(resultSet.getInt("uid"));
admin.setPwd(resultSet.getString("pwd"));
}
resultSet.close();
pst.close();
}catch(Exception e)
{
e.printStackTrace();
}returnadmin;
}
}
packageweb.dao;importjava.sql.Connection;importjava.sql.DriverManager;/***Dbhelper.java
* 连接数据库*/
public classDbhelper {private static String URL = "jdbc:mysql://localhost:3306/roombase"; //数据库地址
private static String userName = "root";private static String passWord = "zxc2505004";private static String DRIVER = "com.mysql.jdbc.Driver";private static Connection conn = null;publicDbhelper() {
}/*** 获取连接
*@return
*/
public staticConnection getConnection()
{if(null ==conn){try{
Class.forName(DRIVER);
conn=DriverManager.getConnection(URL,userName,passWord);
}catch(Exception e)
{
e.printStackTrace();
}
}returnconn;
}/***测试数据库是否联通
*@paramargs*/
public voidmain(String []args)
{
System.err.println(getConnection());
}
}
ENTITY层:
packageweb.entity;importjava.io.Serializable;public class Admin implementsSerializable {private static final long serialVersinID = 1L;private intuid;privateString uname;privateString pwd;public static longgetSerialVersinID() {returnserialVersinID;
}public intgetUid() {returnuid;
}public void setUid(intuid) {this.uid =uid;
}publicString getUname() {returnuname;
}public voidsetUname(String uname) {this.uname =uname;
}publicString getPwd() {returnpwd;
}public voidsetPwd(String pwd) {this.pwd =pwd;
}
@OverridepublicString toString() {return "Admin{" +
"uid=" + uid +
", uname='" + uname + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
Controller层:
packageweb.web.servlet;importweb.dao.adminDao;importweb.entity.Admin;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;/*** 请求控制层
*AddServlet.java*/
public class AddServlet extendsHttpServlet {private static final long serialVersionUID = 1L;
@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {this.doPost(req,resp);
}
@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {
String uname= req.getParameter("uname");
String pwd= req.getParameter("pwd");
Admin admin= newAdmin();
admin.setUname(new String(uname.getBytes("ISO-8859-1"),"UTF-8"));
admin.setPwd(new String(pwd.getBytes("ISO-8859-1"),"UTF-8"));
adminDao dao= newadminDao();
dao.addAdmin(admin);
req.getRequestDispatcher("ShowServlet").forward(req,resp);//内部重定向
}
}
packageweb.web.servlet;importweb.dao.adminDao;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;/*** deleteServlet.java*/
public class deleteServlet extendsHttpServlet {
@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {this.doPost(req,resp);
}
@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {
String idStr= req.getParameter("uid");if(idStr!=null&&!idStr.equals(""))
{int id =Integer.valueOf(idStr);
adminDao dao= newadminDao();
dao.deleteAdmin(id);
}
req.getRequestDispatcher("ShowServlet").forward(req,resp);
}
}
packageweb.web.servlet;importweb.dao.adminDao;importweb.entity.Admin;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;importjava.util.List;/*** ShowServlet.java*/
public class ShowServlet extendsHttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {this.doPost(req, resp);
}protected void doPost(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {
adminDao dao= newadminDao();
List list =dao.getAllAdmin();
req.setAttribute("list", list);
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
packageweb.web.servlet;importweb.dao.adminDao;importweb.entity.Admin;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;/*** UpdateServlet.JAVA*/
public class UpdateServlet extendsHttpServlet {
@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {
String idStr= req.getParameter("uid");if (idStr != null && !idStr.equals("")) {int id =Integer.valueOf(idStr);
adminDao dao= newadminDao();
Admin admin=dao.selectAdminByUid(id);
req.setAttribute("admin", admin);
}
req.getRequestDispatcher("update.jsp").forward(req, resp);
}
@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {
String uname= req.getParameter("uname");
String pwd= req.getParameter("pwd");
String idStr= req.getParameter("uid");
Admin admin= newAdmin();
admin.setUid(Integer.valueOf(idStr));
admin.setUname(new String(uname.getBytes("ISO-8859-1"),"UTF-8"));
admin.setPwd(new String(pwd.getBytes("ISO-8859-1"),"UTF-8"));
adminDao dao= newadminDao();
dao.updateAdmin(admin);
req.getRequestDispatcher("ShowServlet").forward(req, resp);
}
}
JSP页面:
add.jsp
User: ZHENGZHIQIANG
Date:2019/3/2Time:16:14To changethis template use File | Settings |File Templates.--%>
ADDAdd Administrator | |
Add Account number | |
Administrator Password | |
index.jsp
User: ZHENGZHIQIANG
Date:2019/3/2Time:9:59To changethis template use File | Settings |File Templates.--%>
Showborder: 1px solid pink;
margin:0auto;
td{
width: 150px;
border: 1px solid pink;
text-align: center;
}
NUMBER | ACCOUNT NUMBER | PASSWORD | OPERATER |
${item.uid} | ${item.uname} | ${item.pwd} | DELETE-----EDIT |
ADD administrator |
update.jsp
User: ZHENGZHIQIANG
Date:2019/3/2Time:16:34To changethis template use File | Settings |File Templates.--%>
EditEdit administrator info | |
uid | |
uname | |
pwd | |
XML配置文件:
WebBasic
index.html
index.jsp
AddServlet
web.web.servlet.AddServlet
AddServlet
/AddServlet
DeleteServlet
web.web.servlet.deleteServlet
DeleteServlet
/DeleteServlet
UpdateServlet
web.web.servlet.UpdateServlet
UpdateServlet
/UpdateServlet
ShowServlet
web.web.servlet.ShowServlet
ShowServlet
/ShowServlet
源代码如下: