java 连接mysql mariadb,如何使用java获取MariaDB连接?

本文介绍了如何将Java应用程序从连接MySQL切换到连接MariaDB。主要变化在于使用MariaDB Connector/J,并将Driver类改为'org.mariadb.jdbc.Driver'。其余的数据库连接代码保持不变,因为MySQL和MariaDB客户端兼容。MariaDB是MySQL的一个增强版,可以无缝替代。
摘要由CSDN通过智能技术生成

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;

}

}

解决方案

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>