Tomcat JDBC MySQL ClassNotFoundException - mysql

I would like to use springMVC and JPA (using hibernate) on my tomcat 7 server (running locally on my Mac).
I was able to set up everything successfully with an embedded H2 database.
Now I switched to mysql and am getting the following error
java.lang.ClassNotFoundException: "com.mysql.jdbc.Driver"
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
This suggests tomcat is having trouble finding the mysql-connector for java.
There are a multitude of tutorials on how to add the connector to $CATALINA_HOME/lib.
After trying to use a maven dependency for my project, I followed the advice and copied the .jar file into the lib directory:
$ ls $CATALINA_HOME/lib/mysql*.jar
/Users/david/Applications/tomcat/lib/mysql-connector-java-5.1.20-bin.jar
I have read and execute permissions on the directory and file.
At the moment I can't figure out how to make tomcat aware of the jar. The folder is included in
$CATALINA_HOME/conf/catalina.properties
and I have restarted the server multiple times.
Thanks for you help.

Normally, the output of a CNFE is as follows:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
whereas your error message shows
java.lang.ClassNotFoundException: "com.mysql.jdbc.Driver"
I think you've still got quotes around the driver class name in your config.

Related

Pantaho MySQL 8 connection error Driver class 'org.gjt.mm.mysql.Driver' could not be found

While upgrading ETL scripts for Mysql 5.8 to MySQL8 upgrade, as soon as I have updated the data-integration/lib jar to mysql-connector-java-8.0.xx.jar, it has started blowing with following error.
Driver class 'org.gjt.mm.mysql.Driver' could not be found, make sure the 'MySQL' driver (jar file) is installed.
Could you please try to add both jars in
data-integration/lib
As I did, I have added the lastest jar of 5.x i.e mysql-connector-java-5.1.48.jarand the same version mine is 8.0.19 so i have added mysql-connector-java-8.0.19.jar i copied into location data-integration/lib .
After the test its working fine now.
I did spent lot of time debugging and finally concluded, two things, I hope this may save others time in similar situation.
Reason: There is hardcoded jdbc drive name in org.pentaho.di.core.database.MySQLDatabaseMeta, and it always returns org.gjt.mm.mysql.Driver which is removed and new Driver with name com.mysql.jdbc.Driver or com.mysql.cj.jdbc.Driver should be used.
Solutions Any of below should be done to resolve.
Continue using the old jdbc jar.
Modify the org.pentaho.di.core.database.MySQLDatabaseMeta below method, compile and place it in classes directory.
public String getDriverClass() {
if (getAccessType()==DatabaseMeta.TYPE_ACCESS_ODBC)
{
return "sun.jdbc.odbc.JdbcOdbcDriver";
}
else
{
return "com.mysql.cj.jdbc.Driver";
} }
Use the Generic database connection, then you can specify the driver class yourself. (Based on #Cyrus comment.)
Pentaho open bug reference.
I was working with pdi 9.1, all I did to get rid of this error was to copy these two jar files in the data-integration\lib:
mysql-connector-java-5.1.49.jar
mysql-connector-java-5.1.49-bin.jar
The link to the zip folder is mentioned in the above comments
Restart your spoon!
And that's it.
i just to use Pentaho version 9.1 - 9.1.0.0-324 and mysql-connector-java-8.0.25
1- Make sure the Pentaho is not running.
2- Download the mysql connector at the link below.(https://mvnrepository.com/artifact/mysql/mysql-connector-java)
3- Copy the .jar file (mysql-connector-java-8.0.25.jar) and paste it in your Lib folder:
Example: C:....\pentaho-data-integration\lib
4- Execute Pentaho (Spoon.bat)
MySQL driver for version 8 changed the class name. Therefore, you must set it as a Generic connection instead, and use com.mysql.jdbc.Driver as the class.
I have fixed the issue by replacing mysql-connector-java-5.1.42-bin.jar with mysql-connector-java-5.1.44.jar
Reference
I am using Pentaho 9.1.0.0-324 and had the same problem. I downloaded the Everything app and through it I found that I have already a file named mysql-connector-j-8.0.31.jar at C:\Program Files (x86)\MySQL\Connector J 8.0\ folder. I simply copied the file into C:\Pentaho\lib and the problem solved. However, now I am getting another error as below:
Connection failed. Verify all connection parameters and confirm that
the appropriate driver is installed. Access denied for user
'root'#'localhost' (using password: YES)
Update
I solved the Access denied error by reinstalling MySQL with legacy password option along with a password without non-alphabetical symbols.

class not found com.mysql.jdbc.Driver jdbc when trying to connect to a Mysql Database

I'm developing a web application in java where I'm trying to connect to a MySQL database using JDBC template ( with springs dao) but I always get:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/DealingOfInsiders] threw exception [Request processing failed;
nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver '] with root cause
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
[....]
I think the problem is coming from the file mysql-connector-java-5.1.25-bin.jar which contains the targeted class ...
So I tried many things to get this problem solve :
I've configured the build path of my project whith this jar but it didn't worked
I ve also put the file in the folder : WEB-INF/lib , didn't worked
I ve configured a classpath variable to this jar in eclipse ( preference ->java->buildpath->classpath variabe ) it didn't worked
I have put the jar file in the tomcat library but it didn't work as well
'com.mysql.jdbc.Driver '
^^ // remove this space
There appears to be a trailing space on the name?
'com.mysql.jdbc.Driver '
Apart from that, "com.mysql.jdbc.Driver" is correct.
The WEB-INF/lib folder is a good place for project-specific drivers & libraries; keeping the Tomcat shared folders up-to-date is not so easy.
Back in JDBC days you had to load the class with Class.forName( clazzName) to register it before you asked JDBC for a connection, but that's not the issue here.
I had this issue but with the Postgres driver and I'm using Eclipse. If you're using Tomcat 7, try adding the JAR file to the lib folder in your Tomcat directory. That worked for me. I didn't have to use Class.forName() or put the JAR in my classpath. I hope this helps you.

MySQL queries cause: SEVERE: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'

Whenever I try (My)SQL queries on the sever (the server is in debug mode in Eclipse) I get this error:
SEVERE: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
Now I can't recall how I got this detail, but I suspect that the following info is relevant:
with %MyWorkspace% being some path like x:\some_path, this path:
%MyWorkspace%\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\rewardy\WEB-INF\lib
contains the mysql-connector-java-5.1.13-bin.jar file, that if I'm not wrong has the com.mysql.jdbc.Driver class that it looks for (am I right ?...)
I use this config: Win7 x64, Eclipse Helios Service Release 2, WAMP Server and Tomcat 6.0.26.
You should have to add location of mysql-connector-java-5.1.13-bin.jar via project properties + Java Build Path + Add External Jars (Eclipse IDE)
have you mysql-connector-java-5.1.13-bin.jar in classpath or in libs/ on tomcat ? – triclosan
Thanks #triclosan, I put it in Tomcat/lib and it works now!

ClassNotFoundException: com.mysql.jdbc.Driver

I wrote java class to connect mysql database and insert data to database on my centos installed pc.(this java file called by asterisk program using AGI). but I got below exception at runtime.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
How could I solve this ?
Ok. I solved my question. when I run the asterisk program with AGI server, I added mysql connector classpath as below.
java -classpath asterisk-java-0.3.jar:mysql-connector-java-5.1.18-bin.jar:. org.asteriskjava.fastagi.DefaultAgiServer
you need this jar file: mysql-connector-java-{version}.jar
e.g.
http://findjar.com/jar/mysql/mysql-connector-java/5.1.9/mysql-connector-java-5.1.9.jar.html

Connecting a MySQL database to Glassfish classpath is not set or classname is wrong

I'm swapping out a derby database for a MySQL one. I had everything working before but after what I thought was the proper configuration I'm getting the error:
Caused by: javax.resource.ResourceException: Class name is wrong or classpath is not set for : com.mysql.jdbc.jdbc2.optional.MysqlDataSource
Full error output from console:
Caused by: javax.resource.ResourceException: Class name is wrong or classpath is not set for : com.mysql.jdbc.jdbc2.optional.MysqlDataSource
at com.sun.gjc.common.DataSourceObjectBuilder.getDataSourceObject(DataSourceObjectBuilder.java:292)
at com.sun.gjc.common.DataSourceObjectBuilder.constructDataSourceObject(DataSourceObjectBuilder.java:114)
at com.sun.gjc.spi.ManagedConnectionFactory.getDataSource(ManagedConnectionFactory.java:1292)
at com.sun.gjc.spi.DSManagedConnectionFactory.getDataSource(DSManagedConnectionFactory.java:148)
at com.sun.gjc.spi.DSManagedConnectionFactory.createManagedConnection(DSManagedConnectionFactory.java:101)
at com.sun.enterprise.resource.allocator.LocalTxConnectorAllocator.createResource(LocalTxConnectorAllocator.java:87)
I've double checked some of the names, the connection pool and other resources.I've also added the MySQL driver .jars to the library of glassfish in both projects. The database was definitely working correctly through eclipse because I was able to view tables and display the resources inside the database context of eclipse. So I know that at least THOSE drivers are working correcly. Also the persistence.xml file looks good. it references the jdbc/mydatabase jndi reference like it should and default JTA is selected as the manament type.
Does anyone have another suggestion? Thank you
I've also added the MySQL driver .jars to the library of glassfish in both projects.
It was apparently not done right. The JAR has to go in /glassfish/domains/[domainname]/lib/ext folder of the Glassfish installation where [domainname] usually defaults to domain1. You can and should not configure it from the Eclipse side on.
Looks like I am replying very late, but however people who would be referring to this thread may find the following information being useful. So I am posting it here:
Download the connector Jar from http://dev.mysql.com/downloads/connector/j/5.0.html
unzip the pack and copy mysql-connector-java-verno-bin.jar
past the same at [GlassFish Installation Directory]/domains/[domain name]/lib folder
restart your domain and ping to check your connection in JDBC Connection Pools
There you go. If your MySql is running then it will ping the DB successfully
I copied the jar file to $glassfish_install_folder\glassfish\lib, after that it worked. I use glassfish 4.0.
Check this link from oracle.
I encountered this issue in 2018 and would like to note that if you are using glassfish 4 (current is 5) then it seems you have to use the Connector/J 5.1.47 version for it to work. If you use the current version (Connector/J 8.0.13) then exception mentioned in the original question keeps appearing, no matter where you place the .jar file.
With Connector/J 5.1.47 it works perfectly.
Solution is
> asadmin add-library /path/to/mysql-connector-java-bin.jar
Check this link:
https://blog.payara.fish/using-mysql-with-payara
I encountered this issue in 2019 and would like to note that if you are using docker image payara/server-full (I am using 5.194 so far) as I do the location to place the driver jar is:
/opt/payara/appserver/glassfish/domains/production/lib/
In the end I am doing something like this in the Dockerfile of payara server:
RUN wget http://central.maven.org/maven2/org/mariadb/jdbc/mariadb-java-client/2.2.0/mariadb-java-client-2.2.0.jar \
-O /opt/payara/appserver/glassfish/domains/production/lib/mariadb-java-client-2.2.0.jar