Download presentation
Presentation is loading. Please wait.
Published bySophie Merritt Modified over 9 years ago
1
BD05/06 Database Application Development Embedded SQL ODBC JDBC Application architectures
2
BD05/06 SQL in Application Code SQL commands can be called from within a host language (e.g., C++ or Java) program. SQL statements can refer to host variables SQL statements can refer to host variables Must include a statement to connect to the right database. Must include a statement to connect to the right database. Two main integration approaches: 1. Embed SQL in the host language (Embedded SQL, SQLJ) 2. Create special API to call SQL commands (JDBC)
3
BD05/06 Impedance mismatch SQL relations are (multi-) sets of records, with no a priori bound on the number of records. No such data structure exist traditionally in procedural programming languages such as C++. SQL supports a mechanism called a cursor to handle this. SQL supports a mechanism called a cursor to handle this. Data types of SQL and host language are distinct There must be a conversion mechanism There must be a conversion mechanism
4
BD05/06 Embedded SQL The SQL standard defines embeddings of SQL in a variety of programming languages A language to which SQL queries are embedded is referred to as a host language, and the SQL structures in the host language comprise embedded SQL EXEC SQL statement is used to identify embedded SQL request to the preprocessor EXEC SQL END-EXEC Note: this varies by language. E.g. the Java embedding uses # SQL { …. } ; The statement SQL INCLUDE is placed in the program to identify where the preprocessor inserts special variables for communication between the program and the DB system
5
BD05/06 Example Query Specify the query in SQL and declare a cursor for it EXEC SQL declare c cursor for select customer-name, customer-city from depositor, customer, account where depositor.customer-name = customer.customer-name and depositor account-number = account.account-number and account.balance > :amount END-EXEC From within a host language, find the names and cities of customers with more than the variable amount dollars in some account.
6
BD05/06 Embedded SQL (Cont.) The open statement causes the query to be evaluated EXEC SQL open c END-EXEC The fetch statement causes the values of one tupleto be placed on host language variables. EXEC SQL fetch c into :cn, :cc END-EXEC Repeated calls to fetch get successive tuples in the query result A variable called SQLSTATE gets set to ‘02000’ to indicate no more data is available A variable called SQLSTATE gets set to ‘02000’ to indicate no more data is available The close statement causes the database system to delete the temporary relation EXEC SQL close c END-EXEC Note: above details vary with language. E.g. the Java embedding defines Java iterators to step through result tuples.
7
BD05/06 Updates Through Cursors Can update tuples fetched by cursor by declaring that the cursor is for update declare c cursor for select * from account where branch-name = ‘Perryridge’ for update To update tuple at the current location of cursor update account set balance = balance + 100 where current of c
8
BD05/06 Dynamic SQL Allows programs to construct and submit SQL queries at run-time. Example of the use of dynamic “SQL from within a C program. char * sqlprog = “update account set balance = balance * 1.05 where account- number = ?“; EXEC SQL prepare dynprog from :sqlprog; char account [10] = “ A-101 “; EXEC SQL execute dynprog using :account; The dynamic SQL program contains a ?, which is a place holder for a value that is provided when the SQL program is executed.
9
BD05/06 Database APIs: Alternative to embedding Rather than modify compiler, add library with database calls (API) Special standardized interface Pass SQL strings from language, presents result sets in a language-friendly way Ex: Sun’s JDBC: Java API (set of classes/interfaces in java.sql package) Supposedly DBMS-neutral (portable) a driver traps the calls and translates them into DBMS-specific code a driver traps the calls and translates them into DBMS-specific code
10
BD05/06 ODBC Open DataBase Connectivity (ODBC) is a standard for application program to communicate with a database server. Application program interface (API) to: open a connection with a database, open a connection with a database, send queries and updates, send queries and updates, get back results. get back results. Applications such as GUI, spreadsheets, etc. can use ODBC
11
BD05/06 ODBC (Cont.) Each database system supporting ODBC provides a "driver" library that must be linked with the client program. When client program makes an ODBC API call, the code in the library communicates with the server to carry out the requested action, and fetch results. When client program makes an ODBC API call, the code in the library communicates with the server to carry out the requested action, and fetch results. ODBC program first allocates an SQL environment, then a database connection handle. Opens database connection using SQLConnect() with the following parameters: Opens database connection using SQLConnect() with the following parameters: connection handle, the server to which to connect, the user identifier, passwordconnection handle, the server to which to connect, the user identifier, password
12
BD05/06 Example: ODBC Code int ODBCexample(){ RETCODE error; RETCODE error; HENV env; /* environment */ HENV env; /* environment */ HDBC conn; /* database connection */ HDBC conn; /* database connection */ SQLAllocEnv(&env); /* allocates an SQL environment */ SQLAllocEnv(&env); /* allocates an SQL environment */ SQLAllocConnect(env, &conn); /* allocates a DB conn. handle */ SQLAllocConnect(env, &conn); /* allocates a DB conn. handle */ SQLConnect(conn, "aura.bell-labs.com", SQL_NTS, "avi", SQL_NTS, "avipasswd", SQL_NTS); SQLConnect(conn, "aura.bell-labs.com", SQL_NTS, "avi", SQL_NTS, "avipasswd", SQL_NTS); { Main body of program … } { Main body of program … } SQLDisconnect(conn); SQLDisconnect(conn); SQLFreeConnect(conn); SQLFreeConnect(conn); SQLFreeEnv(env); SQLFreeEnv(env); }
13
BD05/06 ODBC Code (Cont.) Program sends SQL commands using SQLExecDirect Result tuples are fetched using SQLFetch() SQLBindCol() binds C language variables to attributes of the query result When a tuple is fetched, its attribute values are automatically stored in corresponding C variables. When a tuple is fetched, its attribute values are automatically stored in corresponding C variables. Arguments to SQLBindCol() ODBC stmt variable, attribute position in query result ODBC stmt variable, attribute position in query result The type conversion from SQL to C. The type conversion from SQL to C. The address of the variable. The address of the variable. For variable-length types like character arrays, the maximum length of the variable and location to store actual length. For variable-length types like character arrays, the maximum length of the variable and location to store actual length. Good programming requires checking results of every function call for errors; we have omitted most checks for brevity.
14
BD05/06 Example: Main body of program char branchname[80]; char branchname[80]; float balance; float balance; int lenOut1, lenOut2; int lenOut1, lenOut2; HSTMT stmt; HSTMT stmt; SQLAllocStmt (conn, &stmt); SQLAllocStmt (conn, &stmt); char * sqlquery = "select branch_name, sum (balance) from account group by branch_name"; char * sqlquery = "select branch_name, sum (balance) from account group by branch_name"; error = SQLExecDirect(stmt, sqlquery, SQL_NTS); error = SQLExecDirect(stmt, sqlquery, SQL_NTS); if (error == SQL_SUCCESS) { SQLBindCol(stmt, 1, SQL_C_CHAR, branchname, 80, &lenOut1); SQLBindCol(stmt, 2, SQL_C_FLOAT, &balance, 0, &lenOut2); if (error == SQL_SUCCESS) { SQLBindCol(stmt, 1, SQL_C_CHAR, branchname, 80, &lenOut1); SQLBindCol(stmt, 2, SQL_C_FLOAT, &balance, 0, &lenOut2); while (SQLFetch(stmt) >= SQL_SUCCESS) { printf (" %s %g\n", branchname, balance); } } SQLFreeStmt(stmt, SQL_DROP); while (SQLFetch(stmt) >= SQL_SUCCESS) { printf (" %s %g\n", branchname, balance); } } SQLFreeStmt(stmt, SQL_DROP);
15
BD05/06 More ODBC Features Prepared Statement SQL statement prepared: compiled at the database SQL statement prepared: compiled at the database Can have placeholders: insert into account values(?,?,?) Can have placeholders: insert into account values(?,?,?) Repeatedly executed with actual values for the placeholders Repeatedly executed with actual values for the placeholders Metadata features finding all the relations in the database and finding all the relations in the database and finding the names and types of columns of a query result or a relation in the database. finding the names and types of columns of a query result or a relation in the database. By default, each SQL statement is treated as a separate transaction that is committed automatically. Can turn off automatic commit on a connection Can turn off automatic commit on a connection SQLSetConnectOption(conn, SQL_AUTOCOMMIT, 0)SQLSetConnectOption(conn, SQL_AUTOCOMMIT, 0) transactions must then be committed or rolled back explicitly by transactions must then be committed or rolled back explicitly by SQLTransact(conn, SQL_COMMIT) orSQLTransact(conn, SQL_COMMIT) or SQLTransact(conn, SQL_ROLLBACK)SQLTransact(conn, SQL_ROLLBACK)
16
BD05/06 ODBC Conformance Levels Conformance levels specify subsets of the functionality defined by the standard. Core Core Level 1 requires support for metadata querying Level 1 requires support for metadata querying Level 2 requires ability to send and retrieve arrays of parameter values and more detailed catalog information. Level 2 requires ability to send and retrieve arrays of parameter values and more detailed catalog information. SQL Call Level Interface (CLI) standard similar to ODBC interface, but with some minor differences.
17
BD05/06 JDBC Java API for communicating with database systems supporting SQL Supports a variety of features for querying and updating data, and for retrieving query results Supports metadata retrieval, such as querying about relations present in the database and the names and types of relation attributes
18
BD05/06 JDBC: Architecture Four architectural components: Application initiates and terminates connections, submits SQL statements, and retrieves results through the JDBC API Driver manager handles initialization, loads JDBC driver Driver connects to data source, transmits requests and returns/translates results and error codes Data source processes SQL statements from the driver and returns results
19
BD05/06 Steps to submit a database query 1)Select a data source 2)Dynamically load the JDBC driver 3)Connect to the data source 4)Execute SQL statements 5)Disconnect from the data source
20
BD05/06 2. JDBC Driver Manager All drivers are managed by the DriverManager class that keeps a list of all currently loaded drivers Has methods: registerDriver, deRegisterDriver, getDrivers Loading a JDBC driver: In the Java code: Class.forName(“oracle/jdbc.driver.Orac ledriver”); In the Java code: Class.forName(“oracle/jdbc.driver.Orac ledriver”); When starting the Java application: -Djdbc.drivers=oracle/jdbc.driver When starting the Java application: -Djdbc.drivers=oracle/jdbc.driver
21
BD05/06 3. Connections in JDBC We interact with a data source through sessions. A session is started through the creation of a Connection object. A Connection is specified through a JDBC URL: jdbc: : A Connection is specified through a JDBC URL: jdbc: : Example of code to establish a connection to Oracle: String url=“jdbc:oracle:www.bookstore.com:3083”; Connection con; try{ con = DriverManager.getConnection(url,usedId,password); } catch SQLException excpt { …}
22
BD05/06 Connection Class Interface public int getTransactionIsolation() void setTransactionIsolation(int level) Sets isolation level for the current connection. public boolean getReadOnly() void setReadOnly (boolean b) Specifies whether transactions in this connection are read- only public boolean getAutoCommit() void setAutoCommit(boolean b) If autocommit is set, then each SQL statement is considered its own transaction. Otherwise, a transaction is committed using commit(), or aborted using rollback(). public boolean isClosed() Checks whether connection is still open.
23
BD05/06 4. Executing SQL Statements Three different ways of executing SQL statements: Statement Statement PreparedStatement PreparedStatement CallableStatment (stored procedures) CallableStatment (stored procedures) PreparedStatement class: Precompiled, parametrized SQL statements that can be executed several times: Structure is fixed Structure is fixed Values of parameters are determined at run-time Values of parameters are determined at run-time
24
BD05/06 Statements and ResultSet executeUpdate only returns the number of affected records executeQuery returns data, encapsulated in a ResultSet object (a cursor) Statement stmt = conn.createStatement(); String sql = "select branch_name, avg(balance) from account group by branch_name" ResultSet rs=stmt.executeQuery(sql); // rs is now a cursor While (rs.next()) { // process the data // process the data}
25
BD05/06 ResultSet (Contd.) A ResultSet is a very powerful cursor: previous() : moves one row back absolute(int num) : moves to the row with the specified number relative (int num) : moves forward or backward relative to the current position first() and last(): moves to the first/last row
26
BD05/06 Matching Java and SQL Data Types getTimestamp()java.sql.TimeStampTIMESTAMP getTime()java.sql.TimeTIME getDate()java.sql.DateDATE getFloat()DoubleREAL getInt()IntegerINTEGER getDouble()DoubleFLOAT getDouble()DoubleDOUBLE getString()StringVARCHAR getString()StringCHAR getBoolean()BooleanBIT ResultSet get methodJava classSQL Type
27
BD05/06 JDBC: Exceptions and Warnings Most of java.sql JDBC methods can throw and SQLException if an error occurs. SQLException methods: getMessage(), getSQLState(), getErrorCode(), getNextException() SQLWarning is a subclass of SQLException; not as severe (they are not thrown and their existence has to be explicitly tested)
28
BD05/06 JDBC Code public static void JDBCexample(String dbid, String userid, String passwd) { try { try { /* load driver */ Class.forName ("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@aura.bell-labs.com:2000:bankdb", userid, passwd); Statement stmt = conn.createStatement(); Statement stmt = conn.createStatement(); … Do Actual Work …. … Do Actual Work …. stmt.close(); stmt.close(); conn.close(); conn.close();} catch (SQLException sqle) { System.out.println("SQLException : " + sqle); System.out.println("SQLException : " + sqle);}}
29
BD05/06 JDBC Code (Cont.) Update to database try { stmt.executeUpdate("insert into account values ('A-9732', 'Perryridge', 1200)"); stmt.executeUpdate("insert into account values ('A-9732', 'Perryridge', 1200)"); } catch (SQLException sqle) { System.out.println("Could not insert tuple. " + sqle); System.out.println("Could not insert tuple. " + sqle);} Execute query and fetch and print results ResultSet rset = stmt.executeQuery( "select branch_name, avg(balance) from account group by branch_name"); while (rset.next()) { System.out.println(rset.getString(" branch_name") + " " + rset.getFloat(2)); }
30
BD05/06 JDBC Code Details Getting result fields: rs.getString(“branchname”) and rs.getString(1) equivalent if branchname is the first argument of select result. rs.getString(“branchname”) and rs.getString(1) equivalent if branchname is the first argument of select result. Dealing with Null values int a = rs.getInt(“a”); if (rs.wasNull()) Systems.out.println(“Got null value”);
31
BD05/06 Prepared Statement Allows queries to be compiled and executed multiple times with different arguments PreparedStatement pStmt = conn.prepareStatement( “insert into account values(?,?,?)”); pStmt.setString(1, "A-9732"); pStmt.setString(2, "Perryridge"); pStmt.setString(2, "Perryridge"); pStmt.setInt(3, 1200); pStmt.setInt(3, 1200); pStmt.executeUpdate(); pStmt.executeUpdate(); pStmt.setString(1, "A-9733"); pStmt.executeUpdate(); pStmt.executeUpdate(); Beware: If value to be stored in database contains a single quote or other special character, prepared statements work fine, but creating a query string and executing it directly would result in a syntax error!
32
BD05/06 Example of PreparedStatement String sql=“INSERT INTO Sailors VALUES(?,?,?,?)”; PreparedStatment pstmt=con.prepareStatement(sql); pstmt.clearParameters();pstmt.setInt(1,sid);pstmt.setString(2,sname); pstmt.setInt(3, rating); pstmt.setFloat(4,age); // we know that no rows are returned, thus we use executeUpdate() int numRows = pstmt.executeUpdate();
33
BD05/06 Transactions in JDBC As with ODBC, each statement gets committed automatically in JDBC To turn off auto commit use conn.setAutoCommit(false); To commit or abort transactions use conn.commit() or conn.rollback() To turn auto commit on again, use conn.setAutoCommit(true);
34
BD05/06 Procedure and Function Calls in JDBC JDBC provides a class CallableStatement which allows SQL stored procedures and functions to be invoked. CallableStatement cs1 = conn.prepareCall(“{call proc(?,?)}”); CallableStatement cs1 = conn.prepareCall(“{call proc(?,?)}”); CallableStatement cs2 = conn.prepareCall(“{?=call func(?,?)}”);
35
BD05/06 Result Set MetaData The class ResultSetMetaData provides information about all the columns of the ResultSet. Instance of this class is obtained by getMetaData ( ) function of ResultSet. Provides functions for getting number of columns, column name, type, precision, scale, table from which the column is derived etc. ResultSetMetaData rsmd = rs.getMetaData ( ); for ( int i = 1; i <= rsmd.getColumnCount( ); i++ ) { String name = rsmd.getColumnName(i); String name = rsmd.getColumnName(i); String typeName = rsmd.getColumnTypeName(i); } String typeName = rsmd.getColumnTypeName(i); }
36
BD05/06 Database Meta Data The class DatabaseMetaData provides information about database relations Has functions for getting all tables, all columns of the table, primary keys, foreign keys, etc. Has functions for getting all tables, all columns of the table, primary keys, foreign keys, etc. Ex: to print column names and types of a relation DatabaseMetaData dbmd = conn.getMetaData( ); ResultSet rs = dbmd.getColumns(null, “BANK-DB”, “account”, “%” ); //Arguments: catalog, schema-pattern, table-pattern, column-pattern // Returns: 1 row for each column, with several // attributes such as COLUMN_NAME, TYPE_NAME, etc. while (rs.next( )) { System.out.println( rs.getString(“COLUMN_NAME”), rs.getString(“TYPE_NAME”) }
37
BD05/06 Schemas, Catalogs, and Environments Three-level hierarchy for naming relations. Database contains multiple catalogs Database contains multiple catalogs each catalog can contain multiple schemas each catalog can contain multiple schemas SQL objects such as relations and views are contained within a schema SQL objects such as relations and views are contained within a schema e.g. catalog5.bank-schema.account Each user has a default catalog and schema, and the combination is unique to the user. Default catalog and schema are set up for a connection Catalog and schema can be omitted, defaults are assumed Multiple versions of an application (e.g. production and test) can run under separate schemas
38
BD05/06 Application Architectures Applications can be built using one of two architectures: Two tier model Application program running at user site directly uses JDBC/ODBC to communicate with the database Application program running at user site directly uses JDBC/ODBC to communicate with the database Three tier model Users/programs running at user sites communicate with an application server. The application server in turn communicates with the database Users/programs running at user sites communicate with an application server. The application server in turn communicates with the database
39
BD05/06 Two-tier Model E.g. Java code runs at client site and uses JDBC to communicate with the server Benefits: flexible, need not be restricted to predefined queries flexible, need not be restricted to predefined queries Problems: Security: passwords available at client site, all database operation possible Security: passwords available at client site, all database operation possible More code shipped to client More code shipped to client Not appropriate across organizations, or in large ones like universities Not appropriate across organizations, or in large ones like universities
40
BD05/06 Three Tier Model CGI Program Database Server Application/HTTP Server Servlets JDBC Network Client HTTP/Application Specific Protocol
41
BD05/06 Three-tier Model (Cont.) E.g. Web client + Java Servlet using JDBC to talk with database server Client sends request over http or application- specific protocol Application or Web server receives request Request handled by CGI program or servlets Security handled by application at server Simple client, but only packaged transactions
42
BD05/06 References Database Management Systems, J. Gerke and R. Ramakrishnan, Chapt. 6 JDBC tutorial (como o indicado na página da cadeira)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.