我是第一次使用 JSQLPARSER。我有一些动态出现的 SQL 文件,我需要从该 SQL 中读取表名和列名。经过大量谷歌搜索后,我尝试使用 JSQLPARSER。我正在尝试从文件中读取列名,但由于表达式而无法读取列名,请任何人纠正我出错的代码。我得到 CLASSCASTEXCEPTION 代码:
public static void main(String[] args) throws JSQLParserException
{
// TODO Auto-generated method stub
String statement="SELECT LOCATION_D.REGION_NAME, LOCATION_D.AREA_NAME, COUNT(DISTINCT INCIDENT_FACT.TICKET_ID) FROM LOCATION_D, INCIDENT_FACT WHERE ( LOCATION_D.LOCATION_SK=INCIDENT_FACT.LOCATION_SK ) GROUP BY LOCATION_D.REGION_NAME, LOCATION_D.AREA_NAME";
CCJSqlParserManager parserManager = new CCJSqlParserManager();
Select select=(Select) parserManager.parse(new StringReader(statement));
PlainSelect plain=(PlainSelect)select.getSelectBody();
List selectitems=plain.getSelectItems();
System.out.println(selectitems.size());
for(int i=0;i<selectitems.size();i++)
{
Expression expression=((SelectExpressionItem) selectitems.get(i)).getExpression();
System.out.println("Expression:-"+expression);
Column col=(Column)expression;
System.out.println(col.getTable()+","+col.getColumnName());
}
}