码迷,mamicode.com
首页 > 数据库 > 详细

QA-IDEA中用maven配置项目无法加载JDBC

时间:2017-12-07 11:53:38      阅读:526      评论:0      收藏:0      [点我收藏+]

标签:manually   pos   project   bsp   creat   undle   connector   ant   java web   

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Im building Maven Java Web application and when I do

Class.forName("com.mysql.jdbc.Driver");

I get

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 

mysql-connector is added to my pom.xml file like this

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.32</version>
    <scope>provided</scope>
</dependency>

But I keep getting this. I even tried downloading mysql-connector and adding it manually to project, but it doesn‘t change anything.

I also have the same app without Maven, and same code works fine

Answer:

You have set the scope of your dependency as provided. This means that the jar is used when compiling the project (although you shouldn‘t need it to compile it, since you should only use standard JDBC classes), but that it‘s not included in the jar or war created by the build, because this dependency is supposed to be "provided" by the application server where you deploy the application.

So, either you actually intend to have this jar provided, and it should be in the application server‘s classpath, or you want to bundle this jar with the application, and it should have the scope runtime(because you need it to run the app, but not to compile it), instead of provided.

 

QA-IDEA中用maven配置项目无法加载JDBC

标签:manually   pos   project   bsp   creat   undle   connector   ant   java web   

原文地址:http://www.cnblogs.com/alone-striver/p/7998281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!