Search code examples
javamysqlregexspecial-characters

Is the checking Special Character in Java better or in MYSQL?


Before saving the user entered data I want to check whether the entered data has Special Characters or not. Do not want to save Special Characters

Checking for Special Character can be done in JAVA as given in this link

Checking of Special Character can be done in MYSQL as given in this link

I want to know, Is it better to check Special Characters in JAVA or in MYSQL, In terms of performance?


Solution

  • It's more efficient to do it in Java, since you'll save a roundtrip to the database. It's also more logical to do it in Java, because this is business logic, and the business logic should generally be in the application code, not in the database.

    Here are some good rules of thumbs regarding performance:

    • don't base all your design choices on performance. Try making your code clean, well structured, and working first. Then if you have a performance problem, measure to know where it comes from. Then only optimize where it's necessary. Premature optimization is the root of all evil.
    • some lines of code manipulating data in memory are almost always several orders of magnitude faster than an interprocess or, even worse, network call. The memory is the house next door. The database is planet Mars.