上篇: 实现简单的登录功能(1.0).
获取数据库信息(连接池,DbUtils工具类)
项目图
导包
DBCP:
- commons-dbcp2-2.6.0.jar
- commons-logging-1.2.jar
- commons-pool2-2.6.2.jar
DbUtils:
- commons-dbutils-1.7.jar
jsp页面标准标签库:
- taglibs-standard-impl-1.2.5.jar
- taglibs-standard-spec-1.2.5.jar
数据库包:
- mysql-connector-java-5.1.39-bin.jar
- commons-io-2.6.jar
- commons-codec-1.12.jar
Dao层
package com.example.dao;
import java.util.List;
import com.example.domain.User;
public interface Dao {
List<User> getAllUser();
}
DaoImpl层
package com.example.dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.example.dao.Dao;
import com.example.domain.User;
import com.example.utils.DBCPUtils;
public class DaoImpl implements Dao {
@Override
public List<User> getAllUser() {
QueryRunner qr = new QueryRunner(DBCPUtils.getDataSource());
try {
//damain层getusername()方法名与username保持一致
return qr.query("select uid,username ppp,password from user", new BeanListHandler<>(User.class));
} catch (SQLException e) {
e.printStackTrace();
return new ArrayList<User>();
}
}
}
Domain层
package com.example.domain;
public class User {
private Integer uid;
// private String username;
private String ppp;
public String getPpp() {
return ppp;
}
public void setPpp(String ppp) {
this.ppp = ppp;
}
private String password;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
// public String getUsername() {
// return username;
// }
// public void setUsername(String uname) {
// this.username = uname;
// }
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [uid=" + uid + ", ppp=" + ppp + ", password=" + password + "]";
}
}
Service层
package com.example.service;
import java.util.List;
import com.example.domain.User;
public interface Service {
List<User> getAllUser();
}
ServiceImpl层
package com.example.service.impl;
import java.util.List;
import com.example.dao.Dao;
import com.example.dao.impl.DaoImpl;
import com.example.domain.User;
import com.example.service.Service;
public class ServiceImpl implements Service {
private Dao dao = new DaoImpl();
@Override
public List<User> getAllUser() {
return dao.getAllUser();
}
}
Utils层
package com.example.utils;
import java.io.IOException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
public class DBCPUtils {
private static BasicDataSource dataSource = null;
static {
Properties props = new Properties();
try {
// props.load(DBCPUtils.class.getResourceAsStream("dbcp.properties"));
props.load(DBCPUtils.class.getClassLoader().getResourceAsStream("dbcp.properties"));
} catch (IOException e) {
e.printStackTrace();
}
try {
dataSource = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDataSource() {
return dataSource;
}
}
Web层
package com.example.web;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.domain.User;
import com.example.service.Service;
import com.example.service.impl.ServiceImpl;
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Service service = new ServiceImpl();
public UserServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<User> list = service.getAllUser();
request.setAttribute("list", list);
request.getRequestDispatcher("/WEB-INF/User.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
dbcp.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///test
username=root
password=123456
User.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1></h1><ol>
<c:forEach items="${list}" var="p">
<li>${p.uid }</li>
<li>${p.ppp }</li>
<li>${p.password }</li>
</c:forEach>
</ol>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>web02</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>UserServlet</display-name>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.example.web.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/UserServlet</url-pattern>
</servlet-mapping>
</web-app>