java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/

13 篇文章 0 订阅
9 篇文章 0 订阅

以前很少在eclipse中运行项目,昨天想跑一跑jetty+nginx一路报错,试试在tomcat中正常不,所以在eclipse中跑一个示例.

五月 31, 2016 12:12:10 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]
五月 31, 2016 12:12:10 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8009"]
五月 31, 2016 12:12:10 上午 org.apache.catalina.startup.Catalina start
信息: Server startup in 3828 ms
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/apo_htage
    at java.sql.DriverManager.getConnection(DriverManager.java:596)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    ...
    at java.lang.Thread.run(Thread.java:745)

驱动没加载?进行以下检查:
1.代码是否少了Class.forName

    private final static String URL="jdbc:mysql://localhost:3306/databaseName" ;
    private final static String UN="root";
    private final static String PS="root";

    public static Connection getInstance(){
        Connection conn=null;
        try {
            //default:
            conn=DriverManager.getConnection(URL,UN,PS);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

2.项目中Libraries中是否有mysql.jar,主要检查
Apache Tomcat vX[Apache Tomcat]
Web App Libraries

这里写图片描述

如果少了某一个在项目右键菜单中Build Path > Configure Build Path > 右侧的Add Library

这里写图片描述

Server Runtime是添加Tomcat的
Web App Libraries是添加项目WEB-INF/lib下的

刚才我试了一下,只要以上都无误不用把mysql.jar 添加到Server中的Classpath中也不会报

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/

我昨天之所以产生此异常是因为我的java源码少了Class.forName,添加后如下

    private final static String URL="jdbc:mysql://localhost:3306/databaseName" ;
    private final static String UN="root";
    private final static String PS="root";
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static Connection getInstance(){
        Connection conn=null;
        try {
            //default:
            conn=DriverManager.getConnection(URL,UN,PS);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

如果以上都没问题?
1.java源码中有Class.forName
2.项目的Libraries中可以找到mysql.jar
3.连接的地址写的无误

还是报该异常就往下看! @20160531


@Since 20160530
我在tomcat中的lib放了mysql.jar了,一想现在用的是eclipse集成的tomcat7

这里写图片描述

知道问题了,eclipse集成的tomcat在哪呢?

这里写图片描述

右键打开菜单,open或者按F3

这里写图片描述

Server Locations是disabled,点机:open launch configuration

这里写图片描述

Arguments标签页中的VM arguments中已经可以看到全路径了。打开

这里写图片描述

没有lib文件夹,只能还回到Edit Configuration配置窗口中,点开Classpath标签页

这里写图片描述

选中Bootstrap Entries后,点机右侧的Add External Jars找到你本地的mysql.jar后添加即可。图中显示我已经加完.

再运行你的项目,就不会报:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/

当然前提是你的jdbc 驱动url写的没有错误.白渡也有说放到\jre\lib\ext目录下,个人不推荐!不能为了一个小错误破坏整个布署环境.如果不在eclipse中运行项目,直接在本机的tomcat中运行是不会出这种错误的.不论mysql.jar是放到tomcat/lib还是项目的WEB-INF\lib下都不会报

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值