从Linux上的NetBeans(Mageia)中的Java应用程序连接到MariaDB [英] Connect to MariaDB from Java application in NetBeans on Linux (Mageia)

查看:146
本文介绍了从Linux上的NetBeans(Mageia)中的Java应用程序连接到MariaDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

 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

我在互联网上寻求帮助,并且由MariaDB Client Library for Java Applications提供的驱动程序类不是com.mysql.jdbc.Driver但是org.mariadb.jdbc.Driver!我相应地改变了它,但似乎问题在于try块内的第一行。驱动程序根本没有加载。
另外,我已经将mysql jar文件添加到我的java应用程序的库中,如下面的屏幕截图所示。请帮我解决这个问题。

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.

推荐答案

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

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 driver that is being used (rather than the database server being accessed).

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

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

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

为我工作。我从这里下载了MariaDB Client Library for Java:

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

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

我是通过

https://downloads.mariadb.org/

附加说明:


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

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

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

The default configuration for MariaDB under Mageia may include the skip-networking directive 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-address value to something like 0.0.0.0 as well.)

这篇关于从Linux上的NetBeans(Mageia)中的Java应用程序连接到MariaDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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