请帮忙修复CCJSqlParser异常

时间:2015-02-05 04:15:20

标签: java jsqlparser

我使用CCJSqlParser来解析sql。我解决了致命的问题。

我的java代码如下

String sqlStr = "SELECT t1.Key as Key1,  t2.Key as Key2   FROM tabel1 as t1, tabel2 as t2 WHERE t1.Key = t2.Key";
Statement statement = CCJSqlParserUtil.parse(sqlStr);

如下例外:

  12:11:27,608         INFO ParseSqlUtil:29 - start to parse SQL: SELECT t1.Key as Key1,  t2.Key as Key2   FROM tabel1 as t1, tabel2 as t2 WHERE t1.Key = t2.Key
    net.sf.jsqlparser.JSQLParserException12:11:27,650         INFO SparkTest:347 - excetption:null

        at net.sf.jsqlparser.parser.CCJSqlParserUtil.parse(CCJSqlParserUtil.java:51)
        at com.spark.ml.query.ParseSqlUtil.parseSqlToSparkQueryDto(ParseSqlUtil.java:30)
        at com.spark.ml.query.SparkTest.main(SparkTest.java:342)
    Caused by: net.sf.jsqlparser.parser.ParseException: Encountered " "." ". "" at line 1, column 10.
    Was expecting one of:
        <EOF> 
        "AS" ...
        "DO" ...
        "INTO" ...
        "FROM" ...

1 个答案:

答案 0 :(得分:0)

使用JSqlParser V0.9.2,您的语句已成功解析。

String sqlStr = "SELECT t1.Key as Key1,  t2.Key as Key2   FROM tabel1 as t1, tabel2 as t2 WHERE t1.Key = t2.Key";
Statement statement = CCJSqlParserUtil.parse(sqlStr);   
System.out.println(statement.toString());

提供正确的输出:

SELECT t1.Key AS Key1, t2.Key AS Key2 FROM tabel1 AS t1, tabel2 AS t2 WHERE t1.Key = t2.Key

我认为问题是列名,这是JSqlParser的关键字。在最近的版本中,允许越来越多的关键字用于数据库对象名称。