首先到官网下载一下java驱动
https://mariadb.com/downloads/mariadb-tx/connector将驱动buildpath进项目中
具体代码
import java.sql.*;
public class DBTest {
public static void main(String[] args) {
try {
String driver ="org.mariadb.jdbc.Driver";
//从配置参数中获取数据库url
String url = "jdbc:mariadb://localhost:3306/test";
//从配置参数中获取用户名
String user = "root";
//从配置参数中获取密码
String pass = "";
//注册驱动
Class.forName(driver);
//获取数据库连接
Connection conn = DriverManager.getConnection(url,user,pass);
//创建Statement对象
Statement stmt = conn.createStatement();
System.out.println("Success!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}