0

I am trying to make the connection to JDBC by using MySQL backend I have already download the java connector(JAR) for my MySQL and placed it in the lib directory of the project but still not able to make the successful connection the error I am getting says java.lang.ClassNotFoundException: com.mysql.jdbc.Driver along with many other errors.

ProcessLoginReq.java

package mybizlogic;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
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("/ProcessLoginReq")
public class ProcessLoginReq extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection con =null;
Statement sm=null;
ResultSet Vtbl=null;

public ProcessLoginReq() {
    super();
        }


public void init(ServletConfig config) throws ServletException {
    }


protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    String uname=request.getParameter("txtuname");

    try
    {
         Class.forName("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException e)
    {
        e.printStackTrace();
    }

    try
    {
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/users","root","root");
        sm = con.createStatement();
        String msg = ("Select * from users");
        Vtbl = sm.executeQuery(msg);
        Vtbl.next();
        String nm = Vtbl.getString("username");
        PrintWriter resgen = response.getWriter();
        if(uname.equals(nm))
        {
            resgen.println("<html><head>  </head>");
            resgen.println("<title>Process</title><body>");
            resgen.println("Welcome user"+uname);
            resgen.println("</body></html>");
        }
        else
        {
            resgen.println("sorry u r not valid user");
        }
    }
    catch (SQLException ex)
    {
        ex.printStackTrace();   
    }   

}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    service(request,response);
    response.getWriter().append("Served at: ").append(request.getContextPath());
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    service(request,response);
    doGet(request, response);
}
}

login.jsp

<%@ 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>Login Page</title>
</head>
<body>
<form action="procLogin" method="post">
UserName:<input type="text" name="txtuname"/><br/>
     <input type="submit" value ="click to submit"/><br/>
      <input type="reset" value ="click to clear"/><br/>


</form>
</body>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>Issac1</display-name>  

  <servlet>
    <servlet-name>xyz</servlet-name>
    <servlet-class>mybizlogic.ProcessLoginReq</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>xyz</servlet-name>
    <url-pattern>/procLogin</url-pattern>
  </servlet-mapping>

</web-app>
sony
  • 375
  • 1
  • 6
  • 21
  • Have you included java connector jar into projects class path? – justRadojko Feb 10 '16 at 21:45
  • yeah i've mentioned it – sony Feb 10 '16 at 21:46
  • 1
    Just because it's in the lib directory doesn't mean it's loaded. How are you starting this servlet? Can you share any logs from your application? – Hamel Kothari Feb 10 '16 at 21:46
  • I'm calling this servlet from login.jsp file – sony Feb 10 '16 at 21:48
  • i remembered that i properly loaded it by right click on properties>build path>add jar ..... i know i did this – sony Feb 10 '16 at 21:52
  • How are you packaging your app, i.e. building the war (is this a Maven project, or are you exporting a war from an IDE)? This looks like the dependent jar is not packaged into the war you are deploying? – Kevin Hooke Feb 10 '16 at 22:15
  • Depending on what IDE you are using you should keep in mind that the build time classpath (i.e. what you compile against) is not necessarily the same as your runtime classpath (i.e. what you package and deploy to your server) – Kevin Hooke Feb 10 '16 at 22:22

0 Answers0