0

I have to evaluate a SQL file for correct syntax using scanner class in java. How is it possible? The steps i followed so far:

File text = new File("C:/Users/Desktop/sample.sql");
Scanner scnr = new Scanner(text);
while(scnr.hasNextLine()){
    String line = scnr.nextLine();
}
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Possible duplicate of [How to validate](http://stackoverflow.com/questions/1599547/how-to-validate-sql-query-syntax) [sql syntax](http://stackoverflow.com/questions/141499/any-java-libraries-out-there-that-validate-sql-syntax) – azurefrog Apr 26 '14 at 07:10
  • not so.. i want to evaluate sql syntax only using scanner class in java.. – user3575347 Apr 26 '14 at 07:19
  • 3
    It doesn't matter what you use to read the sql file. Your question still boils down to "how do I parse a SQL statement". – azurefrog Apr 26 '14 at 07:20
  • 1
    I'd be surprised if you can do it effectively with the Scanner class. I believe you will need to either hand-write or generate a proper lexical analyser and parser. [Javacc](https://javacc.java.net/) comes with an SQL grammer and lexical definition. You should really start with that. – user207421 Apr 26 '14 at 07:34
  • u mean using javacc i can achieve this? – user3575347 Apr 26 '14 at 07:42
  • 1
    What's the use case? Even when the SQL is syntactically valid, there there is no guarantee that it will execute successfully when run against a database. It still may contain semantical errors like references to non existing tables or columns. – Henry Apr 26 '14 at 08:59
  • its like developing a tool in java to validate the sql file based on certain standards – user3575347 Apr 26 '14 at 09:31
  • @user3575347 That's what I said. You should start with `javacc.` But the database already implements those standards, so you already have a tool for validation, and unless you are a professional compiler writer, the tool you write almost certainly won't implement those standards correctly, so all you will have is a tool that passes some invalid SQL, fails some valid SQL, and in short is entirely unfit for purpose. It's clear that whoever has given you this assignment radically underestimates its complexity, and doesn't know what a correct solution would look like. – user207421 Apr 26 '14 at 10:29

0 Answers0