美文网首页
Mariadb java操作(一) 安装

Mariadb java操作(一) 安装

作者: hezhangjian | 来源:发表于2020-08-09 06:02 被阅读0次

下载依赖包

yum install -y mariadb-server

db初始化

mysql_install_db --gssapi=OFF --user=root

命令拉起

初始化SQL

CREATE DATABASE IF NOT EXISTS ttbb DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_bin;
CREATE USER IF NOT EXISTS hzj identified by 'Mysql@123';
GRANT ALL ON ttbb .* to hzj@'%';
/usr/bin/mysqld_safe --datadir='/var/lib/mysql' --user=root --init-file=/opt/sh/init.sql

代码连接

代码,参考地址 https://github.com/Shoothzj/java-demo

package com.github.shoothzj.demo.db.jdbc.mariadb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author akka
 */
public class MariaDBBegin {

    // The JDBC Connector Class.
    private static final String DRIVER_NAME = "org.mariadb.jdbc.Driver";

    private static final String CONNECTION = "jdbc:mariadb://localhost:3306/" + TestConstant.TABLE_NAME;

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        System.out.println(DRIVER_NAME);
        Class.forName(DRIVER_NAME);
        // Properties for user and password.
        Properties p = new Properties();
        p.put("user", TestConstant.USER_NAME);
        p.put("password", TestConstant.PASSWORD);
        // Now try to connect
        Connection c = DriverManager.getConnection(CONNECTION, p);
        System.out.println("It works !");
        c.close();
    }

}

输出

org.mariadb.jdbc.Driver
It works !

相关文章

网友评论

      本文标题:Mariadb java操作(一) 安装

      本文链接:https://www.haomeiwen.com/subject/hlakdktx.html