Roshan
Roshan

Reputation: 645

ClassNotFoundException when trying to connect with mysql dbms?

I have seen many solutions in this site relating the same problem that I am facing now, I rectified my project structure also but still no success,

First attempt I placed mysqlconnector.jar in WEB-INF/lib -> GOT ERROR. Second, I removed it from lib folder, and added as external jar in build path -> GOT ERROR. Also I tried both above ways after placing mysqlconnector jar file in jre8/lib/ext -> NO SUCCESS!

Please ignore any small or irrelevant modifications, I just needed to check the connectivity thats all. I have JRE8 and Tomcat8 server running, which I believe is not an issue in this.

Here is a simple code of connectivity I just want to check if it is establishing a connection or not, that is why I haven't coded any further.

public class MySQLConnection {

    public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver"; 
    public static final String URL = "jdbc:mysql://localhost:3306/payinguest";
    public static final String USER = "root";
    public static final String PASSWORD = "root";


    public MySQLConnection() {
        try {
            Class.forName(DRIVER_CLASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
  }

My index.jsp is

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.pg.dao.MySQLConnection"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% MySQLConnection db = new MySQLConnection();%>
</body>
</html>

Error:

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1324)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1177)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at com.pg.dao.JDBCMySQLConnection.<init>(JDBCMySQLConnection.java:15)
        at org.apache.jsp.index_jsp._jspService(index_jsp.java:71)

Upvotes: 0

Views: 109

Answers (1)

Lokesh
Lokesh

Reputation: 313

//Correction

 Use Jar file Instead of Zip in your class path.

Upvotes: 1

Related Questions