使用Jsqlparser解析表字段名,字段类型,字段属性

public static void main( String[] args ) throws JSQLParserException, ParseException {
String sql =“CREATE TABLE company (comid int(10) unsigned NOT NULL AUTO_INCREMENT,name varchar(45) NOT NULL,organizationcode varchar(45) DEFAULT NULL,businesscode varchar(45) DEFAULT NULL,taxcode varchar(45) DEFAULT NULL,telephone varchar(45) DEFAULT NULL,comcode int(11) NOT NULL,PRIMARY KEY (comid),UNIQUE KEY comid_UNIQUE (comid),UNIQUE KEY comcode_UNIQUE (comcode) USING BTREE) ENGINE=InnoDB AUTO_INCREMENT=1004 DEFAULT CHARSET=utf8”;
int i;
if(sql.contains(“USING BTREE”)){
sql =sql.replaceAll(“USING BTREE”,"");
}
List tablename = getTableNameBySql(sql);
for (i = 0;i System.out.println(tablename.get(i).getColumnName() +" : “+tablename.get(i).getColDataType()+” : "+tablename.get(i).getColumnSpecStrings());
}
}
public static List getTableNameBySql(String sql) throws JSQLParserException {
CCJSqlParserManager parser=new CCJSqlParserManager();
List columnList = null;
Statement stmt=parser.parse(new StringReader(sql));
if (stmt instanceof CreateTable) {
columnList = ((CreateTable) stmt).getColumnDefinitions();
}
return columnList;
}

你可能感兴趣的:(JAVA)