Collect data base meta data by JDBC

本文介绍如何使用Java从SQL Server中收集存储过程(Sproc)的元数据,并列出所需的依赖库,包括Apache Commons DBCP2、Microsoft SQL JDBC驱动等。文章还提到XStream在XML支持方面的应用,并推荐了Freemarker等模型生成工具。

I have not to update the blog for a long time, it seems hard to hold every day敲打

I will introduce how to collect data base meta data for Sproc based on SQL Server


The dependent jar libs as below:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
    <groupId>com.microsoft</groupId>
    <artifactId>sqljdbc</artifactId>
</dependency>
<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>RELEASE</version>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>19.0</version>
</dependency>

BTW, the XStream is good for XML support.

1), Initialized sproc setting for sproc XML(custom)

2), Create connection for SQL Server.

3), Use DatabaseMetaData object to get sproc meta data

4), Parse data list to your object.



and we can use third plugin(model tool) to auto generate Object of my expected, such as

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.23</version>
</dependency>	

The referenced link:

For XStream: http://x-stream.github.io/javadoc/index.html

For DataBaseMetaData API: https://docs.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html 

For Sql Type convertion: https://docs.oracle.com/javase/7/docs/api/constant-values.html, we can use ENUM type to convert


优化这个方法:public void createBgmByData(Long expId,String vol, List<String> bgms, List<JSONObject> meta) { if(CollUtil.isEmpty(bgms) || CollUtil.isEmpty(meta)){ return; } GisExpRecord exp = expRecordMapper.selectById(expId); String rootDir = configService.selectConfigByKey("gis.file.basePath"); final String filePath = BASE_PATH + expId + "/"; List<GisBasicBgm> bgmList = basicBgmMapper.selectList(Wrappers.<GisBasicBgm>lambdaQuery().in(GisBasicBgm::getName, bgms)); for (GisBasicBgm bgm : bgmList){ List<List<String>> data = new ArrayList<>(); data.add(Arrays.asList("相对时间","绝对时间","指标值")); for(JSONObject m : meta){ data.add(Arrays.asList("",m.getStr("time"),m.getStr("血糖值("+bgm.getName()+")"))); } // 添加记录 String newName = IdUtil.fastSimpleUUID() + ".xlsx"; IndicatorUploadBo dbo = new IndicatorUploadBo(); dbo.setIndicator("血糖"); dbo.setCollectType("FB"); dbo.setCategory("原始"); dbo.setExpId(expId); dbo.setPosition("左侧"); dbo.setBgmId(bgm.getId()); String name = StrUtil.format("{}_{}_BGM数据.xlsx", DateUtil.formatDate(exp.getExpTime()).replaceAll("-",""), vol ); addByPath(dbo,rootDir,filePath,name,newName); // 生成文件 ExcelWriter writer = ExcelUtil.getWriter(rootDir + filePath + newName); writer.write(data); writer.flush(); writer.flush(); } if(StrUtil.isNotEmpty(meta.get(0).getStr("糖化血糖蛋白"))){ List<List<String>> data = new ArrayList<>(); data.add(Arrays.asList("相对时间","绝对时间","指标值")); data.add(Arrays.asList("",meta.get(0).getStr("time"),meta.get(0).getStr("糖化血糖蛋白"))); // 添加记录 String newName = IdUtil.fastSimpleUUID() + ".xlsx"; IndicatorUploadBo dbo = new IndicatorUploadBo(); dbo.setIndicator("糖化血红蛋白"); dbo.setCollectType("VB"); dbo.setCategory("原始"); dbo.setExpId(expId); dbo.setPosition("左侧"); String name = StrUtil.format("{}_{}_糖化血红蛋白.xlsx", DateUtil.formatDate(exp.getExpTime()).replaceAll("-",""), vol ); addByPath(dbo,rootDir,filePath,name,newName); // 生成文件 ExcelWriter writer = ExcelUtil.getWriter(rootDir + filePath + newName); writer.write(data); writer.flush(); writer.flush(); } // 更新实验数量 refreshExpCount(expId); // 更新空腹血糖 syncEsConc(expId); }
10-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值