huxaiphaer Idris huxaiphaer Idris - 3 months ago 19
MySQL Question

Connecting Mysql with IntelliJ ultimate 2016.2.3 using JSP

Am new in JSP and the IntelliJ IDE am experiencing an error

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
when i try to login inside my Application. Am using IntelliJ as my IDE while developing a JSP project. How can I connect Mysql to a JSP project?

Below is LoginDao.java class

package com.huza.schooldynamic;

/**
* Created by HUZY_KAMZ on 9/8/2016.
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginDao {
public static boolean validate(String name, String pass) {
boolean status = false;
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;

String url = "jdbc:mysql://localhost:3306";
String dbName = "form";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "namungoona";
try {
Class.forName(driver).newInstance();
conn = DriverManager
.getConnection(url + dbName, userName, password);

pst = conn

.prepareStatement("select * from login where user=? and password=?");
pst.setString(1, name);
pst.setString(2, pass);

rs = pst.executeQuery();
status = rs.next();

} catch (Exception e) {
System.out.println(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return status;
}


}

and this is my servlet class LoginServlet.java

package com.huza.schooldynamic;

/**
* Created by HUZY_KAMZ on 9/8/2016.
*/
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet{

private static final long serialVersionUID = 1L;

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

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String n=request.getParameter("username");
String p=request.getParameter("userpass");

HttpSession session = request.getSession(false);
if(session!=null)
session.setAttribute("name", n);

if(LoginDao.validate(n, p)){
RequestDispatcher rd=request.getRequestDispatcher("welcome.jsp");
rd.forward(request,response);
}
else{
out.print("<p style=\"color:red\">Sorry username or password error</p>");
RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
rd.include(request,response);
}

out.close();
}
}


This is my index.jsp file

<%--
Created by IntelliJ IDEA.
User: HUZY_KAMZ
Date: 9/8/2016
Time: 5:31 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>School Management System</title>
</head>
<body>

<br>
<br>
<br>
<center>
<form action="loginServlet" method="post">
<fieldset style="width: 300px">
<legend> Login here </legend>
<table>
<tr>
<td>User ID</td>
<td><input type="text" name="username" required="required" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="userpass" required="required" /></td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
</tr>
</table>
</fieldset>
</form>

</center>


</body>
</html>


This is my welcome.jsp file

<%--
Created by IntelliJ IDEA.
User: HUZY_KAMZ
Date: 9/8/2016
Time: 6:00 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Welcome <%=session.getAttribute("name")%></title>
</head>
<body>
<h3>Login successful!!!</h3>
<h4>
Hello,
<%=session.getAttribute("name")%></h4>
</body>
</html>


And finally this is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.huza.schooldynamic.LoginServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


So currently i have tried to view Mysql in the IntelliJ IDE but it seems i don't know how to connect it to the project , when i run the app and i try ti login this error comes in the IDE
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
.

Below is the picture of the IDE

So in that picture , i view the database and the table , but how can i connect it to the project

Answer

The ClassNotFoundException is telling you that it cannot find the class com.mysql.jdbc.Driver on your classpath. In other words, non of the library JARs you have defined in your project contain that class.

That particular class is a MySQL specific implementations of the java.sql.Driver interface. (If you do not understand what an interface is, you can start wit the Java tutorial's page on it. But you'll want to do more learning about it since interfaces are a core concept of OOP).

Your DAO class says to use this particular diver in the line String driver = "com.mysql.jdbc.Driver";. But when the code is running, it is not finding a class by that name. So you need to add that class to your classpath. By going to Advance Search Page at the Maven Central, you can search for that (or any other) class to determine what libraries have it. In this particular case, it is in the mysql-connector-java JAR; the latest version of which is v6.0.4.

So yo need to add the mysql-connector-java JAR to your classpath , that is add it as a library to your Java project. How you do that depends on how you have set up your project.

If you are using Maven, you can add the required dependency:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.4</version>
</dependency>

If you are using Gradle, add

compile 'mysql:mysql-connector-java:6.0.4'

If you are using Ant with Ivy, add

<dependency org="mysql" name="mysql-connector-java" rev="6.0.4" />

All those declarations I copied from the information page for that JAR file at maven central.

If you are not using one of the above build tools, you will need to add the JAR to your project in another way. For example, if you are just using the Project configuration in IDEA, go to Project Structure (Ctrl+Shift+Shift+S / ;) and add it a new library to the module. See the Library IntelliJ IDEA help page and the pages it references for more information.