0

I am trying to save records into mysql database by using java servlet and html . I added the mysql connector jr files inside the project . but the problem is when i clicked add button , its not inserting records . In console windows I am getting following errors .

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver java.lang.NullPointerException

Here is code DAO .

package employeeDAO;


import java.util.*;

import employee.Emp;

import java.sql.*; 

public class EmpDAO {  

    public static Connection getConnection(){  
        Connection con=null;  
        try{  
            Class.forName("com.mysql.jdbc.Driver");  
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user905","root","");  
        }catch(Exception e){System.out.println(e);}  
        return con;  
    }  
    public static int save(Emp e){  
        int status=0;  
        try{  
            Connection con=EmpDAO.getConnection();  
            PreparedStatement ps=con.prepareStatement(  
                         "insert into user905(name,password,email,country) values (?,?,?,?)");  
            ps.setString(1,e.getName());  
            ps.setString(2,e.getPassword());  
            ps.setString(3,e.getEmail());  
            ps.setString(4,e.getCountry());  

            status=ps.executeUpdate();  

            con.close();  
        }catch(Exception ex){ex.printStackTrace();}  

        return status;  
    }

Here is code for servlet .

package Servlet.org;
import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import employee.Emp;
import employeeDAO.EmpDAO;

/**
 * Servlet implementation class SaveServlet
 */
@WebServlet("/SaveServlet")
public class SaveServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public SaveServlet() {
        super();
        // TODO Auto-generated constructor stub
    }



        protected void doPost(HttpServletRequest request, HttpServletResponse response)   
             throws ServletException, IOException {  
            response.setContentType("text/html");  
            PrintWriter out=response.getWriter();  

            String name=request.getParameter("name");  
            String password=request.getParameter("password");  
            String email=request.getParameter("email");  
            String country=request.getParameter("country");  

            Emp e=new Emp();  
            e.setName(name);  
            e.setPassword(password);  
            e.setEmail(email);  
            e.setCountry(country);  

            int status=EmpDAO.save(e);  
            if(status>0){  
                out.print("<p>Record saved successfully!</p>");  
                request.getRequestDispatcher("index.html").include(request, response);  
            }else{  
                out.println("Sorry! unable to save record");  
            }  

            out.close();  
        }
    }

Here is code for html

<!DOCTYPE html>  
<html>  
<head>  
<meta charset="ISO-8859-1">  
<title>Insert title here</title>  
</head>  
<body>  

<h1>Add New Employee</h1>  
<form action="SaveServlet" method="post">  
<table>  
<tr><td>Name:</td><td><input type="text" name="name"/></td></tr>  
<tr><td>Password:</td><td><input type="password" name="password"/></td></tr>  
<tr><td>Email:</td><td><input type="email" name="email"/></td></tr>  
<tr><td>Country:</td><td>  
<select name="country" style="width:150px">  
<option>India</option>  
<option>USA</option>  
<option>UK</option>  
<option>Other</option>  
</select>  
</td></tr>  
<tr><td colspan="2"><input type="submit" value="Save Employee"/></td></tr>  
</table>  
</form>  

<br/>  
<a href="ViewServlet">view employees</a>  

</body>  

Here is screen shot for adding mysql connector files.

enter image description here here is screen shot when i clicked the save button .

enter image description here

Mohammad
  • 1,019
  • 8
  • 24
  • *I added the mysql connector jr files inside the project*: how did you do that? Where did you add it? – JB Nizet Jan 11 '20 at 10:41
  • i added into refernace library folder – Mohammad Jan 11 '20 at 10:42
  • That's not where it should be. In aJeve EE web app, the classpath is the set of jars in WEB-INF/lib (+ the directory WEB-INF/classes). – JB Nizet Jan 11 '20 at 10:44
  • this is not javaee applications . I am using servlet and dispaying the result into html page – Mohammad Jan 11 '20 at 10:46
  • Yes, and servlets are part of Java EE, and deployed as part of Java EE web applications. – JB Nizet Jan 11 '20 at 10:47
  • where should i added this ? – Mohammad Jan 11 '20 at 10:49
  • I told you where. The linked duplicate also tells you where: under WEB-INF/lib, in your Eclipse WebContent folder. – JB Nizet Jan 11 '20 at 10:50
  • I am really confused . can you please explain more details – Mohammad Jan 11 '20 at 10:55
  • 2
    I posted **two** duplicates explaining it in details. Take the time to read them. – JB Nizet Jan 11 '20 at 10:55
  • When i clicked the lib folder and trying to add the mysql conncetor its showing alrady added . I am added the screren shot . please check – Mohammad Jan 11 '20 at 11:01
  • 2
    Let's do this step by step. Click on the link I posted: https://stackoverflow.com/questions/11652431/eclipse-add-jar-to-dynamic-web-project. 2.Now scroll to the first, accepted answer. 3. Read what it says. I read: "Paste the jar in WebContent/WEB-INF/lib". Have you done that? No you haven't. You have added the jar to the Java Build path. That's what's used at compilation time, but not at runtime. That's explained by the other link I posted. Do what the accepted answer says, and what I also said: paste it in WebCotent/WEB-INF/lib. – JB Nizet Jan 11 '20 at 11:09

0 Answers0