未找到 JDBC 驱动程序类:com.mysql.jdbc.Driver
我正在使用 maven spring 和 hibernate 开发一个 Web 应用程序,我需要使用 hibernate 创建模式,在我的 pom.xml 中有以下内容来连接到 MySQL 5.5 数据库。
<!-- MySql 5.5 Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
</dependency>
这将 mysql-connector-java-5.1.15.jar
导入到我的 Libraries
下的 Maven 依赖项
中,但是当我尝试连接到数据库时,它给出了我 线程“main”org.hibernate.HibernateException 中出现异常:未找到 JDBC 驱动程序类:com.mysql.jdbc.Driver
。
我已经这样做了无数次,但是当我不习惯使用 Maven 来管理我的依赖项和构建项目时,我就这样做了。我过去只是在 Eclipse IDE 中的 Dynamic Web Project
的 lib
文件夹中使用相同的 jar 文件。
有人可以告诉我这里缺少什么以及我还需要什么才能将此 jar 放在我的构建路径中吗?
谢谢。
I am developing a web application using maven spring and hibernate and I need to create schema using hibernate for which I had the following in my pom.xml
to connect to MySQL 5.5 database.
<!-- MySql 5.5 Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
</dependency>
This imported mysql-connector-java-5.1.15.jar
in my Libraries
under Maven Dependencies
but when I try to connect to database it gives me Exception in thread "main" org.hibernate.HibernateException: JDBC Driver class not found: com.mysql.jdbc.Driver
.
I have done this like gazillions of times, but I did it when I din't used to use Maven to manage my dependencies and build the project. I just used to have it the same jar file in the lib
folder of a Dynamic Web Project
in Eclipse IDE.
Could someone tell me what am I missing here and what else I need to have this jar in my build path?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
首先,我需要连接到 MySQL 5.5 的 jar 应该是 mysql-connector-java-5.1.15-bin.jar,而不是 mysql-connector-java-5.1。 15.jar.其次,这个 jar 在 Maven 存储库中不可用,因此我需要手动将其添加到本地 Maven 存储库,然后将其作为依赖项添加到我的
pom.xml
中。将
mysql-connector-java-5.1.15-bin.jar
添加到本地 Maven 存储库,然后将以下依赖项添加到项目的
pom.xml
中。To start with, the jar that I need to connect to MySQL 5.5 should have been
mysql-connector-java-5.1.15-bin.jar
but notmysql-connector-java-5.1.15.jar
. Secondly, this jar is not available in maven repository so I needed to manually add it to my local maven repository and then added it as a dependency in mypom.xml
.Adding
mysql-connector-java-5.1.15-bin.jar
to the local maven repository byand then adding the following dependency to
pom.xml
of the project.感谢上面的回答 - 只是对 NetBeans 用户的评论:
(名称要替换为您的版本)
c:\Program Files\NetBeans 7.2.1\java\maven\bin
set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.7.0_10
mvn install:install-file -Dfile=mysql-connector-java-5.1.15-bin.jar -DgroupId=mysql -DartifactId=mysql-connector-java -Dversion=5.1.15-bin -Dpackaging=jar
Thanks for the answers above - just a comment for NetBeans users:
(names to be replaced with your versions)
c:\Program Files\NetBeans 7.2.1\java\maven\bin
set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.7.0_10
mvn install:install-file -Dfile=mysql-connector-java-5.1.15-bin.jar -DgroupId=mysql -DartifactId=mysql-connector-java -Dversion=5.1.15-bin -Dpackaging=jar
我的工作解决方案
将上述依赖项添加到 pom.xml 对我有用......
My working solution
adding above dependency to pom.xml works for me...
解压缩生成的 JAR 文件。里面有一个文件夹WEB-INF。然后检查这个WEB-INF/lib中是否存在mysql-connector-java-5.1.15.jar。
Unzip your generated JAR file. There is a folder WEB-INF in. Then check if mysql-connector-java-5.1.15.jar exists in this WEB-INF/lib.
将以下代码添加到 pom.xml 文件
Add belowcode to pom.xml file
使用
SELECT version()
检查MySql版本然后在
pom.xml
application.properties
文件中检查 mysql 版本的依赖关系:Check the MySql version using
SELECT version()
and then check dependency of mysql version in
pom.xml
application.properties
files:如果您使用的是 TOMCAT,请将 .jar 文件放入文件夹 LIB (/lib) 中。
不要忘记重新启动服务器。
Put the .jar file in the folder LIB (/lib) if you are using TOMCAT.
Don't forget to restart the server.