除改以外
servlet连接数据库后代码
继day14基础上加上
FindServlet用于列表显示
IUserService userService=new UserServiceImpl();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List users=userService.findAll();
HttpSession session=request.getSession();
session.setAttribute(“userlist”,users);
request.getRequestDispatcher(“allUser.jsp”).forward(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<User> users=userService.findAll();
HttpSession session=request.getSession();
session.setAttribute("userlist",users);
request.getRequestDispatcher("allUser.jsp").forward(request,response);
}
数据库连接
IUserService
Boolean login(String username,String password);
List findAll();
void delete(int id);
void add(String username,String password);
User findUserById(int id);
void update(int id,String username,String password);
UserServicelmpl
IUserDao userDao=new UserDaoImpl();
@Override
public Boolean login(String username, String password) {
User user=userDao.findUserByUsername(username);
if (user!=null&&user.getPassword().equals(password)){
return true;
}else {
return false;
}
}
@Override
public List<User> findAll(){
List<User> users=userDao.findAll();
return users;
}
@Override
public void delete(int id) {
userDao.delete(id);
}
@Override
public void add( String username, String password) {
userDao.add(username,password);
}
@Override
public User findUserById(int id) {
return userDao.findUserById(id);
}
@Override
public void update(int id, String username, String password) {
userDao.update(id,username,password);
}
User
private int id;
private String username;
private String password;
private int type;
Generate…->getting and setting
Generate…->toString()
IUserDao
User findUserByUsername(String username);
List findAll();
void delete(int id);
void add(String username,String password);
User findUserById(int id);
void update(int id,String username,String password);
UserDaoImpl
package com.zhongruan.dao.impl;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.model.User;
import com.zhongruan.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 UserDaoImpl implements IUserDao {
@Override
public User findUserByUsername(String username) {
User user=null;
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
try{
connection= DBUtil.getConnection();
String sql=“select * from tb_user where username=?”;
statement=connection.prepareStatement(sql);
statement.setString(1,username);
resultSet=statement.executeQuery();
while (resultSet.next()){
user =new User();
user.setId(resultSet.getInt(1));
user.setUsername(username);
user.setPassword(resultSet.getString(3));
}
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,statement,connection);
}
return user;
}
@Override
public List<User> findAll(){
List<User> users=new ArrayList<>();
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
try {
connection=DBUtil.getConnection();
String sql="select * from tb_user";
statement=connection.prepareStatement(sql);
resultSet=statement.executeQuery();
while (resultSet.next()){
User user=new User();
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
users.add(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(resultSet,statement,connection);
}
return users;
}
@Override
public void delete(int id) {
Connection connection=null;
PreparedStatement statement=null;
try {
connection=DBUtil.getConnection();
statement=connection.prepareStatement("delete from tb_user where id=?");
statement.setInt(1,id);
statement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.closeAll(null,statement,connection);
}
}
@Override
public void add(String username, String password) {
Connection connection=null;
PreparedStatement statement=null;
try {
connection=DBUtil.getConnection();
statement=connection.prepareStatement("insert into tb_user(username,password) values (?,?)");
statement.setString(1,username);
statement.setString(2,password);
statement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.closeAll(null,statement,connection);
}
}
@Override
public User findUserById(int id) {
User user = null;
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = DBUtil.getConnection();
//3写sql
String sql = "select * from tb_user where id=?";
statement = connection.prepareStatement(sql);
statement.setInt(1, id);
resultSet = statement.executeQuery();
while (resultSet.next()) {
user = new User();
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(resultSet, statement, connection);
}
return user;
}
@Override
public void update(int id, String username, String password) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection=DBUtil.getConnection();
String sql="update tb_user set username=?,password=? where id=?";
statement=connection.prepareStatement(sql);
statement.setInt(3,id);
statement.setString(1,username);
statement.setString(2,password);
statement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
finally {
DBUtil.closeAll(null,statement,connection);
}
}
}
DeleteServlet
String id1=request.getParameter(“id”);
int id2=Integer.parseInt(id1);
IUserService userService=new UserServiceImpl();
userService.delete(id2);
request.getRequestDispatcher("/find").forward(request,response);
AddServlet
String username=request.getParameter(“username”);
String password=request.getParameter(“password”);
IUserService userService=new UserServiceImpl();
userService.add(username,password);
request.getRequestDispatcher("/find").forward(request,response);
allUser.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8” isELIgnored=“false”%>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
基于servlet+jsp框架的管理系统:简单实现增、删、改、查。
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>用户列表 —— 显示所有用户</small>
</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" href="addUser.jsp">新增</a>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>id</th>
<th>用户名</th>
<th>密码</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${userlist}" var="user" >
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td>
<a href="/delete?id=${user.id}">删除</a>|
<a href="/toUpdate?id=${user.id}">修改</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>