简体   繁体   中英

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)

//更正

 Use Jar file Instead of Zip in your class path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM