前言
1. 连接池概述
2. c3p0概述
登陆步骤
一、准备工作
1. 设置自动编译
2. 放入架包
3. 测试连接池
二、连接数据库
1. 创建数据库连接池 c3p0-config.xml
2. 创建工具类 C3P0Utils
三、登陆页面
表现层
1. MVC中的 controller 控制器
MVC中的 model 模型
MVC中的 view 视图
业务逻辑层 :service
接口
对应的实现类 impl
控制层 → 业务逻辑层
ServletLogin 调用 UsersService
数据访问层:
创建接口
对应的实现类
业务逻辑层 → 数据访问层
【常见问题】
【总代码】
controller 控制层
ServletLogin
entity 模型,实体
Users
web 视图
login.jsp
service 业务逻辑层
UsersService 接口
UsersServiceImpl 实现类
dao 数据访问层
UsersDao 接口
UsersDaoImpl 实现类
utils 连接池
C3P0Utils
c3p0-config.xml
Java为数据库连接池提供了公共的接口: javax.sql.DataSource,各个厂商需要让自己的连接池实现这个接口。这样应用程序可以方便的切换不同厂商的连接池!
常见的连接池:DBCP、C3PO、Druid(阿里。功能强大)。
使用第三方工具需要导入jar包,c3p0使用时还需要添加配置文件 c3pO-config xml.
这个只是测试,真正的项目不
c3p0-config.xml文件一定要放在src里!!!
|
三层架构和MVC
MVC:M:model模型 实体类 V:view视图 jsp页面 C:controller控制器 servlet
三层架构:表现层、业务逻辑层、数据访问层
表现层:用户肉眼可以看的到的页面 MVC
业务逻辑层:逻辑运算
数据访问层:连接数据库的
登录:用户输入用户名和密码,验证用户名和密码在数据库中是否存在,存在提示用户登陆成功,不存在提示用户登录失败
模型(实体):用户 users
视图:登录页
控制器:controller包 --> loginServlet.java
业务逻辑层:service--->
数据访问层:dao--->
页面---》控制器---》业务逻辑层---》数据访问层---》连接数据库 ---》数据访问层---》业务逻辑层---》页面
loginservlet
package com.cl.controller; 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; @WebServlet("/login") public class ServletLogin extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } |
重启,
在新页面的网址后面加login,回车跳转页面,页面跳转成功后是空白的
http://localhost:8080/test4_war_exploded/login
打开数据库,成功导入数据,说明c3p0连接数据库成功
Users 实体 entity
login.jsp 页面
UsersService
UsersServiceImpl
实例化一个对象
防止中文乱码:
response.setCharacterEncoding("utf8");
response.setCharacterEncoding("gbk"); //utf8 不行就换 gbk
UsersDao
UsersDaoImpl
注意这里注册的账号用户名不可重名,看一下数据有没有重名,有的话手动更改,如果不改会报错
UsersServiceImpl 访问 UsersDaoImpl
重启,
http://localhost:8080/test4_war_exploded/login.jsp
ps:开发类项目不要放在中文目录下,会报错
问题:地址不存在
解决方法:改成("/login")
com.cl.controller.ServletLogin |
package com.cl.controller; import com.cl.entity.Users; import com.cl.service.UsersService; import com.cl.service.impl.UsersServiceImpl; import com.cl.utils.C3P0Utils; import org.apache.commons.dbutils.QueryRunner; 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.sql.SQLException; @WebServlet("/login") public class ServletLogin extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("gbk"); //防止中文乱码 //获得用户名和密码 String username = request.getParameter("username"); String pwd = request.getParameter("pwd"); Users users = new Users(); users.setUsername(username); users.setPwd(pwd); //页面---》控制器---》业务逻辑层---》数据访问层---》连接数据库 ---》数据访问层---》业务逻辑层---》页面 //控制层调用业务逻辑层--实例化一个对象 UsersService usersService = new UsersServiceImpl(); boolean flag = usersService.login(users); if(flag){ response.getWriter().println("登录成功"); }else{ response.getWriter().println("登陆失败"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //核心类 QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource()); //sql //int count = queryRunner.update("insert into users(username,pwd) values('admin','111') --然后抛出异常 String sql = "insert into users(username,pwd) values('admin','111')"; int count = 0; try { count = queryRunner.update(sql); } catch (SQLException e) { e.printStackTrace(); } System.out.println(count); } } |
com.cl.entity.Users |
package com.cl.entity; public class Users { private Integer id; private String username; private String pwd; private String sex; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } |
web.login.jsp |
<%-- Created by IntelliJ IDEA. User: 19935 Date: 2022/3/21 Time: 18:25 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
com.cl.service.UsersService |
package com.cl.service; import com.cl.entity.Users; public interface UsersService { public boolean login(Users users); } |
com.cl.service.UsersServiceImpl |
package com.cl.service.impl; import com.cl.dao.UsersDao; import com.cl.dao.impl.UsersDaoImpl; import com.cl.entity.Users; import com.cl.service.UsersService; public class UsersServiceImpl implements UsersService { @Override public boolean login(Users users) { UsersDao usersDao = new UsersDaoImpl();//访问数据层 Users u = usersDao.login(users);//返回结果 if(u==null) { return false; }else return true; } } |
com.cl.dao.UsersDao |
package com.cl.dao; import com.cl.entity.Users; public interface UsersDao { public Users login(Users users); } |
com.cl.dao.UsersDaoImpl |
package com.cl.dao.impl; import com.cl.dao.UsersDao; import com.cl.entity.Users; import com.cl.utils.C3P0Utils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import java.sql.SQLException; public class UsersDaoImpl implements UsersDao { @Override public Users login(Users users) { //连接数据库 QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from users where username=? and pwd=?"; Object[] param = {users.getUsername(),users.getPwd()}; //注册的账号用户名不可重名--注意数据库里的不要重名否则会报错 Users u = null; try { u = queryRunner.query(sql,new BeanHandler<>(Users.class),param); } catch (SQLException e) { e.printStackTrace(); } return u; } } |
com.cl.utils.C3P0Utils |
package com.cl.utils; import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; public class C3P0Utils { //获得连接池 private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); //连接数据源 public static DataSource getDataSource(){ return dataSource; } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } } |
src. c3p0-config.xml |
|