16. JDBC
Review the presentation JDBC
Tasks
Set up MySQL
- If needed:
To enable additional software repositories, run
sudo mousepad /etc/apt/sources.listand uncomment the lines that contain # deb... (delete #)
To refresh the repositories, run the command:
sudo apt-get update
To install MySQL Servers, run the following command:
sudo apt-get install mysql-server- Download the MySQL Workbench installation file from https://askubuntu.com/questions/1230752/how-can-i-install-mysql-workbench-on-ubuntu-20-04-lts for 64-bit Ubuntu 20.04.
- Open the file and install it.
Set up the database
- If necessary, change the values of the variables user, password and database in the TeacherManager.java class and in the src/resources/appplication.properties settings file
Create a new database user by executing the following commands in the terminal (each line one at a time!):
sudo mysql
CREATE USER 'u00'@'localhost' IDENTIFIED BY 'u00';
FLUSH PRIVILEGES;
exitwhere u00 is the code you provided.
Create the database structure by executing the following commands in the terminal:
cd ~/workspace/aa00000/src/main/java/jtm/activity16/
sudo mysql
create database database00;
GRANT ALL ON database00.* TO 'u00'@'localhost';
FLUSH PRIVILEGES;
exit
mysql -uu00 -pu00 database00 < database.sqlwhere aa00000 is your project name and u00 is the code you were given.
Set up verbose MySQL logging (optional)
To enable verbose logging on your MySQL server:
Open the configuration file:
sudo mousepad /etc/mysql/mysql.conf.d/mysqld.cnfChange the two lines to the following (i.e., remove the # from the beginning of the line):
general_log_file = /var/log/mysql/query.log
general_log = 1Restart the mysql server:
sudo systemctl restart mysqlTo view the log in real time, run the command:
sudo tail -f /var/log/mysql/query.log
Implement the class
- Explore the jtm.activity16 package
- Implement the TeacherManager class to pass the unit tests
Error Solutions
To check if the 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:*To check if you can log in to the server, run the MySQL console:
mysql -uu00 -pu00Then in the MySQL console:
show databases;should return the database list.
exit))
Extra exercises
Additional information
In English
- DBeaver tool
- JDBC tutorial (PDF file)