[英]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...屏幕截图在页面底部...
__ 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 中的)
And the stack traces和堆栈跟踪
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.