Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Be sure you have installed MySQL Server and Workbench, and that they are functioning, before starting this assignment. Modify your Java Review 2 Assignment Java
- Be sure you have installed MySQL Server and Workbench, and that they are functioning, before starting this assignment.
- Modify your Java Review 2 Assignment Java program to fulfill the following
Part 1
- Go to this Github URL (Links to an external site.) and download the following two files:
- Database creation script named BuildPracticeDatabase.sql.
- .java file named PracticeDBDemo.java.
- Inside of MySQL Workbench open the BuildPracticeDatabase.sql script file and run it then refresh your Schemas list to confirm you have the database called Practice.
- In Workbench also, if you already have a login created for access from Java, add the Practice schema to that login and set Object Rights to everything checked.
- The PracticeDBDemo.java file provides an example of reading temperature data from a MySQL database.
- Make a copy of your Java Review 2 Assignment Java program.
- In the copy, replace the code that reads data from the csv file with reading the data from the MySQL database based on the PracticeDBDemo.java sample file.
- You may need to modify the connection information in your program depending on your MySQL server set up and the login/password you created in your development environment.
- Modify the report you created in assignment 2 to display it on the console as shown below.
- Note that the report format is slightly different from Review assignment 2, it now shows the full date which comes from the database data.
- And also write the report to a new file called TemperaturesReportFromDB.txt.
- See the notes below the report example for additional information...
-------------------------------------------------------------- December 2020: Temperatures in Utah -------------------------------------------------------------- Date High Low Variance -------------------------------------------------------------- 12/1/2020 40 25 15 12/2/2020 41 21 20 12/3/2020 39 20 19 12/4/2020 37 18 19 12/5/2020 40 19 21 12/6/2020 42 19 23 12/7/2020 43 19 24 12/8/2020 42 20 22 12/9/2020 39 19 20 12/10/2020 36 20 16 12/11/2020 35 20 15 12/12/2020 32 18 14 12/13/2020 31 16 15 12/14/2020 28 23 5 12/15/2020 35 20 15 12/16/2020 43 28 15 12/17/2020 38 30 8 12/18/2020 35 30 5 12/19/2020 42 25 17 12/20/2020 42 27 15 12/21/2020 53 35 18 12/22/2020 49 30 19 12/23/2020 32 21 11 12/24/2020 37 19 18 12/25/2020 36 17 19 12/26/2020 33 20 13 12/27/2020 39 22 17 12/28/2020 35 21 14 12/29/2020 33 17 16 12/30/2020 33 18 15 12/31/2020 34 26 8 -------------------------------------------------------------- December Highest Temperature: 12/21: 53 Average Hi: 37.9 December Lowest Temperature: 12/13: 16 Average Lo: 22.0 -------------------------------------------------------------- Graph -------------------------------------------------------------- 1 5 10 15 20 25 30 35 40 45 50 | | | | | | | | | | | -------------------------------------------------------------- 1 Hi ++++++++++++++++++++++++++++++++++++++++ Lo ------------------------- 2 Hi +++++++++++++++++++++++++++++++++++++++++ Lo --------------------- 3 Hi +++++++++++++++++++++++++++++++++++++++ Lo -------------------- 4 Hi +++++++++++++++++++++++++++++++++++++ Lo ------------------ 5 Hi ++++++++++++++++++++++++++++++++++++++++ Lo ------------------- 6 Hi ++++++++++++++++++++++++++++++++++++++++++ Lo ------------------- 7 Hi +++++++++++++++++++++++++++++++++++++++++++ Lo ------------------- 8 Hi ++++++++++++++++++++++++++++++++++++++++++ Lo -------------------- 9 Hi +++++++++++++++++++++++++++++++++++++++ Lo ------------------- 10 Hi ++++++++++++++++++++++++++++++++++++ Lo -------------------- 11 Hi +++++++++++++++++++++++++++++++++++ Lo -------------------- 12 Hi ++++++++++++++++++++++++++++++++ Lo ------------------ 13 Hi +++++++++++++++++++++++++++++++ Lo ---------------- 14 Hi ++++++++++++++++++++++++++++ Lo ----------------------- 15 Hi +++++++++++++++++++++++++++++++++++ Lo -------------------- 16 Hi +++++++++++++++++++++++++++++++++++++++++++ Lo ---------------------------- 17 Hi ++++++++++++++++++++++++++++++++++++++ Lo ------------------------------ 18 Hi +++++++++++++++++++++++++++++++++++ Lo ------------------------------ 19 Hi ++++++++++++++++++++++++++++++++++++++++++ Lo ------------------------- 20 Hi ++++++++++++++++++++++++++++++++++++++++++ Lo --------------------------- 21 Hi +++++++++++++++++++++++++++++++++++++++++++++++++++++ Lo ----------------------------------- 22 Hi +++++++++++++++++++++++++++++++++++++++++++++++++ Lo ------------------------------ 23 Hi ++++++++++++++++++++++++++++++++ Lo --------------------- 24 Hi +++++++++++++++++++++++++++++++++++++ Lo ------------------- 25 Hi ++++++++++++++++++++++++++++++++++++ Lo ----------------- 26 Hi +++++++++++++++++++++++++++++++++ Lo -------------------- 27 Hi +++++++++++++++++++++++++++++++++++++++ Lo ---------------------- 28 Hi +++++++++++++++++++++++++++++++++++ Lo --------------------- 29 Hi +++++++++++++++++++++++++++++++++ Lo ----------------- 30 Hi +++++++++++++++++++++++++++++++++ Lo ------------------ 31 Hi ++++++++++++++++++++++++++++++++++ Lo -------------------------- -------------------------------------------------------------- | | | | | | | | | | | 1 5 10 15 20 25 30 35 40 45 50 --------------------------------------------------------------
- Report Notes
- Note that the report is essentially two reports combined to be presented together.
- The first report lists all days and temperatures in a tabular format.
- Each row of the tabular report displays the day, high temperature, and low temperature.
- Each row also shows the variance between the high and low temperatures for that day.
- At the bottom of the tabular report is a summary that reports the day that had the highest temperature, the highest temperature, and also the average high temperature for the month.
- The summary at the bottom of the tabular report also reports the day that had the lowest temperature, the lowest temperature, and also the average low temperature for the month.
- The second report presents a simple graph to visually show the temperatures for each day.
- There is a scale presented first to aid the user in seeing the magnitude of each day's hi/lo temperatures.
- Each day contains a number of characters that match the temperature. You can use any character you prefer.
- The scale is presented again at the bottom of the report as an additional aid to the user.
Part 2
- The practice database contains data for November and December 2020.
- Modify the new version of your program to prompt the user for which month they would like to view the report for and display the report accordingly based on the chosen month's data.
- You will need variables to replace hardcoded values and modify your code to use those variables in the SQL SELECT statement and the report.
- Your modified program should display the user-selected month report to the console
- And also write the report to the TemperaturesReportFromDB.txt file and over-write that report each run.
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
public class PracticeDBDemo | |
{ | |
public static void main(String[] args) | |
{ | |
// Your database connection information may be different depending on | |
// your MySQL installation and the dbLogin and dbPassword you choose | |
// to use in your database. | |
String connectionString = "jdbc:mysql://127.0.0.1:3306/practice"; | |
String dbLogin = "javauser"; | |
String dbPassword = "j4v4us3r?"; | |
Connection conn = null; | |
String sql = "SELECT month, day, year, hi, lo FROM temperatures " | |
+ "WHERE month = 12 AND year = 2020 ORDER BY month, day, year;"; | |
try | |
{ | |
conn = DriverManager.getConnection(connectionString, dbLogin, dbPassword); | |
if (conn != null) | |
{ | |
try (Statement stmt = conn.createStatement( | |
ResultSet.TYPE_SCROLL_INSENSITIVE, | |
ResultSet.CONCUR_UPDATABLE); | |
ResultSet rs = stmt.executeQuery(sql)) | |
{ | |
int numRows; | |
int numCols = 5; | |
rs.last(); | |
numRows = rs.getRow(); | |
System.out.printf("Number of Records: %d%n", numRows); | |
rs.first(); | |
String[][] dbResults = new String[numRows][numCols];; | |
for (int i = 0; i < numRows; i++) | |
{ | |
dbResults[i][0] = rs.getString("month"); | |
dbResults[i][1] = rs.getString("day"); | |
dbResults[i][2] = rs.getString("year"); | |
dbResults[i][3] = rs.getString("hi"); | |
dbResults[i][4] = rs.getString("lo"); | |
rs.next(); | |
} | |
System.out.printf("Number of Array Rows: %d%n", dbResults.length); | |
printLine(30); | |
System.out.println("Date\t\tHi\tLo"); | |
printLine(30); | |
for (int i = 0; i < dbResults.length; i++) | |
{ | |
System.out.printf("%s/%s/%s\t%s\t%s%n", | |
dbResults[i][0], | |
dbResults[i][1], | |
dbResults[i][2], | |
dbResults[i][3], | |
dbResults[i][4]); | |
} | |
printLine(30); | |
} | |
catch (SQLException ex) | |
{ | |
System.out.println(ex.getMessage()); | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
System.out.println("Database connection failed."); | |
e.printStackTrace(); | |
} | |
} | |
// Method to print lines in the console as a separator | |
private static void printLine(int dashes) | |
{ | |
for (int i = 1; i <= dashes; i++) | |
{ | |
System.out.print("-"); | |
} | |
System.out.print(" "); | |
} | |
} |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started