Mysql JDBC驱动程序ClassNotFoundException [英] Mysql JDBC driver ClassNotFoundException

查看:96
本文介绍了Mysql JDBC驱动程序ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在桌面APP中注册Mysql JDBC驱动程序

I can't register Mysql JDBC driver in my desktop APP

我下载了mysql-connector-java-5.1.16.zip

I download mysql-connector-java-5.1.16.zip

解压缩 mysql-connector-java-5.1.16-bin.jar ,并将其放入我应用程序的 lib 文件夹中

Unzip mysql-connector-java-5.1.16-bin.jar and put it into lib folder in my app

将此jar文件添加到Eclipse中的构建路径中

Add this jar file into Build Path in Eclipse

但是Class.forName("com.mysql.jdbc.Driver")抛出ClassNotFoundException

But Class.forName("com.mysql.jdbc.Driver") throws ClassNotFoundException

怎么了?

推荐答案

尝试一下:

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
}

问题在于Class.forName(String)引发一个已检查的异常.使用受检查的异常,您可以:

The issue is that Class.forName(String) throws an checked exception. With a checked exception, you can either:

  1. 捕获异常.
  2. 声明您的方法引发异常. (这是我上面建议的).

以下是捕获异常的示例:

Here is an example of catching the exception:

public static void main(String[] args) throws Exception {
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch(ClassNotFoundException e) {
        //do some exception handling
    }
}

这篇关于Mysql JDBC驱动程序ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆