Refactoring to Use a Database

In our current system, the balance of the single account is stored in a file and read and written by the BalanceStore class. In our new design, we will make the Account responsible for reading the balance straight out of a database instead. It’s refactoring time again!

Creating the Database

Once you have installed MySQL[47] you will need to create a database and user. This can be done simply, using the setup-bank.sql script:

databases/00/setup-bank.sql
 
CREATE​ ​DATABASE​ bank;
 
CREATE​ USER ​'teller'​@​'localhost'​ IDENTIFIED ​BY​ ​'password'​;
 
GRANT ALL ​ON​ bank.* TO ​'teller'​@​'localhost'​;

As you can see, this script creates a database called bank. It then adds a user teller with password password and ...

Get The Cucumber for Java Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.