Task 1: Connect a Database to a Spring App
- Start MySQL Workbench and create a new schema named techjobs.
Remember to double click on the schema name in the file tree to make it the default schema.
- In the administration tab, create a new user, techjobswith the same settings as described in the lesson tutorial. This user should have the passwordtechjobsas well.
- Update build.gradlewith the necessary dependencies. At this point, you should be able to run the tests. Run the tests inTestTaskOneto verify your gradle dependencies.
- Update src/resources/application.propertieswith the correct info. This will include settingspring.datasource.urlto the address of your database connection, as well as the username and password for a user you have created. Refer to the tip below for the other properties you must add to complete your database setup.
You can double check your setup against what you’ve already done for your coding events repo. You can copy these property assignments from your coding events repo, only needing to change the database address and username/password values.
If when starting your application, you encounter an error similar to
com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'CDT' is unrecognized …then add ?useLegacyDatetimeCode=false&serverTimezone=America/Chicago to the end of your spring.datasource.url value.
Test It with SQL
When your database is properly configured, you should have no compiler errors when starting the application. Execute bootRun
and check the compiler output to make sure this is the case. If everything runs, you will be able to view your app
locally in the browser at http://localhost:8080 (unless of course you have changed the server port).
- In your MySQL workbench, open a new query tab to check your database connection.
- SQL TASK: At this point, you will have one table, job. Inqueries.sqlunder “Part 1”, list the columns and their data types in the table as a SQL comment.
Your running application still has limited functionality. You won’t yet be able to add a job with the Add Job form. You also won’t yet be able to view the list of jobs or search for jobs - but this is mostly because you have no job data. Move on to Part 2 below to start adding these functionalities.