java 如何导入com.mysql.jdbc.Driver

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10775552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:30:51  来源:igfitidea点击:

How to import com.mysql.jdbc.Driver

javamysql-connector

提问by 11684

I've been trying for a while to connect to my database, and I am pretty close now I think.
But com.mysql.jdbc.Driverrefuses to load.
Here's my code:

我一直在尝试连接到我的数据库,现在我想我已经很接近了。
com.mysql.jdbc.Driver拒绝加载。
这是我的代码:

import java.sql.*;
import com.apple.eawt.*;
import com.mysql.jdbc.Driver;

public class MySQL {
    public MySQL() {
        Connection conn = null;

        try
        {
            String userName = "my_username";
            String password = "*******";
            String url = "jdbc:mysql://korilu.nl/phpMyAdmin/";
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
        }
        catch (SQLException ex)
        {
            System.err.println ("Cannot connect to database server (SQLException)");
            System.out.println(ex.getMessage());
        }
        catch (ClassNotFoundException ex) {
            System.err.println ("Cannot connect to database server (ClassNotFoundException)");
            System.out.println(ex.getMessage());
        }
        catch (InstantiationException ex) {
            System.err.println ("Cannot connect to database server (InstantiationException)");
            System.out.println(ex.getMessage());
        }
        catch (IllegalAccessException ex) {
            System.err.println ("Cannot connect to database server (IllegalAccesException)");
            System.out.println(ex.getMessage());
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close ();
                    System.out.println ("Database connection terminated");
                }
                catch (Exception e) { /* ignore close errors */ }
            }
        }
    }

    public static void main(String args[]) {
        new MySQL();
    }

}

Currently it says: package com.mysql.jdbcdoes not exist, although XCode autocompletes the importline above.
I think I messed up with adding the package to my project, because I did not know how.
I just ctrl+clicked my target and selected Add to target...and added the com folder with the .java files I downloaded.
I think that the compiler doesn't recognize it as a package but I don't know how to make it a package.

目前它说:包com.mysql.jdbc不存在,尽管 XCode 自动完成import上面的行。
我想我搞砸了将包添加到我的项目中,因为我不知道如何。
我只是按 ctrl+单击我的目标并选择Add to target...并添加带有我下载的 .java 文件的 com 文件夹。
我认为编译器不会将其识别为包,但我不知道如何使其成为包。

P.S.
I'm not quite sure about that URL, but that's not the problem for now (but I won't mind someone telling me where antagonist stores their databases).

PS
我不太确定那个 URL,但这不是现在的问题(但我不介意有人告诉我反对者存储他们的数据库的位置)。

采纳答案by rooftop

I am not sure how Xcode handles this as I've not used it for Java development, however the usual cause of this error is the MySQL jdbc (connector) jar file not being on the classpath. Can you show us the command Xcode is using to launch the jvm?

我不确定 Xcode 如何处理这个,因为我没有将它用于 Java 开发,但是这个错误的通常原因是 MySQL jdbc(连接器)jar 文件不在类路径上。你能告诉我们 Xcode 用来启动 jvm 的命令吗?

回答by Vishal Sharma

Just add this library file in your libraries if you haven't yet.

如果您还没有,只需将此库文件添加到您的库中。

mysql connector 5.1

mysql 连接器 5.1