Demo描述:实现C3P0数据库池连接数据库。部署Tomcat服务器,使用servlet完成简单登录测试。
错误描述:测试时,登录账号名和密码与数据库表中信息一致。但是提示登录失败,控制台无错误提示。
产生原因:细节问题。忽略了setCharacterEncoding设置字符的编码方式.
注意点:1.setCharacterEncoding只对post请求有效。
2.setCharacterEncoding必须写在第一个request.getParameter()方法之前
解决方案:在servlet中doPost()方法中发往服务器的参数的编码格式。
Demo结构如下:
1.创建JDBC连接池数据库工具类。
package com.sou1yu.servlet.utils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @Author: sou1yu
* @Email:sou1yu@aliyun.com
* @Date: 2020/6/29
*/
public class JDBCUtils {
private static DataSource dataSource = new ComboPooledDataSource();
public static DataSource getDataSource(){
return dataSource;
}
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//无结果集释放资源
public static void repleaseAll(Statement stm , Connection conn){
try {
if (stm!=null){
stm.close();
stm = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn!=null){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//有结果集释放资源
public static void repleaseAll(ResultSet rs, Statement stm , Connection conn){
try {
if (rs!=null){
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (stm!=null){
stm.close();
stm = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn!=null){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.LoginServlet代码。
package com.sou1yu.servlet.web;
import com.sou1yu.servlet.service.UserService;
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;
/**
* @Author: sou1yu
* @Email:sou1yu@aliyun.com
* @Date: 2020/6/29
*/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
public void init() throws ServletException {
//再次获取servletContext对象 并在该对象中存储conunt 初始化为0 该值用来记录用户登录的次数
getServletContext().setAttribute("count",0);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8"); //设置请求时字符集 否则输入的信息为乱码 在sql中搜寻不到用户信息
resp.setContentType("text/html;charset=UTF-8");//是设置页面中为中文编码
//1.获取表单信息
String username = req.getParameter("username");
String password = req.getParameter("password");
//2.调用service层
UserService userService = new UserService();
boolean flag = userService.login(username,password);
//3.将servlet层结果返回到页面
if (flag){
//1.从servletcontext中获取count值
int count = (int) getServletContext().getAttribute("count");
//2.将context值++
count++;
//3.将修改后的值 写回到servletContext
getServletContext().setAttribute("count",count);
//4.向页面显示信息
resp.getWriter().write("你是第"+count+"位登录的用户");
} else
resp.getWriter().write("Login failed..");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
3.service层逻辑。
package com.sou1yu.servlet.service;
import com.sou1yu.servlet.dao.UserDao;
/**
* @Author: sou1yu
* @Email:sou1yu@aliyun.com
* @Date: 2020/6/29
*/
public class UserService {
public boolean login(String username, String password) {
try {
//1.调用业务逻辑判断
if (username==null|| password==null)
return false;
//2.调用dao层
UserDao userDao = new UserDao();
int count = userDao.login(username,password);
return count>0;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
4.dao层逻辑。
package com.sou1yu.servlet.dao;
import com.sou1yu.servlet.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @Author: sou1yu
* @Email:sou1yu@aliyun.com
* @Date: 2020/6/29
*/
public class UserDao {
public int login(String username, String password) throws SQLException {
//1.获取连接
Connection conn = JDBCUtils.getConnection();
//2.执行sql
PreparedStatement pstm = conn.prepareStatement("select * from user where username=? and password=?");
pstm.setObject(1,username);
pstm.setObject(2,password);
ResultSet rs = pstm.executeQuery();
boolean flag = rs.next();//使用标记记录是否查询到有数据
JDBCUtils.repleaseAll(rs,pstm,conn); //直接释放资源
return flag? 1:-1; //返回由flag决定即可
}
}
5.low爆的html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="login" method="post">
用户名:<input type="text" name="username" /> <br>
密码:<input type="password" name="password" /> <br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
6.C3P0配置文件c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/myweb</property>
<property name="user">root</property>
<property name="password">123456</property>
</default-config>
</c3p0-config>