记录:278

场景:在实际开发中,开发文档中的建表信息以表格的方式提供,包括字段名称、字段类型、字段注释、是否为空等。本例就是先把表格信息转换为约定格式的txt文件,在使用Java代码生成完整的建表语句。

版本:Spring Boot 2.6.3

一、案例场景

1.开发文档中以表格方式提供建表信息。

2.手动转换为txt文件,文件名:SENSOR_DATA.txt,格式约定如下:

第一行为,表名##表名称。

第二行开始,每行:字段名称##字段类型##字段非空##字段注释##字段主键或者索引。

SENSOR_DATA##传感器数据
ID##NUMBER(16)##Y##实体唯一标识##primary
REGION##VARCHAR2(8)##N##区域##index
VALUE1##NUMBER(16,2)##N##取值1##N
VALUE2##NUMBER(16,2)##N##取值2##N

3.执行代码,自动生成sql文件,文件名:SENSOR_DATA.sql,生成结果。

create table SENSOR_DATA ( ID  NUMBER(16) not null ,REGION  VARCHAR2(8),VALUE1  NUMBER(16,2),VALUE2  NUMBER(16,2));
comment on table  SENSOR_DATA is '传感器数据';
comment on column SENSOR_DATA.ID is '实体唯一标识';
comment on column SENSOR_DATA.REGION is '区域';
comment on column SENSOR_DATA.VALUE1 is '取值1';
comment on column SENSOR_DATA.VALUE2 is '取值2';
alter table  SENSOR_DATA add constraint PK_SENSOR_DATA_ID primary key (ID);
create index  IDX_SENSOR_DATA_REGION on SENSOR_DATA (REGION);

二、使用类

1.读文件操作

java.lang.AutoCloseable,接口。

java.io.Closeable,接口,继承AutoCloseable。

java.lang.Readable,接口。

java.io.Reader,抽象类,实现Readable接口和Closeable。

java.io.BufferedReader,实现类,实现Reader抽象类。

java.io.InputStreamReader,实现类,读输入流,Reader抽象类。

java.io.FileReader,实现类,读文件,继承InputStreamReader。

new BufferedReader(new FileReader(fileName))。

2.写文件操作

java.lang.AutoCloseable,接口。

java.io.Closeable,接口,继承AutoCloseable。

java.io.Flushable,接口。

java.lang.Appendable,接口。

java.io.Writer,抽象类,实现Appendable, Closeable, Flushable。

java.io.BufferedWriter,实现类,实现Writer抽象类。

java.io.OutputStreamWriter,实现类,写输出流,Writer抽象类。

java.io.FileWriter,实现类,写文件,继承OutputStreamWriter。

new BufferedWriter(new FileWriter(fileName, true));

3.操作ArrayList

java.lang.Iterable,接口。

java.util.Collection,接口,继承Iterable。

java.util.List,接口,继承Collection。

java.util.AbstractCollection,抽象类,实现Collection。

java.util.AbstractList,抽象类,继承AbstractCollection,实现List。

java.util.ArrayList,一个Resizable-array。

4.Collections

java.util.Collections,对集合相关操作。

三、代码

1.读文件

读取文件,逐行读取,每读取一行,立即解析,根据分隔符##,分割成多个String字符串,存放在一个List<String>中。解析完成的一行数据List<String>,再放入List<List>中。

public static ArrayList<ArrayList<String>> readFromTxt(String fileName) {ArrayList<ArrayList<String>> listAll = new ArrayList<>();try {//1.读txt文件,一次读一行BufferedReader br = new BufferedReader(new FileReader(fileName));String oneLine = null;//2.使用readLine方法,一次读一行while ((oneLine = br.readLine()) != null) {ArrayList<String> oneLineList = getOneLine(oneLine, "##");listAll.add(oneLineList);}br.close();} catch (Exception e) {System.out.println("读异常.");e.printStackTrace();}return listAll;
}
public static ArrayList<String> getOneLine(String content, String split) {String[] strArr = content.split(split);ArrayList<String> oneLine = new ArrayList<>(strArr.length);Collections.addAll(oneLine, strArr);return oneLine;
}

2.写文件

逐行写文件,以追加方式写入,不覆盖已经写入的内容。

public static void writeFile(String fileName, String content) {try {// 设置为追加写入trueBufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true));bw.write(content);bw.close();} catch (IOException e) {System.out.println("读异常.");e.printStackTrace();}
}

3.生成建表语句

根据从txt读取的表字段信息,生成建表语句。

public static void createTable(String fileName, ArrayList<ArrayList<String>> content) {List<String> firstOne = content.get(0);String tableName = firstOne.get(0);String part1 = "create table ";String part2 = tableName;String part3 = " ( ";writeFile(fileName, part1 + part2 + part3 + "\n");String part5 = " );";int size = content.size();for (int i = 0; i < size; i++) {ArrayList<String> oneLine = content.get(i);if (StringUtils.equals(tableName, oneLine.get(0))) continue;String line = "";int last = content.size() - 1;if (StringUtils.equals("Y", oneLine.get(2))) {line = oneLine.get(0) + "  " + oneLine.get(1) + " not null " + "," + "\n";} else if (i == last) {line = oneLine.get(0) + "  " + oneLine.get(1) + "\n";} else {line = oneLine.get(0) + "  " + oneLine.get(1) + "," + "\n";}writeFile(fileName, "  " + line);}writeFile(fileName, part5 + "\n");
}

4.生成字段注释

根据从txt读取的表字段信息,生成字段注释。

public static void createComment(String fileName, ArrayList<ArrayList<String>> content) {List<String> firstOne = content.get(0);String tableName = firstOne.get(0);String tableComment = firstOne.get(1);String part1 = "comment on table  ";String part2 = tableName;String part3 = " is '";String part4 = tableComment;String part5 = "';";String tableComments = part1 + part2 + part3 + part4 + part5;writeFile(fileName, tableComments + "\n");int size = content.size();for (int i = 0; i < size; i++) {ArrayList<String> oneLine = content.get(i);if (StringUtils.equals(tableName, oneLine.get(0))) continue;String part01 = "comment on column ";String part02 = tableName + ".";String part03 = oneLine.get(0);String part04 = " is '";String part05 = oneLine.get(3) + "';\n";String part = part01 + part02 + part03 + part04 + part05;writeFile(fileName, part);}
}

5.生成主键约束和索引

根据从txt读取的表字段信息,生成主键约束和索引。

// 生成主键和索引
public static void createPrimaryAndIndex(String fileName, ArrayList<ArrayList<String>> content) {List<String> firstOne = content.get(0);String tableName = firstOne.get(0);int size = content.size();for (int i = 0; i < size; i++) {ArrayList<String> oneLine = content.get(i);if (StringUtils.equals(tableName, oneLine.get(0))) continue;if (StringUtils.equals("primary", oneLine.get(4).toLowerCase())) {String part1 = "alter table  ";String part2 = tableName;String part3 = " add constraint ";String part4 = "PK_" + tableName + "_" + oneLine.get(0);String part5 = " primary key (" + oneLine.get(0) + ");";part4 = getLimitString(part4);String pk = part1 + part2 + part3 + part4 + part5;writeFile(fileName, pk + "\n");}if (StringUtils.equals("index", oneLine.get(4).toLowerCase())) {String part1 = "create index  ";String part2 = "IDX_" + tableName + "_" + oneLine.get(0);String part3 = " on ";String part4 = tableName;String part5 = " (" + oneLine.get(0) + ");";part2 = getLimitString(part2);String index = part1 + part2 + part3 + part4 + part5;writeFile(fileName, index + "\n");}}
}
// 限定主键或索引名称字符串30个字符
public static String getLimitString(String srcStr){int length = srcStr.length();if (length > 30) {srcStr = srcStr.substring(0, 30);String underline = srcStr.substring(29, 30);if (StringUtils.equals("_", underline)) {// 最后一个字符是字母而不是下划线srcStr = srcStr.substring(0, 29);}}return srcStr;
}

6.在main函数调用

在main函数调用测试。

public static void main(String[] args) {System.out.println("开始...");String baseDir = "D:\\example\\";String fileName = "SENSOR_DATA";String txtFileName = baseDir + fileName + ".txt";String sqlFileName = baseDir + fileName + ".sql";ArrayList<ArrayList<String>> read = readFromTxt(txtFileName);// 表结构createTable(sqlFileName, read);// 注释createComment(sqlFileName, read);// 主键和索引(单个字段)createPrimaryAndIndex(sqlFileName, read);System.out.println("结束...");
}

以上,感谢。

2022年7月1日

使用Java根据约定格式生成Oracle建表语句相关推荐

  1. oracle建表语句string,编程式Mybatis获取oracle表创建表语句

    写在前面 Mybatis之前使用过一段时间,当然只是粗糙地使用,而且配合Spring有一部分Mybatis管理的内容都交给了Spring,加上一段时间没用到又开始忘了,刚好今天碰到一个要求,Oracl ...

  2. python读取excel生成mysql建表语句_python读取excel文件并自动在mysql中建表导数据

    """ 根据excel在mysql中建表(表名为文件名,字段为csv中的header,默认所有字段为varchar,如需更改,在数据库中更改即可),并插入数据 " ...

  3. Excel自动生成sql建表语句

    ETL开发中重新整理很多表结构,写建表语句又是一件头疼的事情,干脆借鉴网友的代码,自己写了一个自动生成建表sql的模板. 模板下载:http://download.csdn.net/detail/en ...

  4. 1.python 根据 oracle字段名和类型 生成 hive建表语句

    一.问题 遇到一个场景,要根据oracle表结构创建hive表. 本来想写一个根据oracle信息和表名自动在hive中创建表的脚本.如果在集群运行,python操作oracle的cx_Oracle在 ...

  5. Quartz的oracle建表语句

    建表语句 其中schedule_job是手动新增的,用于界面增删改查 CREATE TABLE schedule_job (id int NOT NULL,job_id VARCHAR2(255) D ...

  6. mysql json建表_json数据自动生成MySQL建表语句

    #_*_coding:utf-8_*_ #author: cike #date: 18-9-13 import pymysql.cursors connection = pymysql.connect ...

  7. 使用oracle数据库建表语句,怎么使用sql查询oracle建表语句

    SELECT CREATETION AS "年份" ,SUM(DATE_ROW01) AS "01月" ,SUM(DATE_ROW02) AS "02 ...

  8. 使用java代码编写脚本,把oracle建表语句变成hive建表语句

    使用java代码编写脚本,把oracle建表语句变成hive建表语句 java代码 测试oracle.sql 生成hive创表语句 java代码 import java.io.File; import ...

  9. 使用java通过固定的excel模板自动生成数据库表的ddl建表语句

    有时候要建很多表或一个表有很多字段,一个个复制字段弄太麻烦了,为了提高点工作效率,写了个小工具通过固定的excel模板自动生成基础的ddl建表语句 maven依赖 <!--核心jar包--> ...

  10. oracle 创建表空间 pcincrease,oracle建表空间 各种语句

    oracle建表空间 各种语句 在创建用户之前,先要创建表空间: 其格式为:格式: create tablespace 表间名 datafile '数据文件名' size 表空间大小; 如: SQL& ...

最新文章

  1. iOS开发:沙盒机制以及利用沙盒存储字符串、数组、字典等数据
  2. 剑指offer:顺时针打印矩阵
  3. Flutter 插件url_launcher简介及使用,打开外部浏览器,拨打电话,发送短信,打开微信,支付宝,qq等
  4. C++ 枚举类型详解
  5. jQuery 基础学习笔记
  6. SAP IBASE hierarchy remove - debug in ERP step1
  7. linux sed删除指定行_shell三剑客之sed!
  8. 超算和服务器性能,烽火GPU高性能服务器助力武大超算中心建设
  9. oracle中长字符串长度吗,Oracle 字符串长度函数
  10. 软件测试——bug相关知识
  11. RSA 非对称加密之 PKCS8 格式秘钥
  12. LeetCode | 521. Longest Uncommon Subsequence I
  13. Computer Viruses and Malware (Advances in Information Security)
  14. 莮亾芣湜唥洫,莮亾竾浍哭
  15. 排列组合_均匀分组和部分均匀分组的计算与示例/二项式定理二项式系数/求和中的对称性/莱布尼兹公式与推广的求导法则
  16. 数字温湿度传感器DHT11
  17. Hiho303周 H国的身份证号码I
  18. 你非要躺平,我也没办法啊,逼着学吗
  19. MPB:中农戴兆来组-​猪肠道微生物样品的采集与核酸提取
  20. Neo4j详细介绍及使用教程

热门文章

  1. QAM调制原理_广播百科 QAM
  2. 大数据十大核心原理(互联网上整理)
  3. HP DL360 G6 升级BIOS
  4. jdk 6u45 下载地址
  5. Unix网络编程-同步
  6. UNIX网络编程卷1:套接字联网API(第3版).pdf
  7. Alexa工具条遭清洗,其排名逐渐淡出中国(转)
  8. 教你如何找到线程插入式木马
  9. Dreamweaver网页课设做家乡网站
  10. Linux vi命令修改文件内容笔记