VIBE v5.1.6
Search Engine
Loading...
Searching...
No Matches
edu.usfca.cs272.DatabaseConnector Class Reference

Public Member Functions

 DatabaseConnector () throws FileNotFoundException, IOException
 
 DatabaseConnector (Path path) throws FileNotFoundException, IOException
 
void createTables () throws SQLException
 
Connection getConnection () throws SQLException
 
void insertSearch (Connection db, String query) throws SQLException
 
void insertResults (Connection db, String uri) throws SQLException
 
List< String > getTopFiveSearches (Connection db) throws SQLException
 
List< String > getTopFiveResults (Connection db) throws SQLException
 
void resetMetaData (Connection db) throws SQLException
 
Set< String > getTables (Connection db) throws SQLException
 
boolean testConnection ()
 

Static Public Member Functions

static Properties loadConfig (Path path) throws FileNotFoundException, IOException
 

Public Attributes

final String uri
 

Static Public Attributes

static final String INSERT
 
static final String GET_TOP_FIVE
 
static final String CREATE_TABLE
 

Detailed Description

This class is designed to test your database configuration. You need to have a database.properties file with username, password, database, and hostname. You must also have the tunnel to stargate.cs.usfca.edu running if you are off-campus.

Author
CS 272 Software Development (University of San Francisco)
Ravneet Singh Bhatia
Version
Spring 2024

Constructor & Destructor Documentation

◆ DatabaseConnector() [1/2]

edu.usfca.cs272.DatabaseConnector.DatabaseConnector ( ) throws FileNotFoundException, IOException

Creates a connector from a "database.properties" file located in the current working directory.

Exceptions
IOExceptionif unable to properly parse properties file
FileNotFoundExceptionif properties file not found

◆ DatabaseConnector() [2/2]

edu.usfca.cs272.DatabaseConnector.DatabaseConnector ( Path path) throws FileNotFoundException, IOException

Creates a connector from the provided database properties file.

Parameters
pathpath to the database properties file
Exceptions
IOExceptionif unable to properly parse properties file
FileNotFoundExceptionif properties file not found

Member Function Documentation

◆ createTables()

void edu.usfca.cs272.DatabaseConnector.createTables ( ) throws SQLException

Creates the required sql tables if they do not already exist.

Exceptions
SQLExceptionif there is a problem connecting to the database.

◆ getConnection()

Connection edu.usfca.cs272.DatabaseConnector.getConnection ( ) throws SQLException

Attempts to connect to database using loaded configuration.

Returns
database connection
Exceptions
SQLExceptionif unable to establish database connection

◆ getTables()

Set< String > edu.usfca.cs272.DatabaseConnector.getTables ( Connection db) throws SQLException

Opens a database connection and returns a set of found tables. Will return an empty set if there are no results.

Parameters
dbthe active database connection
Returns
set of tables
Exceptions
SQLExceptionif unable to execute SQL

◆ getTopFiveResults()

List< String > edu.usfca.cs272.DatabaseConnector.getTopFiveResults ( Connection db) throws SQLException

Executes the GET_TOP_FIVE query to get the 5 most visited results.

Parameters
dbthe active database connection.
Returns
list containing the top 5 most visited results.
Exceptions
SQLExceptionif unable to execute the query.s

◆ getTopFiveSearches()

List< String > edu.usfca.cs272.DatabaseConnector.getTopFiveSearches ( Connection db) throws SQLException

Executes the GET_TOP_FIVE query to get the 5 most searched queries.

Parameters
dbThe active database connection
Returns
list containing the top 5 queries.
Exceptions
SQLExceptionif unable to execute the query.

◆ insertResults()

void edu.usfca.cs272.DatabaseConnector.insertResults ( Connection db,
String uri ) throws SQLException

Executes INSERT for the results table.

Parameters
dbthe database connection
urithe uri to store.
Exceptions
SQLExceptionif unable to execute the query.

◆ insertSearch()

void edu.usfca.cs272.DatabaseConnector.insertSearch ( Connection db,
String query ) throws SQLException

Executes the INSERT for the queries table..

Parameters
dbThe active database connection
querythe query that has been searched.
Exceptions
SQLExceptionif unable to execute the query.

◆ loadConfig()

static Properties edu.usfca.cs272.DatabaseConnector.loadConfig ( Path path) throws FileNotFoundException, IOException
static

Attempts to load properties file with database configuration. Must include username, password, database, and hostname.

Parameters
pathpath to database properties file
Returns
database properties
Exceptions
IOExceptionif unable to properly parse properties file
FileNotFoundExceptionif properties file not found

◆ resetMetaData()

void edu.usfca.cs272.DatabaseConnector.resetMetaData ( Connection db) throws SQLException

deletes everything from the queries table.

Parameters
dbThe active database connection
Exceptions
SQLExceptionif unable to execute the query.

◆ testConnection()

boolean edu.usfca.cs272.DatabaseConnector.testConnection ( )

Opens a database connection, executes a simple statement, and closes the database connection.

Returns
true if all operations successful

Member Data Documentation

◆ CREATE_TABLE

final String edu.usfca.cs272.DatabaseConnector.CREATE_TABLE
static
Initial value:
=
"create table if not exists %s( %s varchar(255) primary key , count int not null);"

◆ GET_TOP_FIVE

final String edu.usfca.cs272.DatabaseConnector.GET_TOP_FIVE
static
Initial value:
=
"select %s, count from %s order by count desc limit 5;"

SQL query to get the top 5 queries searched in the server.

◆ INSERT

final String edu.usfca.cs272.DatabaseConnector.INSERT
static
Initial value:
=
"insert into %s (%s, count) values (?, 1) on duplicate key update count = count + 1;"

SQL query to insert into the queries table every time a query is called in the server. if a query is seen for the first time, the count is set to 1 if a query has already been seen, the count is incremented.

◆ uri

final String edu.usfca.cs272.DatabaseConnector.uri

URI to use when connecting to database. Should be in the format: jdbc:subprotocol://hostname/database


The documentation for this class was generated from the following file: