问题遇到的现象和发生背景
通过jar包进行数据库连接池,项目是对html页面进行登录操作,将登录数据通过数据库进行匹配配对
问题相关代码,请勿粘贴截图
-------------------------------------------------------------------Servert包
@WebServlet(value = "/showallcontroller")
public class ShowallAdminController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// //通过httpSession完成权限控制
// HttpSession session = req.getSession();
// Manager mgr = (Manager)session.getAttribute("mgr");
// if(mgr!=null){
//只负责调用业务逻辑
AdminService adminService = new AdminServiceImpl();
List<Admin> adminList = adminService.showAllAdmin();
//request作用域存储数据
req.setAttribute("admins",adminList);
//转发 跳转到显示结果
req.getRequestDispatcher("/showalljsp").forward(req,resp);
// }else{
// resp.sendRedirect("/WebProject_war_exploded/loginMgr.html");
// }
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
-------------------------------------------------------------------servert包
@WebServlet(name = "LoginMgrController",value = "/loginMgr")
public class LoginMgrController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.处理乱码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
//2.收参
String username = request.getParameter("username");
String password = request.getParameter("password");
String inputVcode = request.getParameter("inputVcode");
String codes = (String)request.getSession().getAttribute("codes");
if(!inputVcode.isEmpty() && inputVcode.equalsIgnoreCase(codes)){
//3.调用业务方法
ManagerService managerService = new ManagerServiceImpl();
Manager mgr = managerService.login(username,password);
//4.处理结果,流程跳转
if(mgr!=null){
//登陆成功
//将管理员信息存储在Session里
HttpSession session = request.getSession();
session.setAttribute("mgr",mgr);
//跳转 目标 方式
response.sendRedirect("/WebProject_war_exploded/showallcontroller");
}else {
//登陆失败
response.sendRedirect("/WebProject_war_exploded/loginMgr.html");
}
}else{
response.sendRedirect("/WebProject_war_exploded/loginMgr.html");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
-------------------------------------------------------------------service包下接口
public interface ManagerService {
public Manager login (String username,String password);
}
-------------------------------------------------------------------dao包接口
package com.leighy.servletProject.dao;
import com.leighy.servletProject.entity.Manager;
public interface ManagerDao {
public Manager select(String username);
}
-------------------------------------------------------------------dao包接口实现类
public class ManagerDaoImpl implements ManagerDao {
private QueryRunner queryRunner = new QueryRunner();
@Override
public Manager select(String username) {
try {
Manager manager = queryRunner.query(DBUtils.getConnection(),"select * from manager where username=?",new BeanHandler<Manager>(Manager.class),username);
return manager;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理员登录</title>
</head>
<body>
<form action="/WebProject_war_exploded/loginMgr" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password/"><br/>
验证码:<input type="text" name="inputVcode" /><img src="/WebProject_war_exploded/createCode"/><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
运行结果及报错内容
java.sql.SQLException: Can't call commit when autocommit=true
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:869)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:865)
at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1547)
at com.alibaba.druid.pool.DruidPooledConnection.commit(DruidPooledConnection.java:755)
at com.leighy.servletProject.utils.DBUtils.commit(DBUtils.java:53)
at com.leighy.servletProject.service.impl.ManagerServiceImpl.login(ManagerServiceImpl.java:24)
at com.leighy.servletProject.servlet.controller.LoginMgrController.doPost(LoginMgrController.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:681)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:895)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1722)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
我的解答思路和尝试过的方法
根据项目报错提示,对mysql进行自动提交关闭,无法成功
我想要达到的结果
项目代码正常运行