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>