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. when I want it to save it in the MySQL database using JDBC mysql-connector, it shows this error. 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.

__ This is the servlet class πŸ‘‡ __

@WebServlet(name = "ServletOne", value = "/ServletOne")
public class ServletOne extends HttpServlet {
    private Connection con;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        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

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

                    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
                    out.println("<br><h1>ur information have been saved into MySQL Database :)</h1>");

                } catch (Exception e) {
                    out.println("<h3>An Error has occurred while connecting to MySQL Database.</h3>");
            } else
                out.println("Database connection terminated");
        } catch (SQLException e) {

__ signup.jsp file πŸ‘‡ __

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <!-- importing the css materialize library -->
    <!-- Compiled and minified CSS -->
    <link rel="stylesheet" href="">
    <!-- Compiled and minified JavaScript -->
    <script src=""></script>
<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>
                            <br> <br>
                            <button type="reset" class="reset pink darken-4">Reset All</button>
                    <%--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>
<%--Jquery Library--%>
<script src="" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<%--check to see if the jquery library works or not--%>
    $(document).ready(function() {
        console.log("Jquery working properly. Page is ready!")

__ index.jsp πŸ‘‡ __

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
    <title>JSP - Hello World</title>
<h1><%= "Hello World!" %>
<a href="signup.jsp">Hello Servlet</a>

__ web.xml πŸ‘‡ __

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""


When I comment out the jdbc part, and let’s say I want to show all the info, it’ll work. But when I want to save it in the database, it refuses and throws exception.

like this πŸ‘‰ out.println(name + password + email);


These are the screenshots of it (both the stackTrace and in the web)πŸ‘‡

before submitting:
enter image description here

  • before submitting..

enter image description here

  • after submitting..

And the stack traces πŸ‘‡

enter image description here

  • before submitting..
    enter image description here
  • 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");

Answered By – xienan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published