如何使用java获取MariaDB连接? [英] How to get MariaDB connection using java?

查看:472
本文介绍了如何使用java获取MariaDB连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mysql服务器上工作并成功连接了我的java应用程序。现在我改为MariaDB。如何使用java连接MariaDB服务器?
如何更改?

I worked on mysql server and connected my java applications successfully. And now I changed to MariaDB. How to connect with MariaDB server using java? How this should be changed?

public class DBConnection {

private Connection connection;
private static DBConnection dBConnection;

public DBConnection() throws ClassNotFoundException, SQLException {
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "root", "mysql");
}

public static DBConnection getDBConnection() throws ClassNotFoundException, SQLException {
    if (dBConnection == null) {
        dBConnection = new DBConnection();
    }
    return dBConnection;
}

public Connection getConnection() {
    return connection;
}

}

推荐答案

MariaDB的细微更改如下:使用带有以下驱动程序类的MariaDB Connector / J:

Minor changes for MariaDB are as follows: Use the MariaDB Connector/J with the following Driver class:

org.mariadb.jdbc.Driver

对于数据库连接,请使用以下结构:

For DB Connection use the following structure:

jdbc:(mysql|mariadb)://host:port/[database]

因此,您的上述代码只需要更改

Therefore, your code as above would only require the change for

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

其余的工作正常,因为MySQL和MariaDB客户端兼容。毕竟,MariaDB是一个增强版,替代MySQL。

and the rest would work well since MySQL and MariaDB clients are compatible.After all, MariaDB is an enhanced, drop-in replacement for MySQL.

有关使用Java Connector连接MariaDB的更多信息,请访问 MariaDB知识库(MariaDB Connector / J

More information about connecting to MariaDB using the Java Connector can be accessed from MariaDB Knowledge Base (MariaDB Connector/J

这篇关于如何使用java获取MariaDB连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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