0

I have created a login and registration page but when CREATE ACCOUNT button is clicked the data is not getting stored into the data base. It gives a exception saying

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

I have added the mysql jar file in my project.

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

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

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


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String options=request.getParameter("button");
        System.out.println(options);
        PrintWriter pw=response.getWriter();


        if(options.equals("LOGIN")) {

                String username=request.getParameter("loginid");
                String password=request.getParameter("loginpassword");

            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/loginclass","root","root");
                PreparedStatement login=conn.prepareStatement("select * from user_info where username=? and userpassword=?");
                login.setString(1, username);
                login.setString(2, password);

                ResultSet rs=login.executeQuery();
                    if(rs.next()) {

                        pw.println("Welcome");
                        pw.println("Welcome"+username);
                    }else {
                      pw.println("Error");
                    }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }



        if(options.equals("CREATE ACCOUNT")) {

             try {

                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/loginclass","root","root");


                    PreparedStatement register=conn.prepareStatement("insert into user_info values (?,?,?,?,?,?)");

                    String firstname=request.getParameter("fname");
                    String lastname=request.getParameter("lname");
                    String email=request.getParameter("email");
                    String phone=request.getParameter("phone");
                    String uname=request.getParameter("uname");
                    String pass=request.getParameter("password");

                    register.setString(1, firstname);
                    register.setString(2, lastname);
                    register.setString(3, email);
                    register.setString(4, phone);
                    register.setString(5, uname);
                    register.setString(6, pass);
                    register.executeUpdate();

                    }// try
                    catch (Exception e) {
                        e.printStackTrace();
                    }//catch

        }// if create acc.

    }// dopost
}//class
James Z
  • 12,209
  • 10
  • 24
  • 44
Pranav N.
  • 23
  • 6
  • Try Clean and Build of your project. Also which server are you using? – hiren Mar 11 '18 at 08:46
  • I did clean my project.... Nothing changed... I'm using tomcat server @hiren – Pranav N. Mar 11 '18 at 08:49
  • 2
    You haven't needed this line of code for 11 years, but you do need the JAR concerned to be on the CLASSPATH. – user207421 Mar 11 '18 at 08:52
  • @EJP That is not entirely true: if the JDBC driver is deployed together with the web application (it is in inside the WAR), then it needs to be loaded explicitly. Automatic driver loading only works if the driver is on the initial (system) classpath. Not if it is only on the context classpath. – Mark Rotteveel Mar 11 '18 at 09:07

1 Answers1

1

I think you need to add the jar in your tomcat directory's lib folder. Add the jar there and do a restart of the server.

This might do it for you.

hiren
  • 1,067
  • 1
  • 9
  • 16