0

Error opening JDBC, cause: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

my environment variabls are ,

@ user variable

CATALINA --> C:\ApacheTomcat\bin

CATALINA_BASE and CATALINA_HOME --> C:\ApacheTomcat

CLASSPATH --> %CATALINA%\lib\jsp-api.jar;C:\ApacheTomcat\lib\servlet-api.jar;C:\ApacheTomcat\lib\jsp-api.jar;C:\Program Files (x86)\Java\jdk1.7.0_02\lib;C:\Program Files (x86)\Java\jre7\lib;C:\Program Files (x86)\Java\jdk1.8.0_40\lib;C:\Program Files (x86)\Java\jre1.8.0_40\lib;C:\ApacheTomcat\WorkSpace\VipTV\src\mysql-connector-java-5.1.35-bin.jar;%CLASSPATH%

JAVA_HOME --> C:\Program Files (x86)\Java

JRE_HOME --> C:\Program Files (x86)\Java\jre1.8.0_40

path --> C:\ApacheTomcat\bin;C:\Program Files (x86)\Java\jdk1.7.0_02\bin;C:\Program Files (x86)\Java\jre7\bin;C:\Program Files (x86)\Java\jdk1.8.0_40\bin;C:\Program Files (x86)\Java\jre1.8.0_40\bin;C:\Program Files (x86)\MySQL\MySQL Connector J\mysql-connector-java-5.1.35-bin.jar;C:\Program Files (x86)\Microsoft Visual Studio\Common\Tools\WinNT;C:\Program Files (x86)\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Program Files (x86)\Microsoft Visual Studio\Common\Tools;C:\Program Files (x86)\Microsoft Visual Studio\VC98\bin;C:\Program Files (x86)\MySQL\MySQL Server 5.1\bin;%PATH%

@ system variable

PATH --> C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Java\jdk1.7.0_02\bin;C:\Program Files (x86)\Java\jdk1.8.0_40\bin;C:\Program Files (x86)\Microsoft Visual Studio\Common\Tools\WinNT;C:\Program Files (x86)\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Program Files (x86)\Microsoft Visual Studio\Common\Tools;C:\Program Files (x86)\Microsoft Visual Studio\VC98\bin;%PATH%;C:\Program Files (x86)\MySQL\MySQL Server 5.1\bin

( i am not very sure about by environment variable setting ; )

i use Tomcat 8

<html>
<head>
<%@ page import="java.sql.*" %>
<%-- page import="com.mysql.jdbc.Driver" --%>
<%
// mysql driver
String driver = "com.mysql.jdbc.Driver";
out.println(driver);
// the "url" to our DB, the last part is the name of the DB
String url = "jdbc:mysql://localhost:3306/viptv";

String name = "root";
String pass = "";
%>
<title>testServlet</title>
</head>
<body>
<p>Attempting to open JDBC connection to:... </p> <%=url%>
<%
try
{
// Test the DB connection by making an empty table
String tableStr = "CREATE table new2 (testid mediumint(8), name varchar(100))";
out.println("<br>"+tableStr);

Class.forName( driver );
out.println("driver loading  done<br>");

// initialize the Connection, with our DB info ...
Connection con = DriverManager.getConnection( url, name, pass );

out.println("connection done<br>");

Statement stat = con.createStatement();

out.println("at statment.");

%>
<p> executing: <%=tableStr%></p>
<%
stat.executeUpdate( tableStr );
%>
<p> success.... </p>

<%
// close connection
con.close();
}

catch (SQLException sqle)
{ out.println("<p> Error opening JDBC, cause:</p> <b> " + sqle + "</b>"); }

catch(ClassNotFoundException cnfe)
{ out.println("<p> Error opening JDBC, cause:</p> <b>" + cnfe + "</b>"); }

%>
</body>
</html>

i have installed mysql-connector-java-5.1.35-bin.jar in my src folder of y project .

pls make the answer as simple as u can . Thx in advance .. :)

9000
  • 39,899
  • 9
  • 66
  • 104
zubin_rafi
  • 1
  • 1
  • 2
  • Please have a look at [this](http://stackoverflow.com/questions/2591505/java-lang-classnotfoundexception-com-mysql-jdbc-driver) – Tom Nijs Apr 20 '15 at 18:41
  • What does this have to do with Eclipse? If you were launching it from Eclipse, the CLASSPATH environment variable is not used. Is the MySQL connector jar in your app's WEB-INF/lib directory? – nitind Apr 20 '15 at 18:52

2 Answers2

3

The CLASSPATH entries are only used by java.exe command As Suggested by this post

However, since you are working on a web project, there are two things you can do here :

1. If you are using the Command Line (CLI) to manually compile the java classes and then deploying it manually to your tomcat, you will need to do either :

A . Copy the mysql-connector-java-xx.jar to your Applications WEB-INF/lib directory. OR

B . Copy the mysql-connector-java-xx.jar to your Tomcat's lib (TOMCAT_HOME\lib) directory so that you don't have to do the above step every time.

AND

2. If you are using Eclipse IDE then :

A . Right Click on your project (Or press ALT + ENTER after selecting your project).

B . In the Pop-up window just launched Select Deployment Assembly from the list on the left hand side.

C . Then Click on Add.

D . Then Select Build Path Entries from the list in the pop up window .

E . Select the jars you want to add from this list just opened.

This will instruct Eclipse to copy the selected jars from your build path to your applications WEB-INF/lib directory.

OR else you can also manually copy the required jar file to your Apps WEB-INF/lib directory.

Community
  • 1
  • 1
Amitesh Rai
  • 866
  • 11
  • 21
1

You have the import commented out.

<%@ page import="java.sql.*" %>
<%-- page import="com.mysql.jdbc.Driver" --%>

should be

<%@ page import="java.sql.*, com.mysql.jdbc.Driver"%>

If that generates an error, post it here.

Also, consider changing how you create the class to specifically create an instance in the driver manager:

Class.forName("com.mysql.jdbc.Driver").newInstance(); 
Foster Bass
  • 898
  • 10
  • 11
  • I don't this this is causing the problem because the "com.mysql.jdbc.Driver" is passed as a string arg to Class.forName() – Amitesh Rai Apr 20 '15 at 19:31
  • But it's not instantiated in the jsp that way. I updated my answer to show how to instantiate it. – Foster Bass Apr 20 '15 at 19:36
  • Even then there is no need to import com.mysql.jdbc.Driver as OP is not using it as Java variable but only as a String type. However, a +1 to catch this minute detail. – Amitesh Rai Apr 20 '15 at 19:40