Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

You have an error in your SQL syntax; check the manual that

I am getting SQL Syntax Error , i don’t know why? I have checked everything but couldn’t get through this error. I am using eclipse.
Here is the code for adding user

package com.DOA;

import java.sql.Connection;
import java.sql.PreparedStatement;

import com.User.UserDetails;

public class UserDOA {
         private Connection conn;

        public UserDOA(Connection conn) {
            super();
            this.conn = conn;
        }
         public boolean addUser(UserDetails us) {
             
             boolean f = false;
             
             try {
                 String query = "insert into user values (name,email,password) (?,?,?)";
                 
                 PreparedStatement ps = conn.prepareStatement(query);
                 ps.setString(1, us.getName());
                 ps.setString(2, us.getEmail());
                 ps.setString(3, us.getPassword());
                 int i = ps.executeUpdate();
                 if(i==1) {
                     f=true;
                 }
                 
             }catch(Exception e) {
                 e.printStackTrace();
             }
             
             return f;
         }
}

Here is the code of Servlet

package com.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import com.DOA.UserDOA;
import com.User.UserDetails;
import com.DB.DBConnect;


import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet{
           public void doPost(HttpServletRequest request, HttpServletResponse response ) throws ServletException,IOException
           {
               String name = request.getParameter("fname");
               String email = request.getParameter("uemail");
               String password = request.getParameter("upassword");
               
               UserDetails us = new UserDetails(); 
               us.setName(name);
               us.setEmail(email);
               us.setPassword(password);
               
               UserDOA dao = new UserDOA(DBConnect.getConn());
               boolean f=dao.addUser(us);
               PrintWriter out = response.getWriter();
               
               if(f) {
                   
                   out.print("User Register Successfully");
               }else {
                   out.print("Data not insert");
               }
           }
}





I started the tomcat server , registered an account but getting data not insert as an output instead of getting the data in the workbench.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your SQL is wrong, it should be:

insert into user (name,email,password) values (?,?,?)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading