Using regular expressions to validate mysql statements

I am writing a program in java. In the dialog box, the user must enter the MySQL SELECT statement. The program must confirm that the operator continues to work. My question is: is there a way and how to test a statement using regular expressions. I need a "only" regex pattern. Thank.

+4
source share
4 answers

Well, maybe for an extended regular expression, but for the original value, "regular expression", which means "Regular Expression," for "Normal Language," "no.

Each SELECT statement:

SELECT x FROM y WHERE z

, y SELECT, , , , , .

+3

, SELECT, , . SQLException.

, . , JDBC allowMultiQueries=true, , SELECT * FROM table; DROP TABLE table;.

+3

.

An elegant way to solve this problem is to use BNF for Java as a validator for your select statements. If you feed this SQL grammar like this sql-99.bnf , you will have your statement validator as soon as possible and free of charge.

+2
source

If it is a SELECT statement, it should begin with SELECT. The code below matches everything that starts with SELECT.

String sa = "THIS SELECT * from table;";
System.out.println(sa.matches("(?i)^select .*")); //FALSE as the input string is not valid select statement
sa = "SELECT * from table;";
System.out.println(sa.matches("(?i)^select .*")); //TRUE as the input string is  valid select statement
+1
source

All Articles