COSC 304 - Introduction to Database Systems
Lab 6: Using Java/Python with MySQL and Microsoft SQL Server
This lab shows how Java and Python programs can connect to MySQL and Microsoft SQL Server.
Make sure your computer is setup to run Docker by following these setup instructions.
- Create a directory
cosc304_lab6
. - Download the
docker-compose.yml
file into thecosc304_lab6
directory. - Create a folder
ddl
incosc304_lab6
directory. Download the contents of theddl
folder into thecosc304_lab6\ddl
folder. - Open a command shell either directly on your machine or using VSCode. Make sure your current directory is
cosc304_lab6
. - Run the command
docker-compose up -d
- If everything is successful, the MySQL database will start on port 3306. If there is a port conflict, change the port to 3307 in the
docker-compose.yml
file by modifying the line belowports:
to'3307:3306'
. The connection URL is thenlocalhost:3307/testuser
. - Your database is
mydb
ortestuser
. There are other databases also created such asworkson
. Do NOT use the sampleuniversity
database from lab 2. - Microsoft SQL Server will be running on port 1433. Note SQL Server is not supported on the Apple M1 chip. However, there is an alternate version that is. In the docker-compose.yml file, change:
image: mcr.microsoft.com/mssql/server:2019-latest
to this:image: mcr.microsoft.com/azure-sql-edge
.
Download the sample Java program or sample Python program that connects to a WorksOn database hosted by MySQL.
The user id and password information is in the docker-compose.yml
file.
-
Make sure the MySQL JDBC driver is in your classpath. This can be done in VSCode in the
Java Project
tab. -
Download the sample file. Save this file in your
cosc304_lab6
folder that you just created. -
These are the modifications you must make to get the program working:
Change Line 5 to: String url = "jdbc:mysql://localhost/workson";
Change Line 6 to: String uid = "put your user id here";
Change Line 7 to: String pw = "put your password here";
-
Install MySQL connector library in a terminal using the command:
pip install mysql-connector-python
You may need to restart VSCode for it to see the library after it is installed. -
Download the sample Python file and setup in your Python environment.
-
These are the modifications you must make to get the program working:
Change Line 3 to add the password for testuser account:
cnx = mysql.connector.connect(user='testuser', <b>password='todo'</b>, host='localhost', database='workson', ssl_disabled='True') a
The result of the program is this:
Employee Name,Salary
M. Smith,50000.0
A. Lee,40000.0
J. Miller,20000.0
B. Casey,50000.0
L. Chu,30000.0
R. Davis,40000.0
J. Jones,50000.0
Create a new program called MyJDBC.java
or MySQLQuestion.py
.
Note: With Python to have multiple cursors on a connection, set the cursor to buffering like this: cursor = cnx.cursor(buffered=True)
.
Your program should be able to do this:
- List each employee that is a supervisor.
- Sort the list of supervisors by name.
- Under each supervisor, list the employees that he/she directly supervises sorted by decreasing salary.
The result of your program should look like this:
Supervisor: B. Casey
M. Smith, 50000.00
Supervisor: J. Jones
B. Casey, 50000.00
R. Davis, 40000.00
Supervisor: L. Chu
J. Miller, 20000.00
Supervisor: M. Smith
J. Doe, 30000.00
Supervisor: R. Davis
A. Lee, 40000.00
L. Chu, 30000.00
Answer: Java answer file, Python answer file
-
Make sure the Microsoft SQL Server JDBC driver is in your classpath. This can be done in VSCode in the
Java Project
tab. -
If the workson database was not automatically created, you must connect to SQL Server using SQuirreL or command line to create the workson database.
-
Download the sample file. Save this file in your
cosc304_lab6
folder that you just created. -
These are the modifications you must make to get the program working:
Change Line 5 to: String url = "jdbc:sqlserver://localhost;DatabaseName=workson;TrustServerCertificate=True";
Change Line 6 to: String uid = "sa";
Change Line 7 to: String pw = "put password here";
-
Install SQL Server pyodbc connector by following these directions.
-
Download the sample Python file and setup in your Python environment.
-
These are the modifications you must make to get the program working:
Change Line 3 and 4 to: cnx = pyodbc.connect("""DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost;
DATABASE=workson;UID=sa;PWD=yourPassword""")
Employee Name,Salary
M. Smith,50000.0
A. Lee,40000.0
J. Miller,20000.0
B. Casey,50000.0
L. Chu,30000.0
R. Davis,40000.0
J. Jones,50000.0
Modify either the Java or Python sample program to create a program that lists the top project for each department based on the total hours worked by employees on projects.
Id: D1 Name: Management
Proj# Name Total Hours
P1 Instruments 36
Id: D2 Name: Consulting
Proj# Name Total Hours
P4 Maintenance 96
Id: D3 Name: Accounting
Proj# Name Total Hours
P3 Budget 46
Answer: Java answer file, Python answer file