Servlet+JSP项目JDBC连接MySQL报CommunicationsException求助
Servlet+JSP注册项目JDBC连接MySQL抛出CommunicationsException异常求助
使用IntelliJ IDEA搭配Tomcat 10开发Servlet+JSP注册项目,获取用户输入的姓名、密码、邮箱后,尝试通过JDBC的mysql-connector将数据存入MySQL数据库时,抛出com.mysql.cj.jdbc.exceptions.CommunicationsException异常,提示通信链路失败,驱动未收到服务器数据包。注释掉JDBC相关代码后,可正常输出用户输入的信息,但一执行数据库存储操作就报错,求解决。
项目代码
Servlet代码
@WebServlet(name = "ServletOne", value = "/ServletOne") public class ServletOne extends HttpServlet { private Connection con; @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { //get all the data incoming from the request(user).. String name = request.getParameter("user_name"); String password = request.getParameter("user_password"); String email = request.getParameter("user_email"); if (con == null) { try { //load the driver Class.forName("com.mysql.cj.jdbc.Driver"); //create a connection to jdbc Connection con = DriverManager.getConnection("jdbc:mysql://localhost3306/register", "root", "root"); //query String q = "insert into user(name, password, email) values(?,?,?)"; PreparedStatement preparedStatement = con.prepareStatement(q); //set values preparedStatement.setString(1, name); preparedStatement.setString(2, password); preparedStatement.setString(3, email); //fire query preparedStatement.executeUpdate(); out.println("<br><h1>ur information have been saved into MySQL Database :)</h1>"); } catch (Exception e) { e.printStackTrace(); out.println("<h3>An Error has occurred while connecting to MySQL Database.</h3>"); } } else out.println("Database connection terminated"); con.close(); } catch (SQLException e) { e.printStackTrace(); } } }
signup.jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>signup</title> <!-- importing the css materialize library --> <!-- Compiled and minified CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"> <!-- Compiled and minified JavaScript --> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script> </head> <body style="background: url(Images/GenshinMona.jpg); background-size: contain; background-attachment: fixed;"> <div class="container"> <div class="row"> <div class="col m6 offset-m3"> <div class="card"> <div class="card-content"> <h3 style="margin-top:10px;" class="center-align">Register Here</h3> <%-- creating our own form... ** method is for do and post, ** action is for the servlet class name --%> <div class="form center-align"> <form method="post" action="ServletOne"> <input type="text" name="user_name" placeholder="Enter your name"> <input type="password" name="user_password" placeholder="Enter your password"> <input type="email" name="user_email" placeholder="Enter your email address"> <br> <br> <button class="btn waves-effect waves-light" type="submit" name="action">Submit <i class="material-icons right"></i> </button> <br> <br> <button type="reset" class="reset pink darken-4">Reset All</button> </form> </div> <%--creating our own loader i.e. a circle loading thingy--%> <div class="loader center-align" style="margin-top: 10px; display: none;"> <div class="progress"> <div class="indeterminate"></div> <h5>Please wait while we save ur data...</h5> </div> </div> </div> </div> </div> </div> </div> <%--Jquery Library--%> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> <%--check to see if the jquery library works or not--%> <script> $(document).ready(function() { console.log("Jquery working properly. Page is ready!") }) </script> </body> </html>
index.jsp代码
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html> <head> <title>JSP - Hello World</title> </head> <body> <h1><%= "Hello World!" %> </h1> <br/> <a href="signup.jsp">Hello Servlet</a> </body> </html>
web.xml代码
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" version="5.0"> </web-app>
此外,提交表单前的页面、提交后报错页面及异常堆栈截图已附上。
内容的提问来源于stack exchange,提问作者Michael . _ .
相关产品推荐
相关产品推荐

