Issue
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;
@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 π __
<%@ 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>
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..
- after submitting..
And the stack traces π
sorry the question has gotten really long. really appreciate your answerπππ
Solution
Connection con = DriverManager.getConnection("jdbc:mysql://localhost3306/register", "root", "root");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/register", "root", "root");
Answered By - xienan
Answer Checked By - Mildred Charles (JavaFixing Admin)