Presentation is loading. Please wait.

Presentation is loading. Please wait.

EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.

Similar presentations


Presentation on theme: "EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language."— Presentation transcript:

1 EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language SQL query returns relation (no pgmg-language access mechanisms) Data structures in pgmg languages (with access mechanisms) Passing data between these two models is not straightforward Both languages have their strengths

2 EmbeddedSQL: 2 System Aspects of SQL Host language + Embedded SQL Preprocessor Host language + Function Calls Host-language Compiler Object-code program SQL statements are usually part of some larger piece of software SQL library Changes SQL into something familiar to host language Provided by DBMS vendor Call-level interface (CLI) approach Embedded SQL approach

3 EmbeddedSQL: 3 SQL/Host Language Interface Shared variables –Special Declare Section –Syntax A variable with a colon prefix refers to data in the result of an SQL statement. A variable with no colon prefix refers to data in as usual in a host language EXEC SQL –Tells preprocessor that SQL code is coming –Followed by other SQL statements

4 EmbeddedSQL: 4 Example

5 EmbeddedSQL: 5 JDBC Java Database Connectivity Call-Level-Interface (CLI) with Java’s OO style

6 EmbeddedSQL: 6 JDBC API BASIC CLASSES: –Statement Class sends SQL statements to DBMS Useful Methods: int executeUpdate(String sql_statement); »Used to execute DDL SQL statement, ex: creating, altering tables. ResultSet executeQuery(String sql_statement); »used to execute common SQL statements, such as SELECTs –ResultSet Class Manipulates rows in a table Useful Methods: beforeFirst(); //moves cursor to front of result set boolean next(); //moves the cursor to the next row The "cursor" starts just above first row, so call next() to get to the first row

7 EmbeddedSQL: 7 JDBC API Establish Connection 1.Load Driver: For example: using JDBC-ODBC Bridge driver: Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver“); 2.Make Connection Connection con = DriverManager.GetConnection(url, "myLogin", "myPassword");

8 EmbeddedSQL: 8 JDBC API Create Tables String makeTable = “create table Students” + "(Name varchar(32), ID integer)"; Statement stmt = con.createStatement(); stmt.executeUpdate(makeTable); Populate Tables Statement stmt = con.createStatement() stmt.executeUpdate(“insert into students values ('Mark', 523)");

9 EmbeddedSQL: 9 JDBC API Retrieve Data String query = “select Name, ID from Students"; ResultSet rs = stmt.executeQuery(query); while(rs.next()) { String s = rs.getString("Name"); int n = rs.getInt(“ID"); }

10 EmbeddedSQL: 10 Linking Servers: Same idea extended to multiple kinds of data sources on remote servers Linking Servers

11 EmbeddedSQL: 11 PHP: Same Idea as JDBC Project 1


Download ppt "EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language."

Similar presentations


Ads by Google