120 likes | 340 Views
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)
E N D
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
System Aspects of SQL • SQL statements are usually part of some larger piece of software Host language + Embedded SQL Preprocessor Host language + Function Calls Host-language Compiler Object-code program Changes SQL into something familiar to host language Embedded SQL approach Provided by DBMS vendor Call-level interface (CLI) approach SQL library
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
JDBC • Java Database Connectivity • Call-Level-Interface (CLI) with Java’s OO style
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
JDBC API Establish Connection • Load Driver: For example: using JDBC-ODBC Bridge driver: Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver“); • Make Connection Connection con = DriverManager.GetConnection(url, "myLogin", "myPassword");
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)");
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"); }
Linking Servers: Same idea extended to multiple kinds of data sources on remote servers Linking Servers
PHP: Same Idea as JDBC Project 1