This is a problem that I always encounter when I have to connect to a data base; how to separate SQL from the normal java code? I usually use a separate class for the Database connections, however, when you have several databases and several tables in each database, it is always difficult to do this 100%
As an example, if we want to put all the java SQL in a class named DBConnector.java, how do we code generically for different Insertions, Deletions, Data Retrieval, etc? What I'm thinking as the ideal case is that all the SQL statements should be in the same class and should be compatible with different flavors of same operation within the scope of the database application, thus providing a logical separation from rest of the code.
public void insertData (String db, String table, <Whatever Fields to be Inserted>)
{
//generic SQL INSERT statement and execution
}
public ResultSet retrieveData (String db, String table, <Whatever Fields Relevant>)
{
//generic retrieval of data
}
Is there any way to accomplish this? Or should we just add functionality for different flavors of Inserting, Querying, etc?
Thank you!