从 Linux 上的 NetBeans 中的 Java 应用程序连接到 MariaDB (Mageia)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23020857/
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-08-13 20:05:38  来源:igfitidea点击:

Connect to MariaDB from Java application in NetBeans on Linux (Mageia)

javamysqlnetbeansjdbcmariadb

提问by Nitish Pareek

I am trying to connect to a database in Mariadb through a simple java application but the connection is told to be unsuccessful and an Exception is thrown. I have done the similar connection using mysql and it was working correctly. The problem is maybe with the driver here.

我试图通过一个简单的 java 应用程序连接到 Mariadb 中的数据库,但连接被告知不成功并抛出异常。我已经使用 mysql 完成了类似的连接,并且它工作正常。问题可能出在这里的驱动程序上。

 try{
          Class.forName("org.mariadb.jdbc.Driver");  

        Connection connection = DriverManager.getConnection(  
                "jdbc:mariadb://localhost:3306/project", "root", "");  
        Statement statement = connection.createStatement(); 

        String uname="xyz",pass="abc";
       statement.executeUpdate("insert into user values('"+uname+"','"+pass+"')");}//end of try block

I looked up the internet for the help and came by that driver class provided by the MariaDB Client Library for Java Applications is not com.mysql.jdbc.Driver but org.mariadb.jdbc.Driver! I changed it accordingly but it seems the problem is with the very first line inside the try block. The driver is not loading at all. Also, I have added the mysql jar file to the libraries of my java application as in the screen-shot below. Please help me through this.enter image description here

我在互联网上查找帮助,发现 MariaDB Client Library for Java Applications 提供的驱动程序类不是 com.mysql.jdbc.Driver 而是 org.mariadb.jdbc.Driver!我相应地更改了它,但似乎问题出在 try 块内的第一行。驱动程序根本没有加载。此外,我已将 mysql jar 文件添加到我的 java 应用程序的库中,如下面的屏幕截图所示。请帮我解决这个问题。enter image description here

采纳答案by Gord Thompson

It appears that you are trying to use jdbc:mariadb://...to establish a connection to a MariaDB server instance using the MySQL JDBC Driver. That probably won't work because the MySQL JDBC Driver would use jdbc:mysql://..., regardless of whether it is connecting to a MySQL server or a MariaDB server. That is, the connection string must match the driverthat is being used (rather than the database server being accessed).

您似乎正在尝试使用jdbc:mariadb://...MySQL JDBC 驱动程序建立与 MariaDB 服务器实例的连接。这可能不起作用,因为 MySQL JDBC 驱动程序将使用jdbc:mysql://...,无论它是连接到 MySQL 服务器还是 MariaDB 服务器。也就是说,连接字符串必须匹配正在使用的驱动程序(而不是正在访问的数据库服务器)。

The MySQL and MariaDB drivers are supposed to be somewhat interchangeable, but it only seems prudent to use the MariaDB connector when accessing a MariaDB server. For what it's worth, the combination of mariadb-java-client-1.1.7.jar

MySQL 和 MariaDB 驱动程序应该可以互换,但在访问 MariaDB 服务器时使用 MariaDB 连接器似乎是谨慎的做法。对于它的价值,组合mariadb-java-client-1.1.7.jar

projectProperties.png

projectProperties.png

and

Connection con = DriverManager.getConnection(
        "jdbc:mariadb://localhost/project", 
        "root", 
        "whatever");

worked for me. I downloaded the MariaDB Client Library for Java from here:

为我工作。我从这里下载了用于 Java 的 MariaDB 客户端库:

https://downloads.mariadb.org/client-java/1.1.7/

https://downloads.mariadb.org/client-java/1.1.7/

which I arrived at via

我是通过

https://downloads.mariadb.org/

https://downloads.mariadb.org/

Additional notes:

补充说明:

  1. There is no need for a Class.forName()statement in your Java code.

  2. The default configuration for MariaDB under Mageia may include the skip-networkingdirective in /etc/my.cnf. You will need to remove (or comment out) that directive if you want to connect to the database via JDBC because JDBC connections always look like "network" connections to MySQL/MariaDB, even if they are connections from localhost. (You may need to tweak the bind-addressvalue to something like 0.0.0.0as well.)

  1. Class.forName()Java 代码中不需要语句。

  2. 为MariaDB的Mageia下默认配置可能包括skip-networking在指令/etc/my.cnf。如果您想通过 JDBC 连接到数据库,则需要删除(或注释掉)该指令,因为 JDBC 连接看起来总是像到 MySQL/MariaDB 的“网络”连接,即使它们是来自localhost. (您可能还需要将bind-address值调整为类似的值0.0.0.0。)

回答by Basil Musa

An additional note: Exploring the MariaDB JDBC driver, I found this inside the url parsing file:

附加说明:探索 MariaDB JDBC 驱动程序,我在 url 解析文件中找到了这个:

Project: https://github.com/MariaDB/mariadb-connector-j.git
File: src/main/java/org/mariadb/jdbc/UrlParser.java

public static UrlParser parse(final String url, Properties prop) throws SQLException {
....
        if (url.startsWith("jdbc:mysql:")) {
            UrlParser urlParser = new UrlParser();
            parseInternal(urlParser, url, prop);
            return urlParser;
        } else {
            if (url.startsWith("jdbc:mariadb:")) {
                UrlParser urlParser = new UrlParser();
                parseInternal(urlParser, "jdbc:mysql:" + url.substring(13), prop);
                return urlParser;
            }
        }    

As you can see, the string "jdbc:mariadb:"is always replaced with "jdbc:mysql:"internally. So when it comes to the MariaDB driver, whether it is :mariadb: or :mysql: it always gets parsed as "jdbc:mysql:".

如您所见,字符串"jdbc:mariadb:"始终在"jdbc:mysql:"内部替换。因此,当涉及到 MariaDB 驱动程序时,无论是 :mariadb: 还是 :mysql: 它总是被解析为"jdbc:mysql:".

No difference.

没有不同。

if (url.startsWith("jdbc:mariadb:")) {
    ....
    parseInternal(urlParser, "jdbc:mysql:" + url.substring(13), prop);
    ....