当前位置:网站首页>sql语法检测
sql语法检测
2022-06-22 15:08:00 【为爱停留】
maven配置:
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>4.2</version>
</dependency>用 jsqlparser可以限定输入哪种DML sql语句,如只限定select查询语句,用执行计划可以校验sql语法错误,如下:
public SqlCheckRspVo sqlCheck(SqlCheckReqVo sqlCheckReqVo) {
SqlCheckRspVo sqlCheckRspVo = new SqlCheckRspVo();
if(!sqlCheckReqVo.getSql().contains("$")){
return new SqlCheckRspVo(false,"sql中没有添加$");
}
sqlCheckReqVo.setSql(sqlCheckReqVo.getSql().trim());
if(sqlCheckReqVo.getSql().endsWith(";")){
sqlCheckReqVo.setSql(sqlCheckReqVo.getSql().substring(0,sqlCheckReqVo.getSql().length()-1));
}
for(AddReqVo.TableInfo tableInfo : sqlCheckReqVo.getRuleTables()) {
String sql = sqlCheckReqVo.getSql().replace("$",tableInfo.getTableName());
sqlCheckRspVo = sqlparse(sql);
if(sqlCheckRspVo.getCheckResult() == false){
return sqlCheckRspVo;
}
SqlSession sqlSession = null;
PreparedStatement pst ;
try {
sqlSession = getNativeSqlSession();
Connection dbCon = sqlSession.getConnection();
pst = dbCon.prepareStatement("explain plan for " +sql);
pst.executeUpdate();
pst.close();
} catch (SQLException e) {
sqlCheckRspVo.setCheckResult(false);
sqlCheckRspVo.setErrorLog(ExceptionUtils.getStackTrace(e));
return sqlCheckRspVo;
} finally {
if (null != sqlSession) {
closeNativeSqlSession(sqlSession);
}
}
}
return sqlCheckRspVo;
}
public SqlCheckRspVo sqlparse(String sqlContent){
SqlCheckRspVo sqlCheckRspVo = new SqlCheckRspVo();
try {
Statement parse = CCJSqlParserUtil.parse(sqlContent);
if(parse instanceof Select){
sqlCheckRspVo.setCheckResult(true);
}else{
sqlCheckRspVo.setCheckResult(false);
sqlCheckRspVo.setErrorLog("只能输入select语句");
}
}catch (Exception e){
sqlCheckRspVo.setCheckResult(false);
sqlCheckRspVo.setErrorLog(ExceptionUtils.getStackTrace(e));
}
return sqlCheckRspVo;
}边栏推荐
- Adding an unknown type of MCU to jflash
- Odoo local document function development record
- webDriver以及Selenium使用总结
- stack和queue的模拟实现
- SAP ABAP 中的 Smart Forms-014
- 84. (cesium chapter) movement of cesium model on terrain
- 使用 zipfile、openpyxl、flask 批量导出excel zip
- 数睿数据荣获第二届ISIG中国产业智能大会两项年度大奖
- ORB_ VI ideological framework
- odoo部署到服务器并配置为服务
猜你喜欢

wallys/WiFi6 MiniPCIe Module 2T2R 2×2.4GHz 2x5GHz

SAP ABAP 中的 Smart Forms-014

SAP ABAP BAPI-016

着力打造网红产品,新捷途X70S焕新上市,8.79万起售

Focus on creating a net red product. The xinjietu x70s is newly launched, starting from 87900

直播无顶流:董宇辉这么火,还有人看刘畊宏吗?

Research on ICT: domestic databases focus on the ICT market, and Huawei Gauss is expected to become the strongest

Conversion between numeric types and strings

84.(cesium篇)cesium模型在地形上运动

什么是 SAP ABAP? 类型、ABAP 完整形式和含义
随机推荐
[Shanda conference] peer connection based on webrtc
信创研究:国产数据库聚焦信创市场,华为Gauss有望成为最强
New design of databend SQL planner
默认函数控制 =default 与 =delete
【华为云至简致远】征文获奖名单出炉!
[single chip microcomputer] [make buzzer sound] know the buzzer and let it make the sound you want
[Shanda conference] software performance optimization and bug repair
Dear students, don't read the textbooks any more. Just read this one for the complexity of time
数睿数据受邀参与南通企业数字化转型研讨会
数值类型和字符串之间的转换
畅享高性能计算!天翼云HPC解决方案来了
5.文件的读写(学生类)
用递归法求Fibonacci数列第n项的值
SAP ABAP 表控制与示例-07
着力打造网红产品,新捷途X70S焕新上市,8.79万起售
pymssql模块使用指南
【山大会议】软件性能优化及bug修复
Mysql触发器
[leetcode] 9. Palindromes
Runtime -- explore the nature of classes, objects, and classifications