Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- class Arena {
- private String venue;
- private String city;
- private String state;
- private int maxCapacity;
- private int yearOpened;
- private String teamName;
- private String sport;
- private String league;
- public Arena(String venue, String city, String state, int maxCapacity, int yearOpened, String teamName,
- String sport, String league) {
- this.venue = venue;
- this.city = city;
- this.state = state;
- this.maxCapacity = maxCapacity;
- this.yearOpened = yearOpened;
- this.teamName = teamName;
- this.sport = sport;
- this.league = league;
- }
- // Accessor for the aggregate Tenant field
- public String getAggregateTenant() {
- return this.teamName + " - " + this.sport + " - " + this.league;
- }
- }
- public class ArenasDB{
- // JDBC URL of PostgreSQL server
- private static final String URL = "jdbc:postgresql://localhost:5432/ArenasDB";
- // Create a connection to the database
- private static Connection connection;
- public static void main(String[] args) {
- try {
- // Load PostgreSQL JDBC driver
- Class.forName("org.postgresql.Driver");
- // Establish connection to PostgreSQL server
- connection = DriverManager.getConnection(URL);
- // Drop existing Arenas table
- dropArenasTable();
- // Create Arenas table
- createArenasTable();
- // Process information from the text file and insert into the database
- processAndInsertArenasFromFile("IndoorArenas.txt");
- // Display all rows and columns of the Arenas table
- displayArenasTable();
- // Close connection
- connection.close();
- } catch (ClassNotFoundException | SQLException | FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- // Method to drop existing Arenas table
- private static void dropArenasTable() throws SQLException {
- String query = "DROP TABLE IF EXISTS Arenas";
- try (Statement statement = connection.createStatement()) {
- statement.executeUpdate(query);
- System.out.println("Arenas table dropped successfully");
- }
- }
- // Method to create the Arenas table
- private static void createArenasTable() throws SQLException {
- String query = "CREATE TABLE Arenas (" + "Venue CHAR(45) PRIMARY KEY," + "City CHAR(15)," + "State CHAR(15),"
- + "MaxCapacity INT," + "YearOpened INT," + "TeamName CHAR(30)," + "Sport CHAR(10)," + "League CHAR(15)"
- + ")";
- try (Statement statement = connection.createStatement()) {
- statement.executeUpdate(query);
- System.out.println("Arenas table created successfully");
- }
- }
- // Method to process information from the text file and insert into the database
- private static void processAndInsertArenasFromFile(String filename)
- throws FileNotFoundException, SQLException {
- File file = new File(filename);
- try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
- String line;
- while ((line = reader.readLine()) != null) {
- String[] parts = line.split(",");
- String venue = parts[0].trim();
- String city = parts[1].trim();
- String state = parts[2].trim();
- int maxCapacity = Integer.parseInt(parts[3].trim());
- int yearOpened = Integer.parseInt(parts[4].trim());
- String teamName = parts[5].trim();
- String sport = parts[6].trim();
- String league = parts[7].trim();
- String query = "INSERT INTO Arenas VALUES ('" + venue + "','" + city + "','" + state + "',"
- + maxCapacity + "," + yearOpened + ",'" + teamName + "','" + sport + "','" + league + "')";
- try (Statement statement = connection.createStatement()) {
- statement.executeUpdate(query);
- }
- }
- System.out.println("Arenas data inserted into the database successfully");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // Method to display all rows and columns of the Arenas table
- private static void displayArenasTable() throws SQLException {
- String query = "SELECT * FROM Arenas";
- try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query)) {
- System.out.println("Arenas Table:");
- while (resultSet.next()) {
- System.out.println(resultSet.getString(1) + ", " + resultSet.getString(2) + ", "
- + resultSet.getString(3) + ", " + resultSet.getInt(4) + ", " + resultSet.getInt(5) + ", "
- + resultSet.getString(6) + ", " + resultSet.getString(7) + ", " + resultSet.getString(8));
- }
- }
- }
- }
Advertisement