Week 22
CST-363 Week 6
This is our sixth week in CST-363, also known as Introduction to Database Systems.
Summary
This week we read about integrating SQL into programs and other languages. We learned about some of the differences between SQL and other languages and some of the ways to integrate SQL into a program, such as Embedded SQL and APIs. Then, we learned how to use an SQL API for Java called JDBC.
Vocabulary
Programming Languages
Imperative Languages: Contain control flow statements that determine the execution order of program steps. Two popular types of imperative languages are:
- Procedural Languages: Composed of procedures, also called functions or subroutines. Most languages developed prior to 1990 are procedural. Ex: C and COBOL.
- Object-Oriented Languages: Organize code into classes. A class combines variables and procedures into a single construct. Most languages developed since 1990 are object-oriented. Ex: Java, Python, and C++.
Declarative Languages: Do not contain control flow statements. Each statement declares what result is desired, using logical expressions, rather than how the result is processed. SQL and HTML are leading examples of declarative languages.
Database Programming: Building applications with both SQL and a general-purpose language. Database programming presents to challenges:
- Syntax Gap: The syntax of SQL and most other languages are quite different.
- Paradigm Gap: Query processing is fundamentally different in SQL and procedural or object-oriented languages.
To bridge the syntax and paradigm gaps, three database programming techniques have emerged:
- Embedded SQL: Codes SQL statements directly in a program written in another language.
- Procedural SQL: Extends the SQL language with control flow statements, creating a new programming language.
- API: A library of procedures or classes.
Embedded SQL
Host Language: Embedded SQL statements appear in programs written in another language, called the host language.
Precompiler: Translates SQL statements into host language statements and function calls.
Shared Variable: A host language variable that appears in SQL statements.
Cursor: An embedded SQL variable that identifies an individual row of a result table. Cursors are necessary to process queries that return multiple rows.
Dynamic SQL: Embedded SQL statements that are generated at run-time.
Static SQL: Embedded SQL statements that are fixed in program code.
Application Programming Interfaces (APIs)
Database API implementations typically contain two software layers:
- Driver: Connects directly to the database.
- Driver Manager: Connects the application to the drivers.
Database Programming with Java
Connection: An object of the Connection interface that is created with:
- DiverManager.getConnection(): Specifies the database server address, database name, login username, and password.
connection.close(): Releases the connection.
Statement Interface: Defines and executes SQL queries.
- createStatement(): Creates Statement objects.
SQL queries are defined and executed with Statement methods:
- executeQuery(): Associates a statement object with a SELECT query, compiles the query to low-level database operations and executes the query. executeQuery() has one required parameter, a string containing the SQL query, and returns a ResultSet object, described below.
- executeUpdate(): Executes INSERT, UPDATE, and DELETE queries. executeUpdate() returns the number of rows matched by the query, not the number of rows that were modified.
- execute(): Executes any SQL statement but is commonly used for data definition statements such as CREATE TABLE. execute() returns a boolean value. If the value is true, the statement has generated a ResultSet object which can be accessed with the Statement getResultSet() method.
- close: Releases statement objects and must be executed prior to closing the associated connection.
ResultSet Interface: The object that retrieves query results.
next(): Advances the current row to the next row. Returns true if the current row advances, or false if the current row is at the last row of the query result.
Values in the current row are accessed with ResultSet methods:
- getInt(): Takes a column name parameter and returns the integer value of the column.
- getDouble(): Takes a column name parameter and returns the Double value of the column.
- getString(): Takes a column name parameter and returns the String value of the column.
Other ResultSet methods return additional information:
- getMetaData(): Returns a ResultSetMetaData object, containing information about the columns of a ResultSet object.
- getWarnings(): Returns the first warning generated by a query.
PreparedStatement: An interface that extends the Statement interface and supports SQL query parameters.
- prepareStatement(): A Connection interface method that creates PreparedStatement objects. Takes an SQL query String parameter.
SQL Injection Attack: Ehen a user intentionally enters values that alter the intent of an SQL statement.
CallableStatement: An interface that extends PreparedStatement with methods that call stored procedures.
- prepareCall(): A Connection interface method that creates CallableStatement objects.
Comments
Post a Comment