Reputation: 546
I have a DAO with many methods, each containing an SQL statement to execute. I don't want to hard code SQL strings into the DAO Java class so what's the best way of managing this scenario? I am thinking of injecting them, but how best to do this?
Cheers, Matt
Upvotes: 1
Views: 298
Reputation: 12084
You can inject your SQL statements during application initialization.
class MyDAO {
private String someQuery;
public void setSomeQuery(String someQuery) {
this.someQuery = someQuery;
}
}
Then you can have your SQL statments in XML file, not hardcoded.
<bean class="some.package.MyDAO">
<property name="someQuery">
<value>
SELECT * FROM user WHERE id = ?
</value>
</property>
</bean>
You can even externalize your SQL statements to .properties
files.
Take a look: http://www.summa-tech.com/blog/2009/04/20/6-tips-for-managing-property-files-with-spring/
Upvotes: 2