13. JDBC
Main steps
- Set up MySQL
- Set up database
- Configure detailed MySQL logging (Optional)
- Implement activity
- Extra exercise
- Error solution
- Extra exercises
- Additional info
Set up MySQL
- If necessary:
To enable additional software repositories, run
sudo mousepad /etc/apt/sources.listand uncomment lines staring with # deb... (delete #)
To refresh info about repositories run command:
sudo apt-get update
To install MySQL server, run following command:
sudo apt-get install mysql-server- Download MySQL Workbench installation file from https://dev.mysql.com/downloads/workbench/ for 64-bit Ubuntu 20.04
- Open file and install it.
Set up database
- If necessary, change values of user, password and database variables in TeacherManager.java class and key values of src/resources/appplication.properties configuration file
Create new database user, by executing following commands in terminal (each line one by one!):
sudo mysql
CREATE USER 'uXX'@'localhost' IDENTIFIED BY 'uXX';
FLUSH PRIVILEGES;
exitwhere XX is your given number.
Create structure of the database, by executing following commands in terminal:
cd ~/workspace/JTM/src/main/java/jtm/activity13/
sudo mysql
create database databaseXX;
GRANT ALL ON databaseXX.* TO 'uXX'@'localhost';
FLUSH PRIVILEGES;
exit
mysql -uuXX -puXX databaseXX < database.sqlwhere XX is your given number.
Configure detailed MySQL logging (Optional)
To enable detailed logging on MySQL server:
Open configuration file:
sudo mousepad /etc/mysql/mysql.conf.d/mysqld.cnfChange two lines to following (i.e. remove # from beginning of line):
general_log_file = /var/log/mysql/query.log
general_log = 1Restart mysql server:
sudo systemctl restart mysqlTo watch log in real time, run command:
sudo tail -f /var/log/mysql/query.log
Implement activity
- Investigate jtm.activity13 package
- Implement class TeacherManager to pass unit tests
Extra exercise
Error solution
To check that server is running:
ss -ntlp|grep 3306should return:
LISTEN 0 151 127.0.0.1:3306 0.0.0.0:*
LISTEN 0 70 127.0.0.1:33060 0.0.0.0:*Check, that you can log into server, run MySQL console:
mysql -uuXX -puXXThen, inside MySQL console:
show databases;should return list of databases.
exit
Extra exercises
Additional info
In English
- DBeaver tool
- JDBC tutorial (PDF file)