You are on page 1of 6

3/4/22

Database

CS314 Software
Engineering

The solution in this course uses a three-tier architecture, with the application
running on a client, services on a webserver, and a separate database server.

1
3/4/22

The server environment varies so your code will need to adapt to three different
run-time scenarios: black-bottle, csu-eid, and csu-guest/external.

Command Line
Interface
with SQL

2
3/4/22

Command line interface queries via mysql such as these will help you explore
the information available in the database without any coding.

# connect to the database from a shell


mysql -D cs314 -h faure --user=cs314-db --password=eiK5liet1uej
# once in SQL…
# see a list of tables
show tables;
# see the columns in a table (world replaces airports)
show columns from world;
show columns from region;
show columns from country;
show columns from continent;
# notice the column similarities between tables

The SELECT statement allows us to combine (join) information from multiple


tables into a single view for reporting purposes.

3
3/4/22

A query like this combines information from multiple tables and matches
information in multiples columns. London is not just a city in England.
SET @phrase="%london%";
# search query, more columns for plan query
SELECT world.name, world.municipality, region.name, country.name,
continent.name
FROM continents
INNER JOIN country ON continent.id = country.continent
INNER JOIN region ON country.id = region.iso_country
INNER JOIN world ON region.id = world.iso_region
WHERE (country.name LIKE @phrase
OR region.name LIKE @phrase
OR world.name LIKE @phrase
OR world.municipality LIKE @phrase)
LIMIT 100;

A query like this can help you filter the data to just the United States to provide
more meaningful results.
SET @country="United States";
SET @phrase="%london%";
# search query, more columns for plan query
SELECT world_airports.name, world_airports.municipality, region.name,
country.name, continents.name
FROM continents
INNER JOIN country ON continents.id = country.continent
INNER JOIN region ON country.id = region.iso_country
INNER JOIN world_airports ON region.id = world_airports.iso_region
WHERE (country.name LIKE @phrase
OR region.name LIKE @phrase
OR world_airports.name LIKE @phrase
OR world_airports.municipality LIKE @phrase)
AND country.name IN (@country)
LIMIT 100;

4
3/4/22

Java Interface
with SQL

The course Guide contains some simple, standalone Java code to query the
database and print the distinct values from a single column.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;

public class sqlGuide {

// connection information when using port forwarding from local host


private final static String DB_URL = "jdbc:mariadb://127.0.0.1:56247/cs314";

// shared user with read-only access


private final static String DB_USER = "cs314-db";
private final static String DB_PASSWORD = "eiK5liet1uej";

// SQL SELECT query statement


private final static String COLUMN = "id";
private final static String QUERY =
"SELECT DISTINCT "+ COLUMN +" FROM world ORDER BY "+ COLUMN +" ASC;";

10

5
3/4/22

The course Guide contains some simple, standalone Java code to query the
database and print the distinct values from a single column.

public static void main(String[] args) {


try (
// connect to the database and query
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement query = conn.createStatement();
ResultSet results = query.executeQuery(QUERY)
) {
// iterate through query results and print out the column values
int i = 0;
while (results.next()) {
System.out.printf("%6d %s\n", ++i, results.getString(COLUMN));
}
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
}

11

Running this sample code requires a few steps.

• Create a project in IntelliJ using Java version 8.


• Copy the source into a class file.
• Add the mariadb library to your project (see the Database guide)
• Set up port forwarding to faure (see the Port Forwarding guide)

12

You might also like