简体   繁体   English

com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链路故障。 驱动程序没有收到来自服务器的任何数据包

[英]com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure. The driver has not received any packets from the server

I have a Servlet and jsp project in which it takes the name and pass.我有一个 Servlet 和 jsp 项目,它在其中获取名称并通过。 when I want it to save it in the MySQL database using JDBC mysql-connector, it shows this error.当我希望它使用 JDBC mysql-connector 将其保存在 MySQL 数据库中时,它会显示此错误。 Please help me I'm stuck in this for a long time.请帮助我,我被困了很长时间。
Screenshots are at the bottom of the page...屏幕截图在页面底部...

I'm using IntelliJ JetBrains and tomcat 10.我正在使用 IntelliJ JetBrains 和 tomcat 10。

__ This is the servlet class __ __ 这是 servlet class __

@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 file __ __ 注册.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 __ __ 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 __ __ 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>

When I comment out the jdbc part, and let's say I want to show all the info, it'll work.当我注释掉 jdbc 部分,假设我想显示所有信息时,它会起作用。 But when I want to save it in the database, it refuses and throws exception.但是当我想将它保存在数据库中时,它会拒绝并抛出异常。

like this out.println(name + password + email);像这样 out.println(name + password + email);

================================================================ ==================================================== ===============

These are the screenshots of it (both the stackTrace and in the web)这些是它的屏幕截图(stackTrace 和 web 中的)

before submitting:提交前: 在此处输入图像描述

  • before submitting..提交之前..

在此处输入图像描述

  • after submitting..提交后..

And the stack traces和堆栈跟踪

在此处输入图像描述

  • before submitting..提交之前.. 在此处输入图像描述
  • after submitting and the exceptions...提交后和例外情况...

sorry the question has gotten really long.抱歉这个问题已经很长了。 really appreciate your answer非常感谢你的回答

                Connection con = DriverManager.getConnection("jdbc:mysql://localhost3306/register", "root", "root");


                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/register", "root", "root");

It seems I had been missing a syntax error.看来我错过了一个语法错误。 When I was trying to connect to the JDBC using DriverManager , I had forgotten to write ("jdbc:mysql://*localhost: 3306*/register") [the colon was forgotten]当我尝试使用DriverManager连接到 JDBC 时,我忘记写("jdbc:mysql://*localhost: 3306*/register") [冒号被遗忘]

Thanks for everyone's help.感谢大家的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链接失败 - com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure Spring MySQL com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链接失败 - Spring MySQL com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure com.mysql.cj.jdbc.exceptions.CommunicationsException:Z05B60653C41A8BDA4E6DFC3AFD31A214E6D中的通信链路故障 - com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure in docker com.mysql.cj.jdbc.exceptions.CommunicationsException:Z9CE3D468890F9087C项目中的通信链路故障 - com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure in JPA project com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure - 仅在 docker 中部署应用程序时失败 - com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure - Fails only when deploying the app in docker com.mysql.cj.jdbc.exceptions.CommunicationsException:GCP 中的通信链路故障 - CloudSQL - com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure in GCP - CloudSQL com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链路故障 2021 - com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure 2021 com.mysql.cj.jdbc.exceptions.CommunicationsException:尝试构建 spring 引导时通信链接失败 docker 图像 - com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure when trying to build spring boot docker image com.mysql.cj.jdbc.exceptions.CommunicationsException:从服务器成功收到的最后一个数据包是XXXXXXXXXXXX毫秒前 - com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was XXXXXXXXXXXX milliseconds ago com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败? - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM