美文网首页
Mariadb java操作(二) 得到数据库中的DDL信息

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

作者: ShootHzj | 来源:发表于2020-08-18 13:25 被阅读0次
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 *即为查询全部的字段

相关文章

网友评论

      本文标题:Mariadb java操作(二) 得到数据库中的DDL信息

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