Application Development 
SQL 
References 
Database Systems 
Application Development 
H. Turgut Uyar ¸Sule ¨O˘g¨ud¨uc¨u 
20...
Application Development 
SQL 
References 
License 

c 2002-2014 T. Uyar, S¸. O¨g˘u¨du¨cu¨ 
You are free to: 
Share – copy ...
Application Development 
SQL 
References 
Topics 
1 Application Development 
Introduction 
Embedded SQL 
ODBC 
JDBC 
2 SQL...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Topics 
1 Application Development 
Intro...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Introduction 
using the database languag...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Introduction 
using the database languag...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Program Structure 
connect 
server, data...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Program Structure 
connect 
server, data...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Program Structure 
connect 
server, data...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Approaches 
application programming inte...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Application Programming Interface 
using...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
API Example: PostgreSQL - C 
#include <l...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
API Example: Connection 
/* PGconn *conn...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
API Example: Query Execution 
sprintf(qu...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
API Example: Result Set 
for (i = 0; i <...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Topics 
1 Application Development 
Intro...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Embedded SQL 
mark SQL statements within...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Embedded SQL 
mark SQL statements within...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Embedded SQL 
skip Embedded SQL 
H. Turg...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Embedded SQL 
variable sharing 
EXEC SQL...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Embedded SQL 
variable sharing 
EXEC SQL...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Query Results 
cursors for iterating ove...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Embedded SQL Example: Connect 
EXEC SQL ...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Embedded SQL Example: Cursor 
scanf("%d"...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Embedded SQL Example: Query 
EXEC SQL WH...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Topics 
1 Application Development 
Intro...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
ODBC 
ODBC: Open DataBase Connectivity 
...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
ODBC Architecture 
application 
driver m...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
ODBC Example: PHP 
$conn = odbc_connect(...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
ODBC Example: PHP 
echo "<table>n"; 
whi...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Topics 
1 Application Development 
Intro...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC 
JDBC: Java DataBase Connectivity 
...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Drivers 
Type I: bridges 
translate...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Drivers 
Type I: bridges 
translate...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Drivers 
Type I: bridges 
translate...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Drivers 
Type I: bridges 
translate...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Flow 
get a connection object: 
Con...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Flow 
get a connection object: 
Con...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Flow 
get a connection object: 
Con...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Flow 
get a connection object: 
Con...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Flow 
get a connection object: 
Con...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Processing Results 
ResultSet is an iter...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Data Type Conversions 
SQL type Java cla...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Example: Driver 
try { 
Class.forNa...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Example: Connect 
try { 
conn = Dri...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Example: Insert 
query = "INSERT IN...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Security 
don’t trust inputs from outsid...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Little Bobby Tables 
http://xkcd.com/327...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Prepared Statements 
prepared statements...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Example: Prepared Statement 
query ...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Example: Result Set 
query = String...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Example: Delete 
query = "DELETE FR...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Example: Update 
query = "UPDATE MO...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
JDBC Example: Close Resources 
Statement...
Application Development 
SQL 
References 
Introduction 
Embedded SQL 
ODBC 
JDBC 
Auto-Generated Identity Values 
query = ...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Topics 
1 Application Development 
Introducti...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Stored Procedures 
implementing functionality...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Stored Procedures 
implementing functionality...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Stored Procedures 
implementing functionality...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Stored Procedures 
implementing functionality...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Stored Procedures 
implementing functionality...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Functions 
creating a function 
CREATE FUNCTI...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
SQL Function Example 
calculating new score: ...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Triggers 
trigger: a function that will be au...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Triggers 
trigger: a function that will be au...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Trigger Example 
let SCORE * VOTES be kept in...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Trigger Example 
calculate POINTS automatical...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Topics 
1 Application Development 
Introducti...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Views 
presenting a derived table like a base...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Views 
presenting a derived table like a base...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
View Example 
identifiers, titles and years o...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Updating Views 
any change will have to perfo...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Updating Views 
any change will have to perfo...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
View Rule Example 
modify the title of a new ...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
View Rule Example 
modify the title of a new ...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Topics 
1 Application Development 
Introducti...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Indexes 
some operations require sorting: 
OR...
Application Development 
SQL 
References 
Stored Procedures 
Views 
Indexes 
Indexes 
some operations require sorting: 
OR...
Application Development 
SQL 
References 
References 
Required Reading: Date 
Chapter 4: An Introduction to SQL 
4.6. Embe...
Upcoming SlideShare
Loading in …5
×

Database Systems - Application Development

1,288 views
1,147 views

Published on

Bindings to general purpose programming languages, embedded SQL, ODBC, JDBC, stored procedures, triggers, views.

Published in: Education, Technology
4 Comments
1 Like
Statistics
Notes
No Downloads
Views
Total views
1,288
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
75
Comments
4
Likes
1
Embeds 0
No embeds

No notes for slide

Database Systems - Application Development

  1. 1. Application Development SQL References Database Systems Application Development H. Turgut Uyar ¸Sule ¨O˘g¨ud¨uc¨u 2002-2014 H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  2. 2. Application Development SQL References License c 2002-2014 T. Uyar, S¸. O¨g˘u¨du¨cu¨ You are free to: Share – copy and redistribute the material in any medium or format Adapt – remix, transform, and build upon the material Under the following terms: Attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. NonCommercial – You may not use the material for commercial purposes. ShareAlike – If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. For more information: https://creativecommons.org/licenses/by-nc-sa/4.0/ Read the full license: https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  3. 3. Application Development SQL References Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Stored Procedures Views Indexes H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  4. 4. Application Development SQL References Introduction Embedded SQL ODBC JDBC Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Stored Procedures Views Indexes H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  5. 5. Application Development SQL References Introduction Embedded SQL ODBC JDBC Introduction using the database language in conjunction with a general-purpose programming language general-purpose language: host language mismatch between SQL and the host language: sets in SQL objects and iteration constructs in the host language H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  6. 6. Application Development SQL References Introduction Embedded SQL ODBC JDBC Introduction using the database language in conjunction with a general-purpose programming language general-purpose language: host language mismatch between SQL and the host language: sets in SQL objects and iteration constructs in the host language H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  7. 7. Application Development SQL References Introduction Embedded SQL ODBC JDBC Program Structure connect server, database, username, password run statements as needed: query operations return result sets ! iterate over rows update operations return number of affected rows disconnect H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  8. 8. Application Development SQL References Introduction Embedded SQL ODBC JDBC Program Structure connect server, database, username, password run statements as needed: query operations return result sets ! iterate over rows update operations return number of affected rows disconnect H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  9. 9. Application Development SQL References Introduction Embedded SQL ODBC JDBC Program Structure connect server, database, username, password run statements as needed: query operations return result sets ! iterate over rows update operations return number of affected rows disconnect H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  10. 10. Application Development SQL References Introduction Embedded SQL ODBC JDBC Approaches application programming interface (API) embedded SQL ODBC language standard interfaces H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  11. 11. Application Development SQL References Introduction Embedded SQL ODBC JDBC Application Programming Interface using the library functions of the SQL server pros: fast cons: specific to SQL product H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  12. 12. Application Development SQL References Introduction Embedded SQL ODBC JDBC API Example: PostgreSQL - C #include <libpq-fe.h> int main(int argc, char *argv[]) { /* connect */ /* execute query */ /* disconnect */ } H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  13. 13. Application Development SQL References Introduction Embedded SQL ODBC JDBC API Example: Connection /* PGconn *conn; */ conn = PQconnectdb("host=localhost dbname=imdb" " user=itucs password=itucs"); if (PQstatus(conn) == CONNECTION_BAD) { fprintf(stderr, "Connection failed.n"); exit(1); } /* execute query */ PQfinish(conn); H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  14. 14. Application Development SQL References Introduction Embedded SQL ODBC JDBC API Example: Query Execution sprintf(query, "SELECT TITLE, SCORE" " FROM MOVIE WHERE (YR = %d)", year); /* PGresult *result; */ result = PQexec(conn, query); if (PQresultStatus(result) != PGRES_TUPLES_OK) { fprintf(stderr, "Query failed.n"); PQclear(result); PQfinish(conn); exit(1); } H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  15. 15. Application Development SQL References Introduction Embedded SQL ODBC JDBC API Example: Result Set for (i = 0; i < PQntuples(result); i++) { title = PQgetvalue(result, i, 0); score = PQgetvalue(result, i, 1); ... } PQclear(result); H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  16. 16. Application Development SQL References Introduction Embedded SQL ODBC JDBC Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Stored Procedures Views Indexes H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  17. 17. Application Development SQL References Introduction Embedded SQL ODBC JDBC Embedded SQL mark SQL statements within host language code: EXEC SQL preprocessor: embedded SQL directives ! API calls pros: fast, standard cons: difficult, does not support most languages H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  18. 18. Application Development SQL References Introduction Embedded SQL ODBC JDBC Embedded SQL mark SQL statements within host language code: EXEC SQL preprocessor: embedded SQL directives ! API calls pros: fast, standard cons: difficult, does not support most languages H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  19. 19. Application Development SQL References Introduction Embedded SQL ODBC JDBC Embedded SQL skip Embedded SQL H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  20. 20. Application Development SQL References Introduction Embedded SQL ODBC JDBC Embedded SQL variable sharing EXEC SQL BEGIN DECLARE SECTION; -- shared variables EXEC SQL END DECLARE SECTION; error control EXEC SQL WHENEVER { SQLERROR | SQLWARNING | NOT FOUND } { STOP | CONTINUE | DO command | GOTO label } H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  21. 21. Application Development SQL References Introduction Embedded SQL ODBC JDBC Embedded SQL variable sharing EXEC SQL BEGIN DECLARE SECTION; -- shared variables EXEC SQL END DECLARE SECTION; error control EXEC SQL WHENEVER { SQLERROR | SQLWARNING | NOT FOUND } { STOP | CONTINUE | DO command | GOTO label } H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  22. 22. Application Development SQL References Introduction Embedded SQL ODBC JDBC Query Results cursors for iterating over query results EXEC SQL DECLARE cursor_name CURSOR FOR SELECT ...; EXEC SQL OPEN cursor_name; EXEC SQL FETCH IN cursor_name INTO variables; EXEC SQL CLOSE cursor_name; query is executed when cursor is opened H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  23. 23. Application Development SQL References Introduction Embedded SQL ODBC JDBC Embedded SQL Example: Connect EXEC SQL BEGIN DECLARE SECTION; int year; char *title = NULL, *score = NULL; EXEC SQL END DECLARE SECTION; EXEC SQL CONNECT TO imdb USER itucs IDENTIFIED BY itucs; /* process query */ EXEC SQL DISCONNECT; H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  24. 24. Application Development SQL References Introduction Embedded SQL ODBC JDBC Embedded SQL Example: Cursor scanf("%d", &year); EXEC SQL DECLARE c_query CURSOR FOR SELECT TITLE, SCORE FROM MOVIE WHERE (YR = :year); EXEC SQL OPEN c_query; /* execute query */ EXEC SQL CLOSE c_query; EXEC SQL COMMIT; H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  25. 25. Application Development SQL References Introduction Embedded SQL ODBC JDBC Embedded SQL Example: Query EXEC SQL WHENEVER NOT FOUND DO break; while (1) { EXEC SQL FETCH c_query INTO :title, :score; ... } H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  26. 26. Application Development SQL References Introduction Embedded SQL ODBC JDBC Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Stored Procedures Views Indexes H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  27. 27. Application Development SQL References Introduction Embedded SQL ODBC JDBC ODBC ODBC: Open DataBase Connectivity a service layer between the application and the server pros: standard cons: slow H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  28. 28. Application Development SQL References Introduction Embedded SQL ODBC JDBC ODBC Architecture application driver manager registers the ODBC drivers transfers requests from application to driver driver translates and transfers requests to data source data source processes instructions from the driver H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  29. 29. Application Development SQL References Introduction Embedded SQL ODBC JDBC ODBC Example: PHP $conn = odbc_connect("imdb", "itucs", "itucs"); $query = "SELECT TITLE, SCORE FROM MOVIE" . " WHERE (YR = " . $year . ")"; $result = odbc_exec($conn, $query); /* process the result set */ odbc_close($conn); H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  30. 30. Application Development SQL References Introduction Embedded SQL ODBC JDBC ODBC Example: PHP echo "<table>n"; while (odbc_fetch_row($result)) { $title = odbc_result($result, "title"); $score = odbc_result($result, "score"); echo "<tr>n"; echo " <td>$title</td>n"; echo " <td>$score</td>n"; echo "</tr>n"; } echo "</table>n"; H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  31. 31. Application Development SQL References Introduction Embedded SQL ODBC JDBC Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Stored Procedures Views Indexes H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  32. 32. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC JDBC: Java DataBase Connectivity same architectural concepts as in ODBC H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  33. 33. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Drivers Type I: bridges translate into non-native calls (for example ODBC) Type II: direct translation via non-Java driver translate into API of data source (for example C++) Type III: network bridges connect to middleware server for translating into API of data source Type IV: direct translation via Java driver communicate with DBMS through Java sockets H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  34. 34. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Drivers Type I: bridges translate into non-native calls (for example ODBC) Type II: direct translation via non-Java driver translate into API of data source (for example C++) Type III: network bridges connect to middleware server for translating into API of data source Type IV: direct translation via Java driver communicate with DBMS through Java sockets H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  35. 35. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Drivers Type I: bridges translate into non-native calls (for example ODBC) Type II: direct translation via non-Java driver translate into API of data source (for example C++) Type III: network bridges connect to middleware server for translating into API of data source Type IV: direct translation via Java driver communicate with DBMS through Java sockets H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  36. 36. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Drivers Type I: bridges translate into non-native calls (for example ODBC) Type II: direct translation via non-Java driver translate into API of data source (for example C++) Type III: network bridges connect to middleware server for translating into API of data source Type IV: direct translation via Java driver communicate with DBMS through Java sockets H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  37. 37. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Flow get a connection object: Connection conn = DriverManager.getConnection(); create a statement object on the connection Statement stmt = conn.createStatement(); execute the query select: ResultSet results = stmt.executeQuery(query); insert, update, delete: stmt.executeUpdate(query); process the results close resources which are no longer needed (result sets, statements, connections) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  38. 38. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Flow get a connection object: Connection conn = DriverManager.getConnection(); create a statement object on the connection Statement stmt = conn.createStatement(); execute the query select: ResultSet results = stmt.executeQuery(query); insert, update, delete: stmt.executeUpdate(query); process the results close resources which are no longer needed (result sets, statements, connections) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  39. 39. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Flow get a connection object: Connection conn = DriverManager.getConnection(); create a statement object on the connection Statement stmt = conn.createStatement(); execute the query select: ResultSet results = stmt.executeQuery(query); insert, update, delete: stmt.executeUpdate(query); process the results close resources which are no longer needed (result sets, statements, connections) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  40. 40. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Flow get a connection object: Connection conn = DriverManager.getConnection(); create a statement object on the connection Statement stmt = conn.createStatement(); execute the query select: ResultSet results = stmt.executeQuery(query); insert, update, delete: stmt.executeUpdate(query); process the results close resources which are no longer needed (result sets, statements, connections) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  41. 41. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Flow get a connection object: Connection conn = DriverManager.getConnection(); create a statement object on the connection Statement stmt = conn.createStatement(); execute the query select: ResultSet results = stmt.executeQuery(query); insert, update, delete: stmt.executeUpdate(query); process the results close resources which are no longer needed (result sets, statements, connections) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  42. 42. Application Development SQL References Introduction Embedded SQL ODBC JDBC Processing Results ResultSet is an iterator whether there are more rows: .hasNext() proceed to the next row: .next() convert and transfer data in the row to variables by column name: .getXXX(name) by column order: .getXXX(order) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  43. 43. Application Development SQL References Introduction Embedded SQL ODBC JDBC Data Type Conversions SQL type Java class ResultSet method BIT Boolean .getBoolean() CHAR String .getString() VARCHAR String .getString() FLOAT Float .getDouble() INTEGER Integer .getInt() DATE java.sql.Date .getDate() TIME java.sql.Time .getTime() TIMESTAMP java.sql.TimeStamp .getTimestamp() H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  44. 44. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Example: Driver try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { // PostgreSQL driver not installed } H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  45. 45. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Example: Connect try { conn = DriverManager.getConnection( "jdbc:postgresql:imdb", "itucs", "itucs" ); } catch (SQLException e) { // connection error } H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  46. 46. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Example: Insert query = "INSERT INTO MOVIE (TITLE, YR)" + " VALUES (’Casablanca’, 1942)"; stmt = conn.createStatement(); stmt.executeUpdate(query); stmt.close(); H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  47. 47. Application Development SQL References Introduction Embedded SQL ODBC JDBC Security don’t trust inputs from outside sources Example name = ...; // get from outside source query = "INSERT INTO Students (NAME)" + " VALUES (’" + name + "’)"; stmt = conn.createStatement(); stmt.executeUpdate(query); stmt.close(); H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  48. 48. Application Development SQL References Introduction Embedded SQL ODBC JDBC Little Bobby Tables http://xkcd.com/327/ H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  49. 49. Application Development SQL References Introduction Embedded SQL ODBC JDBC Prepared Statements prepared statements can be reused by changing parameter values creating: PreparedStatement stmt = conn.prepareStatement(query); placeholder for parameters in query: ? values must be set before executing: stmt.setXXX(order, value); executing: ResultSet results = stmt.executeQuery(); stmt.executeUpdate(); H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  50. 50. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Example: Prepared Statement query = "INSERT INTO MOVIE (TITLE, YR) VALUES (?, ?)"; stmt = conn.prepareStatement(query); for (Movie movie : getMovies()) { stmt.setString(1, movie.getTitle()); stmt.setInt(2, movie.getYear()); stmt.executeUpdate(); } stmt.close(); H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  51. 51. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Example: Result Set query = String.format( "SELECT TITLE FROM MOVIE WHERE (YR = %d)", year ); stmt = conn.createStatement(); results = stmt.executeQuery(query); while (results.next()) { title = results.getString("TITLE"); System.out.println("Title: " + title), } results.close(); stmt.close(); H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  52. 52. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Example: Delete query = "DELETE FROM MOVIE WHERE (ID = ?)"; stmt = conn.prepareStatement(query); stmt.setInt(1, movie.getId()); stmt.executeUpdate(); stmt.close(); H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  53. 53. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Example: Update query = "UPDATE MOVIE SET YR = ? WHERE (ID = ?)"; stmt = conn.prepareStatement(query); stmt.setInt(1, movie.getYear()); stmt.setInt(2, movie.getId()); stmt.executeUpdate(); stmt.close(); H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  54. 54. Application Development SQL References Introduction Embedded SQL ODBC JDBC JDBC Example: Close Resources Statement stmt = ...; ResultSet results = ...; try { results = stmt.executeQuery(query); ... } catch (SQLException e) { ... } finally { results.close(); stmt.close(); } H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  55. 55. Application Development SQL References Introduction Embedded SQL ODBC JDBC Auto-Generated Identity Values query = "INSERT INTO ... "; stmt = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); stmt.executeUpdate(); ResultSet ids = stmt.getGeneratedKeys(); // assuming there is one and only one result ids.next(); int id = ids.getInt(1); H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  56. 56. Application Development SQL References Stored Procedures Views Indexes Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Stored Procedures Views Indexes H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  57. 57. Application Development SQL References Stored Procedures Views Indexes Stored Procedures implementing functionality in the database server languages: SQL, PL/SQL, C, ... not portable not scalable database servers are not optimized for business logic not recommended ! implement business logic on the application server H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  58. 58. Application Development SQL References Stored Procedures Views Indexes Stored Procedures implementing functionality in the database server languages: SQL, PL/SQL, C, ... not portable not scalable database servers are not optimized for business logic not recommended ! implement business logic on the application server H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  59. 59. Application Development SQL References Stored Procedures Views Indexes Stored Procedures implementing functionality in the database server languages: SQL, PL/SQL, C, ... not portable not scalable database servers are not optimized for business logic not recommended ! implement business logic on the application server H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  60. 60. Application Development SQL References Stored Procedures Views Indexes Stored Procedures implementing functionality in the database server languages: SQL, PL/SQL, C, ... not portable not scalable database servers are not optimized for business logic not recommended ! implement business logic on the application server H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  61. 61. Application Development SQL References Stored Procedures Views Indexes Stored Procedures implementing functionality in the database server languages: SQL, PL/SQL, C, ... not portable not scalable database servers are not optimized for business logic not recommended ! implement business logic on the application server H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  62. 62. Application Development SQL References Stored Procedures Views Indexes Functions creating a function CREATE FUNCTION function_name(parameter_type [, ...]) RETURNS return_type AS function_body LANGUAGE language_name first parameter $1, second parameter $2, ... H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  63. 63. Application Development SQL References Stored Procedures Views Indexes SQL Function Example calculating new score: $1: old score, $2: old votes, $3: new vote CREATE FUNCTION NEW_SCORE(float, int, int) RETURNS float AS ’SELECT ($1 * $2 + $3) / ($2 + 1);’ LANGUAGE ’sql’ H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  64. 64. Application Development SQL References Stored Procedures Views Indexes Triggers trigger: a function that will be automatically invoked on an event can be useful for maintaining integrity creating a trigger: CREATE TRIGGER trigger_name { BEFORE | AFTER } { event [ OR ... ] } ON table_name [ FOR [ EACH ] { ROW | STATEMENT } ] EXECUTE PROCEDURE function_name(...) PL/pgSQL: old: tuple before the operation new: tuple after the operation H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  65. 65. Application Development SQL References Stored Procedures Views Indexes Triggers trigger: a function that will be automatically invoked on an event can be useful for maintaining integrity creating a trigger: CREATE TRIGGER trigger_name { BEFORE | AFTER } { event [ OR ... ] } ON table_name [ FOR [ EACH ] { ROW | STATEMENT } ] EXECUTE PROCEDURE function_name(...) PL/pgSQL: old: tuple before the operation new: tuple after the operation H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  66. 66. Application Development SQL References Stored Procedures Views Indexes Trigger Example let SCORE * VOTES be kept in the POINTS column CREATE FUNCTION UPDATE_MOVIE_POINTS() RETURNS opaque AS ’BEGIN new.POINTS = new.SCORE * new.VOTES; RETURN new; END;’ LANGUAGE ’plpgsql’ H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  67. 67. Application Development SQL References Stored Procedures Views Indexes Trigger Example calculate POINTS automatically on updates CREATE TRIGGER UPDATE_MOVIE BEFORE INSERT OR UPDATE ON MOVIE FOR EACH ROW EXECUTE PROCEDURE UPDATE_MOVIE_POINTS() H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  68. 68. Application Development SQL References Stored Procedures Views Indexes Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Stored Procedures Views Indexes H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  69. 69. Application Development SQL References Stored Procedures Views Indexes Views presenting a derived table like a base table: view isolating users and application programs from changes in database structure creating a view: CREATE VIEW view_name AS SELECT ... SELECT will be executed every time the view is used H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  70. 70. Application Development SQL References Stored Procedures Views Indexes Views presenting a derived table like a base table: view isolating users and application programs from changes in database structure creating a view: CREATE VIEW view_name AS SELECT ... SELECT will be executed every time the view is used H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  71. 71. Application Development SQL References Stored Procedures Views Indexes View Example identifiers, titles and years of new movies CREATE VIEW NEW_MOVIE AS SELECT ID, TITLE, YR FROM MOVIE WHERE (YR > 1995) SELECT * FROM NEW_MOVIE H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  72. 72. Application Development SQL References Stored Procedures Views Indexes Updating Views any change will have to performed on the base tables rules need to be defined creating a rule: CREATE RULE rule_name AS ON event TO view_name [ WHERE condition ] DO [ INSTEAD ] sql_statement H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  73. 73. Application Development SQL References Stored Procedures Views Indexes Updating Views any change will have to performed on the base tables rules need to be defined creating a rule: CREATE RULE rule_name AS ON event TO view_name [ WHERE condition ] DO [ INSTEAD ] sql_statement H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  74. 74. Application Development SQL References Stored Procedures Views Indexes View Rule Example modify the title of a new movie UPDATE NEW_MOVIE SET TITLE = ... WHERE (ID = ...) rule for updating the base table CREATE RULE UPDATE_TITLE AS ON UPDATE TO NEW_MOVIE DO INSTEAD UPDATE MOVIE SET TITLE = new.TITLE WHERE (ID = old.ID) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  75. 75. Application Development SQL References Stored Procedures Views Indexes View Rule Example modify the title of a new movie UPDATE NEW_MOVIE SET TITLE = ... WHERE (ID = ...) rule for updating the base table CREATE RULE UPDATE_TITLE AS ON UPDATE TO NEW_MOVIE DO INSTEAD UPDATE MOVIE SET TITLE = new.TITLE WHERE (ID = old.ID) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  76. 76. Application Development SQL References Stored Procedures Views Indexes Topics 1 Application Development Introduction Embedded SQL ODBC JDBC 2 SQL Stored Procedures Views Indexes H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  77. 77. Application Development SQL References Stored Procedures Views Indexes Indexes some operations require sorting: ORDER BY, DISTINCT, GROUP BY, UNION, ... indexes speed up queries slow down inserts and updates creating an index CREATE [ UNIQUE ] INDEX index_name ON table_name(column_name [, ...]) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  78. 78. Application Development SQL References Stored Procedures Views Indexes Indexes some operations require sorting: ORDER BY, DISTINCT, GROUP BY, UNION, ... indexes speed up queries slow down inserts and updates creating an index CREATE [ UNIQUE ] INDEX index_name ON table_name(column_name [, ...]) H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems
  79. 79. Application Development SQL References References Required Reading: Date Chapter 4: An Introduction to SQL 4.6. Embedded SQL Chapter 9: Integrity 9.11. Triggers (a Digression) Chapter 10: Views Supplementary Reference: Ramakrishnan, Gehrke Chapter 6: Database Application Development H. Turgut Uyar, ¸Sule ¨O˘g¨ud¨uc¨u Database Systems

×