I wrote code that adds the student's first and last name to the database. I wrote everything, but for some reason it only displays NULL

My jsp

<%-- Document : index Created on : 26.02.2019, 9:56:07 Author : Adil --%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Mysql Database Connection using jsp, servlet and tomcat</title> </head> <body> <h1>Student</h1> <form action="firstdbserv" method="Post"> Name : <input type="text" name="name"><br><br> Surname : <input type="text" name="surname"><br><br> <input type="submit" value="Submit"><br> </form> </body> </html> 

My servlet

 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author Adil */ public class firstdbserv extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet firstdbserv</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet firstdbserv at " + request.getContextPath() + "</h1>"); out.println("</body>"); out.println("</html>"); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); PrintWriter out = response.getWriter(); String name = request.getParameter("name"); String surname = request.getParameter("surname"); try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school_db_newnew?useTimezone=true&serverTimezone=GMT","root","123456"); Statement st = conn.createStatement(); String sql = "INSERT INTO student (name, surname) VALUES( name,surname)"; st.executeUpdate(sql); out.println("Data is Successfully Inserted into Student Table"); }catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } } 

Closed due to the fact that off-topic participants are Roman C , freim , LFC , iluxa1810 , aleksandr barakin Feb 27 at 14:32 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reasons:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - iluxa1810, aleksandr barakin
  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Roman C, freim, LFC
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    Request failed. Use preparedStatement - Oleksiy Morenets

1 answer 1

You have the wrong way to pass parameters. Should they be passed through ? .

Replace

 Statement st = conn.createStatement(); String sql = "INSERT INTO student (name, surname) VALUES( name,surname)"; st.executeUpdate(sql); 

on

 String sql = "INSERT INTO student (name, surname) VALUES(?,?)"; try(PreparedStatement st = con.prepareStatement(sql)){ st.setString(1,name); st.setString(2,surname); st.executeUpdate(); }; 

PS Pay attention to the try-with-resource construction. Creating a Connection also desirable to enclose in such a structure.