Skip to content

Commit b7976c1

Browse files
Finished Unit 5 Homework
1 parent f14acd3 commit b7976c1

File tree

16 files changed

+278
-0
lines changed

16 files changed

+278
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
This program was written by Kyle Martin on 8/7/2021 for Java Programming Principles 2 during Summer Session 2
3+
at Southwestern College, Kansas.
4+
5+
IMPORTANT: Normally I would not place a bunch of comments in my code describing what my code is doing as I like to
6+
have code that is written in a manner to be understandable while reading it. Though, do to the grading rubric I will
7+
explain my code.
8+
9+
This program was created to fix sudo code.
10+
See Chapter 16 Review Questions - Find the Error Question 1
11+
*/
12+
SELECT * FROM Coffee WHERE Description = 'French Roast Dark'
13+
-- Changed double quotes to single quotes
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
This program was written by Kyle Martin on 8/7/2021 for Java Programming Principles 2 during Summer Session 2
3+
at Southwestern College, Kansas.
4+
5+
IMPORTANT: Normally I would not place a bunch of comments in my code describing what my code is doing as I like to
6+
have code that is written in a manner to be understandable while reading it. Though, do to the grading rubric I will
7+
explain my code.
8+
9+
This program was created to fix sudo code.
10+
See Chapter 16 Review Questions - Find the Error Question 2
11+
*/
12+
String sql = "SELECT * FROM Coffee";
13+
Statement stmt = conn.createStatement();
14+
ResultSet result = stmt.executeQuery(sql); //Added Query to execute command
15+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
This program was written by Kyle Martin on 8/7/2021 for Java Programming Principles 2 during Summer Session 2
3+
at Southwestern College, Kansas.
4+
5+
IMPORTANT: Normally I would not place a bunch of comments in my code describing what my code is doing as I like to
6+
have code that is written in a manner to be understandable while reading it. Though, do to the grading rubric I will
7+
explain my code.
8+
9+
This program was created to fix sudo code.
10+
See Chapter 16 Review Questions - Find the Error Question 2
11+
*/
12+
SELECT * FROM Coffee WHERE ProdNum != 14-001
13+
-- Got rid of quotes as it seems to be a number not a string
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
This program was written by Kyle Martin on 8/7/2021 for Java Programming Principles 2 during Summer Session 2
3+
at Southwestern College, Kansas.
4+
5+
IMPORTANT: Normally I would not place a bunch of comments in my code describing what my code is doing as I like to
6+
have code that is written in a manner to be understandable while reading it. Though, do to the grading rubric I will
7+
explain my code.
8+
9+
This file was created to answer questions from the Algorithm Workbench portion of Ch. 16
10+
See Chapter 16 Algorithm Workbench - Exercise Two
11+
*/
12+
13+
/*
14+
Look at the following SQL statement:
15+
SELECT Name FROM Employee
16+
17+
What is the name of the table from which this statement is retrieving data?
18+
Answer: Employee
19+
20+
What is the name of the column that is being retrieved?
21+
Answer: Name
22+
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
This program was written by Kyle Martin on 8/7/2021 for Java Programming Principles 2 during Summer Session 2
3+
at Southwestern College, Kansas.
4+
5+
IMPORTANT: Normally I would not place a bunch of comments in my code describing what my code is doing as I like to
6+
have code that is written in a manner to be understandable while reading it. Though, do to the grading rubric I will
7+
explain my code.
8+
9+
This file was created to answer questions from the Algorithm Workbench portion of Ch. 16
10+
See Chapter 16 Algorithm Workbench - Exercise One
11+
*/
12+
13+
/*
14+
What SQL data types correspond with the following Java types?
15+
int
16+
float
17+
String
18+
double
19+
*/
20+
21+
-- Answers:
22+
/*
23+
int = INTEGER or INT
24+
float = REAL
25+
String = CHARACTER or CHAR or VARCHAR
26+
double = DOUBLE
27+
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
This program was written by Kyle Martin on 8/7/2021 for Java Programming Principles 2 during Summer Session 2
3+
at Southwestern College, Kansas.
4+
5+
IMPORTANT: Normally I would not place a bunch of comments in my code describing what my code is doing as I like to
6+
have code that is written in a manner to be understandable while reading it. Though, do to the grading rubric I will
7+
explain my code.
8+
9+
This file was created to answer questions from the Algorithm Workbench portion of Ch. 16
10+
See Chapter 16 Algorithm Workbench - Exercise Three
11+
*/
12+
13+
SELECT * FROM STOCK
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This program was written by Kyle Martin on 8/7/2021 for Java Programming Principles 2 during Summer Session 2
3+
* at Southwestern College, Kansas.
4+
*
5+
* IMPORTANT: Normally I would not place a bunch of comments in my code describing what my code is doing as I like to
6+
* have code that is written in a manner to be understandable while reading it. Though, do to the grading rubric I will
7+
* explain my code.
8+
*
9+
* This program was created to sum a total from a database
10+
* See Chapter 16 Programming Challenges - Challenge Three.
11+
*/
12+
import java.sql.*;
13+
14+
public class UnpaidOrderCost {
15+
public static void main(String[] args) {
16+
String DB_URL = "jdbc:mysql://localhost:3306/CoffeeDB";
17+
String DB_USER = "root";
18+
String DB_Pass = "password"; //Actual password changed, also hard coding secrets isn't a good idea
19+
20+
try {
21+
Connection DBConnection;
22+
DBConnection = DriverManager.getConnection(DB_URL, DB_USER, DB_Pass);
23+
Statement DBStatement = DBConnection.createStatement();
24+
25+
String DBCommand = "SELECT SUM(Cost) FROM UnpaidOrder";
26+
ResultSet result = DBStatement.executeQuery(DBCommand);
27+
28+
if (result.next())
29+
System.out.println("Total Cost = " + result.getDouble(1));
30+
31+
DBStatement.close();
32+
DBConnection.close();
33+
} catch (SQLException throwables) {
34+
throwables.printStackTrace();
35+
System.out.println("Error:" + throwables.getMessage());
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* This program was written by Kyle Martin on 8/7/2021 for Java Programming Principles 2 during Summer Session 2
3+
* at Southwestern College, Kansas.
4+
*
5+
* IMPORTANT: Normally I would not place a bunch of comments in my code describing what my code is doing as I like to
6+
* have code that is written in a manner to be understandable while reading it. Though, do to the grading rubric I will
7+
* explain my code.
8+
*
9+
* This program was created to provide querys from a dropdown and then execute them and see the results
10+
* See Chapter 16 Programming Challenges - Challenge Four.
11+
*/
12+
13+
import javafx.application.Application;
14+
import javafx.collections.FXCollections;
15+
import javafx.collections.ObservableList;
16+
import javafx.scene.control.ComboBox;
17+
import javafx.stage.Stage;
18+
import javafx.scene.Scene;
19+
import javafx.scene.control.TextArea;
20+
import javafx.scene.control.Button;
21+
import javafx.scene.layout.VBox;
22+
import javafx.geometry.Insets;
23+
import javafx.geometry.Pos;
24+
import javafx.event.EventHandler;
25+
import javafx.event.ActionEvent;
26+
import java.sql.*;
27+
28+
public class DBViewer extends Application
29+
{
30+
final String DB_URL = "jdbc:mysql://localhost:3306/CityDB"; // Don't Hardcode Secrets
31+
final String DB_USER = "root"; // Don't Hardcode Secrets
32+
final String DB_Pass = "password"; // Don't Hardcode Secrets
33+
34+
TextArea queryTextArea;
35+
TextArea resultsTextArea;
36+
ComboBox comboBox;
37+
38+
public static void main(String[] args)
39+
{
40+
launch(args);
41+
}
42+
43+
@Override
44+
public void start(Stage primaryStage)
45+
{
46+
final int COL_SIZE = 50;
47+
final int ROW_SIZE = 10;
48+
final double SPACING = 10.0;
49+
50+
String populationAscending = "SELECT * FROM City ORDER BY Population ASC";
51+
String populationDescending = "SELECT * FROM City ORDER BY Population DESC";
52+
String sortCityNames = "SELECT CityName FROM City ORDER BY CityName ASC";
53+
String totalPopulation = "SELECT SUM(Population) FROM City";
54+
String avgPopulation = "SELECT AVG(Population) FROM City";
55+
String highestPopulation = "SELECT MAX(Population) FROM City";
56+
String lowestPopulation = "SELECT MIN(Population) FROM City";
57+
58+
ObservableList<String> queryOptions =
59+
FXCollections.observableArrayList(populationAscending,
60+
populationDescending,
61+
sortCityNames,
62+
totalPopulation,
63+
avgPopulation,
64+
highestPopulation,
65+
lowestPopulation);
66+
comboBox = new ComboBox(queryOptions);
67+
68+
// Build the query results area.
69+
resultsTextArea = new TextArea();
70+
resultsTextArea.setPrefColumnCount(COL_SIZE);
71+
resultsTextArea.setPrefRowCount(ROW_SIZE);
72+
73+
// Create the Submit button.
74+
Button submitButton = new Button("Submit");
75+
submitButton.setOnAction(new SubmitButtonHandler());
76+
77+
// Put the controls in a VBox.
78+
VBox vbox = new VBox(SPACING, comboBox,
79+
submitButton, resultsTextArea);
80+
vbox.setAlignment(Pos.CENTER);
81+
vbox.setPadding(new Insets(SPACING));
82+
83+
primaryStage.setTitle("Population Database - Kyle Martin");
84+
85+
Scene scene = new Scene(vbox);
86+
primaryStage.setScene(scene);
87+
88+
primaryStage.show();
89+
}
90+
91+
/**
92+
* Event handler class for submitButton
93+
*/
94+
95+
class SubmitButtonHandler implements EventHandler<ActionEvent>
96+
{
97+
@Override
98+
public void handle(ActionEvent event)
99+
{
100+
try
101+
{
102+
resultsTextArea.setText("");
103+
104+
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_Pass);
105+
Statement stmt = conn.createStatement();
106+
107+
ResultSet resultSet =
108+
stmt.executeQuery(comboBox.getValue().toString());
109+
110+
ResultSetMetaData meta = resultSet.getMetaData();
111+
112+
String output = "";
113+
114+
while (resultSet.next())
115+
{
116+
for (int i = 1; i <= meta.getColumnCount(); i++)
117+
{
118+
output += resultSet.getString(i);
119+
output += '\t';
120+
}
121+
output += '\n';
122+
}
123+
124+
resultsTextArea.setText(output);
125+
126+
stmt.close();
127+
conn.close();
128+
129+
}
130+
catch (SQLException e)
131+
{
132+
e.printStackTrace();
133+
System.exit(0);
134+
}
135+
}
136+
}
137+
}

0 commit comments

Comments
 (0)