My application contains over 20 individual SQL queries and updates
Right now, the queries are located wherever they are used in the code, usually spun up with StringBuilders to avoid spaghetti code, but I feel that this is not good for long-term scalability and sanity.
I want to know if there is a “best” way of storing multiple SQL queries in my Java application.
Right now queries are stored in the application as such
<code>StringBuilder query = new StringBuilder();
query.append("SELECT * FROM MY_TABLE ");
query.append("WHERE MY_FIELD = ?");
try (PreparedStatement statement = connection.prepareStatement(query.toString())) {
...
}
</code>
<code>StringBuilder query = new StringBuilder();
query.append("SELECT * FROM MY_TABLE ");
query.append("WHERE MY_FIELD = ?");
try (PreparedStatement statement = connection.prepareStatement(query.toString())) {
...
}
</code>
StringBuilder query = new StringBuilder();
query.append("SELECT * FROM MY_TABLE ");
query.append("WHERE MY_FIELD = ?");
try (PreparedStatement statement = connection.prepareStatement(query.toString())) {
...
}
Here are my two ideas, as of now:
- Create a class with static references to my queries
- Create a class which reads *.sql files from resources on demand
Any input would be valuable here. Thanks.