简体   繁体   English

如何使用Java获取MariaDB连接?

[英]How to get MariaDB connection using java?

I worked on mysql server and connected my java applications successfully. 我在mysql服务器上工作,并成功连接了Java应用程序。 And now I changed to MariaDB. 现在我换成了MariaDB。 How to connect with MariaDB server using java? 如何使用Java与MariaDB服务器连接? 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;
}

} }

Minor changes for MariaDB are as follows: Use the MariaDB Connector/J with the following Driver class: MariaDB的较小更改如下:将MariaDB Connector / J与以下Driver类一起使用:

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");

and the rest would work well since MySQL and MariaDB clients are compatible.After all, MariaDB is an enhanced, drop-in replacement for MySQL. 毕竟,MySQL和MariaDB客户端是兼容的,因此其余的都可以正常工作。毕竟,MariaDB是MySQL的增强的直接替代品。

More information about connecting to MariaDB using the Java Connector can be accessed from MariaDB Knowledge Base (MariaDB Connector/J 有关使用Java连接器连接到MariaDB的更多信息,可以从MariaDB知识库(MariaDB Connector / J

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM