作业………………………….
1: 环境搭建
安装maven : 模版webapp创建项目,并且建立以下项目结构:
2:工具类
package utils; import java.sql.*; public class JdbcUtils { /** * 可以把几个字符串定义成常量:用户名,密码,URL,驱动类 */ private static final String USER = "root"; private static final String PWD = "root"; private static final String URL = "jdbc:mysql://127.0.0.1:3306/day01?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC"; private static final String DRIVER= "com.mysql.jdbc.Driver"; /** * 注册驱动 */ static{ try { Class.forName(DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 得到数据库的连接 * @throws SQLException * @return */ public static Connection getConnection() throws SQLException { return DriverManager.getConnection(URL, USER, PWD); } /** * 关闭所有打开的资源 */ public static void close(Connection conn, Statement stmt) { if(stmt!=null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 关闭所有打开的资源 */ public static void close(Connection conn, Statement stmt, ResultSet rs) { if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } close(conn, stmt); } }
3:依赖
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> </dependencies>
4:编码
4.1 实体
@Data public class User { private Integer uid; private String username; private String password; }
4.2 dao
/** * @program: day01-maven * @description: * @author: Mr.Dai * @create: 2020-06-08 22:50 **/ public interface UserDao { User findUserById(Integer uid) throws SQLException; Integer updateUserById(Integer uid,String username,String password); void deleteUserById(Integer uid); void addUser(); } public class UserDaoImpl implements UserDao { @Override public User findUserById(Integer uid) throws SQLException { Connection connection = JdbcUtils.getConnection(); PreparedStatement statement = connection.prepareStatement("select * from user where uid=?"); statement.setInt(1,uid); ResultSet resultSet = statement.executeQuery(); User user = new User(); while(resultSet.next()){ user.setUid(resultSet.getInt(1)); user.setUsername(resultSet.getString(2)); user.setPassword(resultSet.getString(3)); } return user; } @Override public Integer updateUserById(Integer uid, String username, String password) { return null; } @Override public void deleteUserById(Integer uid) { } @Override public void addUser() { } }
4.3 service
public class UserServiceImpl implements service.UserService { private Logger logger=Logger.getLogger(UserService.class.getName()); private UserDao dao=new UserDaoImpl(); @Override public User findUserById(Integer uid) { User user=null; try { user=dao.findUserById(uid); } catch (SQLException throwables) { logger.log(Level.SEVERE,"查询失败!"); throwables.printStackTrace(); } return user; } }
5:前端
<%@ page import="domain.User" %> <%@ page import="java.util.List" %> <%@ page import="java.util.Arrays" %> <%@ page language="java" contentType="text/html; charset=utf-8" 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>??</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"> </head> <body> <form action="findUByUid" method="post"> <label for="uid">查询用户的id</label> <input id="uid" type="text" name="uid" value=" " > <input type="submit" value="查询"> </form> <table border="1"> <tr> <td>id</td> <td>name</td> <td>creator</td> <td>createTime</td> </tr> <% User user = new User(); for(User tl:(List<User>)pageContext.getAttribute("userlist")!=null?(List<User>)pageContext.getAttribute("userlist"): Arrays.asList(user)) {%> <tr> <td><%=tl.getUid()%></td> <td><%=tl.getUsername() %></td> <td><%=tl.getPassword() %></td> <td><%=(List<User>)pageContext.getAttribute("userlist")%></td> </tr> <%} %> </table> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>findUserServlet</servlet-name> <servlet-class>servlet.FindUserServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>findUserServlet</servlet-name> <url-pattern>/findUByUid</url-pattern> </servlet-mapping> <filter> <filter-name>characterFilter</filter-name> <filter-class>filter.CharacterFilter</filter-class> </filter> <filter-mapping> <filter-name>characterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
package servlet;
import domain.User;
import service.UserService;
import service.impl.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
/**
* @program: day01-maven
* @description:
* @author: Mr.Dai
* @create: 2020-06-08 23:17
**/
public class FindUserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String uid = req.getParameter("uid");
UserService userService = new UserServiceImpl();
User user = userService.findUserById(Integer.parseInt(uid.trim()));
System.out.println(user.getUsername());
req.setAttribute("userlist", Arrays.asList(user));
req.getRequestDispatcher("/index.jsp").forward(req,resp);
}
}