当前位置:网站首页>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;
}边栏推荐
- Mr. Du built a domestic non garlic Statistics Platform
- Cmake tutorial series-00-introduction
- Wechat applet avatar pendant production
- SAP ABAP 对话框编程教程:中的模块池-09
- 【山大会议】WebRTC基础之对等体连接
- 使用 zipfile、openpyxl、flask 批量导出excel zip
- 实现一个Container全局组件步骤(给还不会使用组件的新手一个思路,大佬绕道)
- LeetCode_回溯_动态规划_中等_131.分割回文串
- Rosbag use command
- How to embody the value of knowledge management in business
猜你喜欢
随机推荐
SAP价值流程&帮助请求流程-011
Gbase "library" special training of innovation and application Committee of Beijing fintech Industry Alliance
SAP ABAP 数据字典教程 SE11:表、锁定对象、视图和结构 -03
Cross border integration, creativity and innovation to help improve the influence of cultural tourism night tour
ironSource Luna 推出苹果搜索广告限时优惠,注册即享3个月免费服务
SAP web service 无法使用 SOAMANAGER 登陆到SOA管理页面
Maze problem (BFS record path)
Deploy odoo to the server and configure it as a service
[Shanda conference] use typescript to reconstruct the project
SAP ABAP 子屏幕教程:在 SAP 中调用子屏幕-010
Linux安装mysql
默认函数控制 =default 与 =delete
[VTK] model rotation and Translation
期货怎么开户?网上期货开户安全吗?
Make the text template in pycharm project support jinjia2 syntax
5.文件的读写(学生类)
SAP ABAP BAPI-016
Unity game optimization (version 2) learning record 8
C语言贪吃蛇
Huawei cloud hcdez special session and Distributed Technology Summit: Huawei cloud distributed cloud native technology and Practice







![[译文] 弥合开源数据库和数据库业务之间的鸿沟](/img/e5/f89a8f3e2e9034f557ea3e76f37f81.jpg)

