Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate sql query syntax?

java 1.4 Sql server 2000

i am taking input of sql query (for validation of field value against values retrieved by executing sql query) from admin user which will be stored in database and later i will executing sql query corresponding to field.Before inserting sql query in database i want to validate its syntax in java code.

Fields         Sql Query

stateCode      select statecode from states
district code  select district code from districts
like image 798
Maddy.Shik Avatar asked Oct 21 '09 08:10

Maddy.Shik


2 Answers

Create a PreparedStatement with the query string; if this works, the query string is ok (but nothing is executed yet)

like image 157
Erich Kitzmueller Avatar answered Sep 19 '22 19:09

Erich Kitzmueller


dont think there is any (easy) way to validate sql

Sql syntax is complex and allows for alot of different ways to enter a statement.

Think you best shot would be to just execute the sql statent and if you have a SQl exception see if its a bad syntax thats causing it.

you can prepend some sql to avoid from actually executing the query

in sybase it would be SET NOEXEC ON

like image 23
Peter Avatar answered Sep 19 '22 19:09

Peter