Advertisement
Guest User

Untitled

a guest
Dec 1st, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.88 KB | None | 0 0
  1. // Student Name: Daniel Posvolsky Bujarski
  2. // Student Number: 3089771
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8. import java.util.Scanner;
  9.  
  10. /**
  11. *
  12. * @author User
  13. */
  14. public class TeamDB
  15. {
  16. public static void main(String[] args)
  17. {
  18. // Create a named constant for the URL
  19. // NOTE: This value is specific for Java DB
  20. final String DB_URL = "jdbc:mysql://localhost:3306/teamdb"; // Did it using mysql but professor said it was okay to change the URl
  21. Connection conn;
  22. try
  23. {
  24. // Create a connection to the database
  25. conn = DriverManager.getConnection(DB_URL,"root","");
  26. char choice;
  27. Scanner keyboard = new Scanner(System.in);
  28. System.out.println("Welcome to the Sports " +
  29. "Teams Database Manager!");
  30. do
  31. {
  32. printMenu();
  33. choice = keyboard.nextLine().charAt(0);
  34. switch(choice)
  35. {
  36. case '0':
  37. // Close the connection
  38. conn.close();
  39. break;
  40. case '1':
  41. viewTeams(conn);
  42. break;
  43. case '2':
  44. viewSchedule(conn);
  45. break;
  46. case '3':
  47. addTeams(conn);
  48. break;
  49. case '4':
  50. addGames(conn);
  51. break;
  52. case '5':
  53. enterScores(conn);
  54. break;
  55. case '6':
  56. beginNewSeason(conn);
  57. break;
  58. }
  59. } while(choice != '0');
  60. }
  61. catch(Exception ex)
  62. {
  63. System.out.println("ERROR: " + ex.getMessage());
  64. }
  65. }
  66. /**
  67. The printMenu method displays the menu choices
  68. for the user to work with the database.
  69. */
  70. public static void printMenu()
  71. {
  72. System.out.println();
  73. System.out.println("Select from the following options:");
  74. System.out.println("1. View team standings");
  75. System.out.println("2. View the schedule");
  76. System.out.println("3. Add a team");
  77. System.out.println("4. Add a game to the schedule");
  78. System.out.println("5. Enter game scores");
  79. System.out.println("6. Begin a new season");
  80. System.out.println("0. Exit the program");
  81. }
  82. /**
  83. The beginNewSeason method is a utility method that
  84. removes the tables and allows the user to reset the
  85. database for a new season.
  86. @param conn A connection to the database.
  87. */
  88. public static void beginNewSeason(Connection conn)
  89. {
  90. try
  91. {
  92. Statement stmt = conn.createStatement();
  93. // Remove tables if database tables have been created.
  94. // This will throw an exception if the tables do not exist
  95. stmt.execute("DROP TABLE Games");
  96. stmt.execute("DROP TABLE Teams");
  97. // Once the tables have been removed, call the method to
  98. // create and initialize the tables
  99. System.out.println("Reinitializing database " +
  100. "for a new season");
  101. createTeamDB(conn);
  102. }
  103. catch(Exception ex)
  104. {
  105. // Call the method to create tables for the database
  106. System.out.println("Creating database for the first time");
  107. createTeamDB(conn);
  108. }
  109. }
  110. /**
  111. The createTeamDB method is a utility method that
  112. creates the tables and initializes the database
  113. with teams and games.
  114. @param conn A connection to the database.
  115. */
  116. public static void createTeamDB(Connection conn)
  117. {
  118. try
  119. {
  120. Statement stmt = conn.createStatement();
  121. // Create the table of teams
  122. stmt.execute("CREATE TABLE Teams (" +
  123. "TeamName CHAR(15) NOT NULL PRIMARY KEY, " +
  124. "Wins INT, " + "Losses INT, " +
  125. "Ties INT" + ")");
  126. // Add some teams
  127. stmt.executeUpdate("INSERT INTO Teams " +
  128. "(TeamName) " +
  129. "VALUES ('Astros')");
  130. stmt.executeUpdate("INSERT INTO Teams " +
  131. "(TeamName) " +
  132. "VALUES ('Marlins')");
  133. stmt.executeUpdate("INSERT INTO Teams " +
  134. "(TeamName) " +
  135. "VALUES ('Brewers')");
  136. stmt.executeUpdate("INSERT INTO Teams " +
  137. "(TeamName) " +
  138. "VALUES ('Cubs')");
  139. // Create a listing of the games to be played
  140. stmt.execute("CREATE TABLE Games (" +
  141. "GameNumber INT NOT NULL PRIMARY KEY, " +
  142. "HomeTeam CHAR(15) NOT NULL REFERENCES " +
  143. "Teams (TeamName), " +
  144. "HomeTeamScore INT, " +
  145. "VisitorTeam CHAR(15) NOT NULL " +
  146. "REFERENCES Teams (TeamName), " +
  147. "VisitorTeamScore INT" + ")");
  148. stmt.executeUpdate("INSERT INTO Games " +
  149. "(GameNumber, HomeTeam, VisitorTeam) " +
  150. "VALUES (1, 'Astros', 'Brewers')");
  151. stmt.executeUpdate("INSERT INTO Games " +
  152. "(GameNumber, HomeTeam, VisitorTeam) " +
  153. "VALUES (2, 'Brewers', 'Cubs')");
  154. stmt.executeUpdate("INSERT INTO Games " +
  155. "(GameNumber, HomeTeam, VisitorTeam) " +
  156. "VALUES (3, 'Cubs', 'Astros')");
  157. }
  158. catch (Exception ex)
  159. {
  160. System.out.println("ERROR: " + ex.getMessage());
  161. }
  162. }
  163. /**
  164. The addTeams method allows the user to add
  165. more teams to the database.
  166. @param conn A connection to the database.
  167. */
  168. public static void addTeams(Connection conn)
  169. {
  170. Scanner keyboard = new Scanner(System.in);
  171. try
  172. {
  173. char ans;
  174. String teamName = "";
  175. Statement stmt = conn.createStatement();
  176. do
  177. {
  178. // TASK #3
  179. // Prompt the user for a new team name
  180. System.out.print("Team name: ");
  181. teamName = keyboard.nextLine();
  182. String sqlStatement = "INSERT INTO teams VALUES ("+ teamName + ",0,0,0)";
  183. stmt.execute(sqlStatement);
  184. // Write SQL statement and update the Teams table
  185. System.out.print("Do you want to enter another team: ");
  186. ans = keyboard.nextLine().charAt(0);
  187. } while(ans == 'Y'|| ans == 'y');
  188.  
  189.  
  190.  
  191. }
  192. catch(Exception ex)
  193. {
  194. System.out.println("ERROR: " + ex.getMessage());
  195. }
  196. }
  197. /**
  198. The addGames method allows the user to add games to the
  199. schedule. A unique game number is created for each game
  200. on the schedule.The user will need to supply a home team
  201. name and a visitor team name from the keyboard.
  202. @param conn A connection to the database.
  203. */
  204. public static void addGames(Connection conn)
  205. {
  206. Scanner keyboard = new Scanner (System.in);
  207. try
  208. {
  209. char ans;
  210. String homeTeam;
  211. String visitingTeam;
  212. int gameNumber = 1;
  213. Statement stmt = conn.createStatement();
  214. // This retrieves the data and allows you to count
  215. // the number of games already scheduled so that you
  216. // add a unique game number
  217. String sqlStatement = "SELECT * from Games";
  218. ResultSet result = stmt.executeQuery(sqlStatement);
  219. while(result.next())
  220. {
  221. gameNumber++;
  222. }
  223. do
  224. {
  225. System.out.print("Enter the home team name: ");
  226. homeTeam = keyboard.nextLine();
  227. System.out.print("Enter the visiting team name: ");
  228. visitingTeam = keyboard.nextLine();
  229. sqlStatement = "INSERT INTO Games " +
  230. "(GameNumber, HomeTeam, VisitorTeam) " +
  231. "VALUES (" + gameNumber + ", '" +
  232. homeTeam + "', '" + visitingTeam + "')";
  233. stmt.executeUpdate(sqlStatement);
  234. System.out.print("Do you want to enter another game: ");
  235. ans = keyboard.nextLine().charAt(0);
  236. } while(ans == 'Y'|| ans == 'y');
  237. }
  238. catch(Exception ex)
  239. {
  240. System.out.println("ERROR: " + ex.getMessage());
  241. }
  242. }
  243. /**
  244. The viewTeams method displays a table listing the
  245. team names and season records. Since teams have not
  246. yet played, all numbers are zero.
  247. @param conn A connection to the database.
  248. */
  249. public static void viewTeams(Connection conn)
  250. {
  251. try
  252. {
  253. // Create a Statement object
  254. Statement stmt = conn.createStatement();
  255. // Create a string with a SELECT statement
  256. String sqlStatement = "SELECT * FROM Teams";
  257. // Send the statement to the DBMS
  258. ResultSet result = stmt.executeQuery(sqlStatement);
  259. System.out.printf("%-15s %10s %10s %10s\n",
  260. "Team Name", "Win", "Lose",
  261. "Tie");
  262. // Display the contents of the result set
  263. // The result set will have 5 columns
  264. while(result.next())
  265. {
  266. System.out.printf("%-15s %10d %10d %10d\n",
  267. result.getString("TeamName"),
  268. result.getInt("Wins"),
  269. result.getInt("Losses"),
  270. result.getInt("Ties"));
  271. }
  272. }
  273. catch(Exception ex)
  274. {
  275. System.out.println("ERROR: " + ex.getMessage());
  276. }
  277. }
  278. /**
  279. The viewSchedule method retrieves and displays the
  280. teams and scores for all games.
  281. @param conn A connection to the database.
  282. */
  283. public static void viewSchedule(Connection conn)
  284. {
  285. try
  286. {
  287. // Create a Statement object
  288. Statement stmt = conn.createStatement();
  289. // TASK #2
  290. // Create a string with a SELECT statement
  291. String sqlStatement = "SELECT * FROM Games";
  292. // Send the statement to the DBMS
  293. ResultSet result = stmt.executeQuery(sqlStatement);
  294. // This is a suggested column headings display
  295. System.out.println("List of games and scores:");
  296. System.out.printf("%-6s %-20s %6s %-20s %6s\n",
  297. "GameID", "Home", "Score",
  298. "Visitor", "Score");
  299. // TASK #2
  300. // Use a while loop to display the result set
  301. while(result.next())
  302. {
  303. System.out.printf("%-6s %-20s %6s %-20s %6s\n",
  304. result.getInt("GameNumber"),
  305. result.getString("HomeTeam"),
  306. result.getInt("HomeTeamScore"),
  307. result.getString("VisitorTeam"),
  308. result.getInt("VisitorTeamScore"));
  309. }
  310. // The result set will have five columns
  311. }
  312. catch(Exception ex)
  313. {
  314. System.out.println("ERROR: " + ex.getMessage());
  315. }
  316. }
  317. /**
  318. The enterScores method allows user to enter scores for both
  319. teams. The method will update the Games table with the scores
  320. entered. It will also compare the scores to determine the
  321. winning and losing teams (or tie) and update the appropriate
  322. column in the Teams table for each team involved in the game.
  323. @param conn A connection to the database.
  324. */
  325. public static void enterScores(Connection conn)
  326. {
  327. Scanner keyboard = new Scanner(System.in);
  328. try
  329. {
  330. char ans;
  331. int gameNumber;
  332. String homeTeam;
  333. String visitingTeam;
  334. int score1;
  335. int score2;
  336. String sqlStatement;
  337. ResultSet result = null;
  338. Statement stmt = conn.createStatement();
  339. do
  340. {
  341. viewSchedule(conn);
  342. System.out.print("Enter the game ID: ");
  343. gameNumber = keyboard.nextInt();
  344. // TASK #4
  345. sqlStatement = "SELECT * FROM `games` WHERE gamenumber = " + gameNumber + "";
  346.  
  347. result = stmt.executeQuery(sqlStatement);
  348.  
  349.  
  350.  
  351. // Get the result set from a query that selects all
  352. // information for the gameNumber the user entered
  353. if(result.next())
  354. {
  355. homeTeam = result.getString("HomeTeam");
  356. visitingTeam = result.getString("VisitorTeam");
  357. System.out.print("Enter the score for the " +
  358. homeTeam);
  359. score1 = keyboard.nextInt();
  360. System.out.print("Enter the score for the " +
  361. visitingTeam);
  362. score2 = keyboard.nextInt();
  363. keyboard.nextLine();
  364. // TASK #4
  365.  
  366.  
  367.  
  368. sqlStatement = "UPDATE games SET homeTeamScore = " + score1 + ", visitorTeamScore = " + score2 + " WHERE gameNumber = " + gameNumber + "";
  369. stmt.execute(sqlStatement);
  370.  
  371. // Execute an update to the Games table to
  372. // store the score for each team of that
  373. // game number
  374. if(score1 < score2)
  375. {
  376. // TASK #4
  377.  
  378. System.out.println(visitingTeam+" WIN!");
  379. sqlStatement = "UPDATE teams SET wins = wins + 1 WHERE teamName = '" + visitingTeam + "'";
  380. stmt.execute(sqlStatement);
  381. sqlStatement = "UPDATE teams SET losses = losses + 1 WHERE teamName = '" + homeTeam + "'";
  382. stmt.execute(sqlStatement);
  383.  
  384.  
  385.  
  386. // Retrieve the number from the appropriate column
  387. // (wins, losses, or ties) for the home team,
  388. // increment, and update that team's record.
  389. // Do the same for the visiting team
  390. }
  391. else if(score2 < score1)
  392. {
  393. // TASK #4
  394. System.out.println(homeTeam+" WIN!");
  395. sqlStatement = "UPDATE teams SET wins = wins + 1 WHERE teamName = '" + homeTeam + "'";
  396. stmt.execute(sqlStatement);
  397. sqlStatement = "UPDATE teams SET losses = losses + 1 WHERE teamName = '" + visitingTeam + "'";
  398. stmt.execute(sqlStatement);
  399.  
  400. // Retrieve the number from the appropriate column
  401. // (wins, losses, or ties) for the home team,
  402. // increment, and update that team's record.
  403. // Do the same for the visiting team
  404. }
  405. else
  406. {
  407. // TASK #4
  408. sqlStatement = "UPDATE teams SET ties = ties + 1 WHERE teamName = '" + visitingTeam + "'";
  409. stmt.execute(sqlStatement);
  410. sqlStatement = "UPDATE teams SET ties = ties + 1 WHERE teamName = '" + homeTeam + "'";
  411. stmt.execute(sqlStatement);
  412. // Retrieve the number from the appropriate column
  413. // (wins, losses, or ties) for the home team,
  414. // increment, and update that team's record.
  415. // Do the same for the visiting team
  416. }
  417. }
  418. System.out.print("Do you want to enter another game: ");
  419. ans = keyboard.nextLine().charAt(0);
  420. } while(ans == 'Y'|| ans == 'y');
  421. }
  422. catch(Exception ex)
  423. {
  424. System.out.println("ERROR: " + ex.getMessage());
  425. }
  426. }
  427.  
  428.  
  429.  
  430. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement