Guest User

Untitled

a guest
Mar 2nd, 2024
91
0
Never
5
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. import java.sql.*;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.util.ArrayList;
  13.  
  14. class Arena {
  15. private String venue;
  16. private String city;
  17. private String state;
  18. private int maxCapacity;
  19. private int yearOpened;
  20. private String teamName;
  21. private String sport;
  22. private String league;
  23.  
  24. public Arena(String venue, String city, String state, int maxCapacity, int yearOpened, String teamName,
  25. String sport, String league) {
  26. this.venue = venue;
  27. this.city = city;
  28. this.state = state;
  29. this.maxCapacity = maxCapacity;
  30. this.yearOpened = yearOpened;
  31. this.teamName = teamName;
  32. this.sport = sport;
  33. this.league = league;
  34. }
  35.  
  36. // Accessor for the aggregate Tenant field
  37. public String getAggregateTenant() {
  38. return this.teamName + " - " + this.sport + " - " + this.league;
  39. }
  40.  
  41. }
  42.  
  43. public class ArenasDB{
  44. // JDBC URL of PostgreSQL server
  45. private static final String URL = "jdbc:postgresql://localhost:5432/ArenasDB";
  46.  
  47. // Create a connection to the database
  48. private static Connection connection;
  49.  
  50. public static void main(String[] args) {
  51. try {
  52. // Load PostgreSQL JDBC driver
  53. Class.forName("org.postgresql.Driver");
  54.  
  55. // Establish connection to PostgreSQL server
  56. connection = DriverManager.getConnection(URL);
  57.  
  58. // Drop existing Arenas table
  59. dropArenasTable();
  60.  
  61. // Create Arenas table
  62. createArenasTable();
  63.  
  64. // Process information from the text file and insert into the database
  65. processAndInsertArenasFromFile("IndoorArenas.txt");
  66.  
  67. // Display all rows and columns of the Arenas table
  68. displayArenasTable();
  69.  
  70. // Close connection
  71. connection.close();
  72. } catch (ClassNotFoundException | SQLException | FileNotFoundException e) {
  73. e.printStackTrace();
  74. }
  75. }
  76.  
  77. // Method to drop existing Arenas table
  78. private static void dropArenasTable() throws SQLException {
  79. String query = "DROP TABLE IF EXISTS Arenas";
  80. try (Statement statement = connection.createStatement()) {
  81. statement.executeUpdate(query);
  82. System.out.println("Arenas table dropped successfully");
  83. }
  84. }
  85.  
  86. // Method to create the Arenas table
  87. private static void createArenasTable() throws SQLException {
  88. String query = "CREATE TABLE Arenas (" + "Venue CHAR(45) PRIMARY KEY," + "City CHAR(15)," + "State CHAR(15),"
  89. + "MaxCapacity INT," + "YearOpened INT," + "TeamName CHAR(30)," + "Sport CHAR(10)," + "League CHAR(15)"
  90. + ")";
  91. try (Statement statement = connection.createStatement()) {
  92. statement.executeUpdate(query);
  93. System.out.println("Arenas table created successfully");
  94. }
  95. }
  96.  
  97. // Method to process information from the text file and insert into the database
  98. private static void processAndInsertArenasFromFile(String filename)
  99. throws FileNotFoundException, SQLException {
  100. File file = new File(filename);
  101. try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
  102. String line;
  103. while ((line = reader.readLine()) != null) {
  104. String[] parts = line.split(",");
  105. String venue = parts[0].trim();
  106. String city = parts[1].trim();
  107. String state = parts[2].trim();
  108. int maxCapacity = Integer.parseInt(parts[3].trim());
  109. int yearOpened = Integer.parseInt(parts[4].trim());
  110. String teamName = parts[5].trim();
  111. String sport = parts[6].trim();
  112. String league = parts[7].trim();
  113.  
  114. String query = "INSERT INTO Arenas VALUES ('" + venue + "','" + city + "','" + state + "',"
  115. + maxCapacity + "," + yearOpened + ",'" + teamName + "','" + sport + "','" + league + "')";
  116. try (Statement statement = connection.createStatement()) {
  117. statement.executeUpdate(query);
  118. }
  119. }
  120. System.out.println("Arenas data inserted into the database successfully");
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. }
  124. }
  125.  
  126. // Method to display all rows and columns of the Arenas table
  127. private static void displayArenasTable() throws SQLException {
  128. String query = "SELECT * FROM Arenas";
  129. try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query)) {
  130. System.out.println("Arenas Table:");
  131. while (resultSet.next()) {
  132. System.out.println(resultSet.getString(1) + ", " + resultSet.getString(2) + ", "
  133. + resultSet.getString(3) + ", " + resultSet.getInt(4) + ", " + resultSet.getInt(5) + ", "
  134. + resultSet.getString(6) + ", " + resultSet.getString(7) + ", " + resultSet.getString(8));
  135. }
  136. }
  137. }
  138. }
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment