Databases and MySQL COMP 342: Programming Methods 16 September 2008 Databases and MySQL
Database basics What is a database? A database consists of some number of tables. Each table consists of field names and entries. A field name consists of a textual name and data type. An entry consists of values for each field. Think of the fields as column headers, entries as rows in the table. Databases and MySQL 1 / 11
A sample Human Resources database Employees table: lastname:string firstname:string positionid:integer hiredate:date Smith Joe 37 12 Nov. 1987 Doe Jane 1 4 Dec. 1098 Danner Norman 37 1984 Positions table: id:integer title:string minsal:currency maxsal:currency 1 President 23, 000 1, 000, 000, 000 2 Vice-president 20, 000 1, 500, 000... 37 Peon 1 10 Databases and MySQL 2 / 11
Database basics Relational databases Relations databases are characterized by the relationships between entries in different tables. Example: can require that each ID be distinct in Positions. Example: can require that positionid field of each entry in Employees matches the id field of one of the entries in Positions. This is called a foreign-key constraint. Look at some tables in the Petstore database. Databases and MySQL 3 / 11
Structured Query Language Interact with many databases by executing Structured Query Language (SQL) commands. Commands for 1. Creating tables; 2. Modifying tables; 3. Extracting data from the tables. We will mostly be concerned with data extraction. Databases and MySQL 4 / 11
Structured Query Language Basic SQL query select <field-spec> from <table-spec> [where <cond>] <field-spec> is a field specification; e.g.: * for all columns; lastname, firstname, salary for specific columns. <table-spec> is the name of a table in the database. <cond> is a boolean condition used to limit which rows are returned. The query returns a new table: 1. Get rows from <table-spec> that satsify <cond>. 2. Delete fields not in <field-spec>. Databases and MySQL 5 / 11
Structured Query Language A simple query select firstname, lastname from Employees where positionid = 37 Returns firstname:string Joe Norman lastname:string Smith Danner Some examples from the Petstore database. Databases and MySQL 6 / 11
Structured Query Language A JOIN query If multiple tables are specified in the from clause, the tables are joined: there is one result row for each combination of rows from each of the tables. select firstname, lastname from Employees, Positions where employees.positionid = Positions.id and Positions.minSal > 15000 Returns firstname:string Jane lastname:string Doe Databases and MySQL 7 / 11
The JDBC API The Java Database Connectivity Framework Provides interfaces for managing and interacting with databases. For simple SQL queries << interface >> Connection +createstatement():statement +preparecall(statement:string):callablestatement Iterator over table produced by a query; starts just before first row. getxxx() methods get column entry according to column type. << interface >> Statement << interface >> ResultSet +executequery (sqlquery:string):resultset +getresultset():resultset +getmoreresults():boolean +getint(col:string):int +getstring(col:string):string +next():boolean +getmetadata():resultsetmetadata For complex SQL queries with parameters. << interface >> PreparedStatement +execute():boolean << interface >> ResultSetMetaData For stored procedures. << interface >> CallableStatement Meta-data about the table for which this ResultSet is an iterator. Databases and MySQL 8 / 11
The JDBC API The Connection interface Represents a connection to the database. Obtained from the DriverManager, which in turn depends on a database system-dependent class. Steps: 1. Load database system-dependent driver into JVM with Class.forName(). 2. Get Connection object from DriverManager with getconnection, which requires information such as hostname, port, connection protocol, etc. Databases and MySQL 9 / 11
The JDBC API The Statement interface Represents a statement (SQL query, stored procedure, etc.) to be executed by database. Obtained from Connection object with createstatement. Typically for simple SQL statements; for us, typically for simple SQL queries that return a single table. ResultSet executequery(string sqlquery): executes the stock SQL query given by argument, returns the results via a ResultSet object. Databases and MySQL 10 / 11
The JDBC API The ResultSet interface An iterator over the table of data produced by an SQL query. Starts just before first row of table. Advance to next row with next(). Get column values for current row with getxxx methods. Databases and MySQL 11 / 11