Mariadb java操作(二) 得到数据库中的DDL信息

举报
张俭 发表于 2023/12/29 17:31:56 2023/12/29
【摘要】 package com.github.shoothzj.demo.db.jdbc.mariadb;import com.github.shoothzj.demo.base.mariadb.module.FieldDescribe;import com.github.shoothzj.demo.base.util.LogUtil;import lombok.extern.slf4j.Slf4j...
package com.github.shoothzj.demo.db.jdbc.mariadb;

import com.github.shoothzj.demo.base.mariadb.module.FieldDescribe;
import com.github.shoothzj.demo.base.util.LogUtil;
import lombok.extern.slf4j.Slf4j;

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

/**
 * @author hezhangjian
 */
@Slf4j
public class MariadbDescribeTable {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        LogUtil.configureLog();
        Class.forName("org.mariadb.jdbc.Driver");
        // Now try to connect
        Properties p = new Properties();
        p.put("user", "hzj");
        p.put("password", "Mysql@123");
        try (Connection c = DriverManager.getConnection("jdbc:mariadb://localhost:3306/ttbb", p)) {
            {
                PreparedStatement preparedStatement = c.prepareStatement("CREATE TABLE XX(\n" +
                        "  id VARCHAR(50) PRIMARY KEY,\n" +
                        "  male BOOLEAN,\n" +
                        "  weight DOUBLE,\n" +
                        "  age INT,\n" +
                        "  age2 INTEGER\n" +
                        ")");
                preparedStatement.execute();
            }
            {
                PreparedStatement statement = c.prepareStatement("SELECT * FROM XX LIMIT 1");
                ResultSetMetaData metaData = statement.getMetaData();
                int size = metaData.getColumnCount();
                for (int i = 1; i <= size; i++) {
                    FieldDescribe fieldDescribe = new FieldDescribe();
                    fieldDescribe.setColumnType(metaData.getColumnType(i));
                    fieldDescribe.setColumnTypeName(metaData.getColumnTypeName(i));
                    fieldDescribe.setColumnDisplaySize(metaData.getColumnDisplaySize(i));
                    fieldDescribe.setColumnLabel(metaData.getColumnLabel(i));
                    log.info("[{}]", fieldDescribe);
                }
            }
            {
                PreparedStatement preparedStatement = c.prepareStatement("DROP TABLE XX");
                preparedStatement.execute();
            }
        }
    }

}

输出结果

13:22:38.263 [main] INFO  com.github.shoothzj.demo.db.jdbc.mariadb.MariadbDescribeTable - [FieldDescribe(columnType=12, columnTypeName=VARCHAR, columnDisplaySize=50, columnLabel=id)]
13:22:38.266 [main] INFO  com.github.shoothzj.demo.db.jdbc.mariadb.MariadbDescribeTable - [FieldDescribe(columnType=-7, columnTypeName=TINYINT, columnDisplaySize=1, columnLabel=male)]
13:22:38.266 [main] INFO  com.github.shoothzj.demo.db.jdbc.mariadb.MariadbDescribeTable - [FieldDescribe(columnType=8, columnTypeName=DOUBLE, columnDisplaySize=22, columnLabel=weight)]
13:22:38.266 [main] INFO  com.github.shoothzj.demo.db.jdbc.mariadb.MariadbDescribeTable - [FieldDescribe(columnType=4, columnTypeName=INTEGER, columnDisplaySize=11, columnLabel=age)]
13:22:38.266 [main] INFO  com.github.shoothzj.demo.db.jdbc.mariadb.MariadbDescribeTable - [FieldDescribe(columnType=4, columnTypeName=INTEGER, columnDisplaySize=11, columnLabel=age2)]

备注:
你可以在SELECT语句中指定你需要的字段,SELECT *即为查询全部的字段

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。