Mouna Mouna - 1 year ago 103
SQL Question

How to externalize an insert file SQL query

In my application, I want to externalize SQL query (in .properties files for example). But sometimes I had to insert the entire content of a text file into a CLOB column.

This is the code I use now:

String requete = "the content of the file in xml";
PreparedStatement prepareStatement = con.prepareStatement("INSERT INTO \"TABLE\".\"_XML\" (ID, BLOC_XML) VALUES ('1',?)");
prepareStatement.setCharacterStream(1, new StringReader(requete), requete.length());

I really need to decouple the SQL logic from the application business logic. Any suggestions to tackle this problem.


Answer Source

It's not a good idea to externalize the SQL queries. Imagine that someone would change the .properties file to something like

drop table really_important_stuff;

Furthermore, as a developer I would prefer to have the SQL queries as close to the source code as possible. So that I do not need to look them up in some other resource.

It's simple to gain control of the complexity using DAO pattern for example.