patternjavaMinor
Database access concept
Viewed 0 times
databaseconceptaccess
Problem
I've got a small tomcat web application that's used to receive customer data from an iPad app. After receiving those information they will be saved on a local MySQL database and then sent to an SAP system.
I'm focusing on my "concept" of handling database connections since this is my first ever productive web app that makes intense use of the database. I was wondering if there is something wrong in the way I talk with the database or if this might be prone to problems in future.
I have a class
This Database class is used throughout the whole application (nearly every other class uses the
```
private void insertDealer(int dealer
I'm focusing on my "concept" of handling database connections since this is my first ever productive web app that makes intense use of the database. I was wondering if there is something wrong in the way I talk with the database or if this might be prone to problems in future.
I have a class
Database that creates the connection to the database and then provides a PreparedStatement through a method getPreparedStatement(String sql) that can be filled with the SQL query. It looks like this:public class Database {
private Connection connection = null;
private Config config = ConfigurationUtils.config;
public Database() {
try {
// Load the MySQL driver
Class.forName("com.mysql.jdbc.Driver");
// Create the database connection
this.connection = DriverManager.getConnection("jdbc:mysql://" + config.getDatabase().getHost() + ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabaseName() + "?user=" + config.getDatabase().getUser() + "&password=" + config.getDatabase().getPassword());
} catch (Exception e) {
e.printStackTrace();
}
}
public PreparedStatement getPreparedStatement(String sql) throws SQLException {
return this.connection.prepareStatement(sql);
}
public void close() throws SQLException {
this.connection.close();
}
}This Database class is used throughout the whole application (nearly every other class uses the
Database class, some classes might even use them twice because multiple SQL queries have to be executed). The following example is the code of a method insertDealer() that's used to fill the database with master data about our dealerships:```
private void insertDealer(int dealer
Solution
-
I don't like repeated calls like this
-
You will run into trouble when writing unit tests. Right now you have no way of mocking out the database access which forces you to run against a real MySql database which tends to make unit testing a pain in the backside.
Create an interface for
I don't like repeated calls like this
config.getDatabase().getHost() + ":" + config.getDatabase().getPort() + .... config.GetDatabase() should be called once and stored in a local variable.-
You will run into trouble when writing unit tests. Right now you have no way of mocking out the database access which forces you to run against a real MySql database which tends to make unit testing a pain in the backside.
Create an interface for
Database against which all your business logic should work and inject it into the classes responsible for creating the database queries. Then during unit testing you can pass in a mock-implementation which simply checks that the methods are preparing the statements correctly without having to run a query against an actual database.Context
StackExchange Code Review Q#82661, answer score: 2
Revisions (0)
No revisions yet.