maven配置:
<dependency>
<groupId></groupId>
<artifactId>jsqlparser</artifactId>
<version>4.2</version>
</dependency>
用 jsqlparser可以限定输入哪种DML sql语句,如只限定select查询语句,用执行计划可以校验sql语法错误,如下:
public SqlCheckRspVo sqlCheck(SqlCheckReqVo sqlCheckReqVo) {
SqlCheckRspVo sqlCheckRspVo = new SqlCheckRspVo();
if(!().contains("$")){
return new SqlCheckRspVo(false,"sql中没有添加$");
}
(().trim());
if(().endsWith(";")){
(().substring(0,().length()-1));
}
for( tableInfo : ()) {
String sql = ().replace("$",());
sqlCheckRspVo = sqlparse(sql);
if(() == false){
return sqlCheckRspVo;
}
SqlSession sqlSession = null;
PreparedStatement pst ;
try {
sqlSession = getNativeSqlSession();
Connection dbCon = ();
pst = ("explain plan for " +sql);
();
();
} catch (SQLException e) {
(false);
((e));
return sqlCheckRspVo;
} finally {
if (null != sqlSession) {
closeNativeSqlSession(sqlSession);
}
}
}
return sqlCheckRspVo;
}
public SqlCheckRspVo sqlparse(String sqlContent){
SqlCheckRspVo sqlCheckRspVo = new SqlCheckRspVo();
try {
Statement parse = (sqlContent);
if(parse instanceof Select){
(true);
}else{
(false);
("只能输入select语句");
}
}catch (Exception e){
(false);
((e));
}
return sqlCheckRspVo;
}