MySQL JDBC驱动程序未找到错误。在课程路径中,验证

时间:2017-08-07 03:22:21

标签: java mysql

我不能让Java MySQL Connector在我的生命中连接到MySQL ...我通过apt-get安装它并将JAR下载到" Dependencies"我正在构建的Java包中的文件夹。

javac -cp ./Dependencies/mysql-connector-java-5.1.43-bin.jar:/usr/share/java/* -source 1.8 -target 1.8 -d . SDUtils/*.java
added manifest
adding: Build.sh(in = 158) (out= 119)(deflated 24%)
adding: Dependencies/(in = 0) (out= 0)(stored 0%)
adding: Dependencies/mysql-connector-java-5.1.43-bin.jar(in = 999018) (out= 950443)(deflated 4%)
adding: Legacy/(in = 0) (out= 0)(stored 0%)
adding: Legacy/SD-Utils.py(in = 3576) (out= 1268)(deflated 64%)
adding: SDUtils/(in = 0) (out= 0)(stored 0%)
adding: SDUtils/dbConnector.java(in = 729) (out= 402)(deflated 44%)
adding: SDUtils/SDUtils.java(in = 4091) (out= 1589)(deflated 61%)
adding: SDUtils/dbConnector.class(in = 1299) (out= 769)(deflated 40%)
adding: SDUtils/SDUtils.class(in = 2574) (out= 1568)(deflated 39%)

运行SDUtils / dbConnector的结果:

Where is your MySQL JDBC Driver?
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at SDUtils.dbConnector.main(dbConnector.java:9)

来源:

package SDUtils;

import java.sql.*;

public class dbConnector {

    public static void main (String[] args) {

        try { Class.forName("com.mysql.jdbc.Driver"); }

        catch (ClassNotFoundException e) {

            System.out.println("Where is your MySQL JDBC Driver?");
            e.printStackTrace();
            return;

        }

        String dbUrl = "jdbc:mysql://localhost:3306/Core";
        String uName = "MyUser";
        String uPass = "MyPass";

        System.out.println("Connecting database...");

        try {
            Connection con = DriverManager.getConnection(dbUrl, uName, uPass);
            System.out.println("Database connected! (Core)");
        }

        catch (SQLException err) { System.out.println(err.getMessage()); }
        catch (Exception e) { System.out.println(e); }

    }

}

ps -aux |的结果grep mysql:

mysql     2929  0.1  1.8 2310936 602096 ?      Ssl  15:43   0:40 /usr/sbin/mysqld
astump   22480  0.0  0.0  14240   948 pts/3    S+   22:20   0:00 grep --color=auto mysql

2 个答案:

答案 0 :(得分:0)

此异常是由于类路径中缺少jar。您需要在运行程序时使用“java”命令在类路径中指定jar路径,以解决此问题。

答案 1 :(得分:0)

所以我真的想出了真正的问题。我使用的是mysql-connector-java-5.1.43-bin.jar - 我本应该使用没有-bin的那个。