1 / 14

Department of Computer and Information Science, School of Science, IUPUI

Department of Computer and Information Science, School of Science, IUPUI. Database Programming. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. DataBase Programming. Creating a Database Application:

taffy
Download Presentation

Department of Computer and Information Science, School of Science, IUPUI

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Department of Computer and Information Science,School of Science, IUPUI Database Programming Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

  2. DataBase Programming • Creating a Database Application: • Database applications usually consist of the following major components: • Program Modules written in a high-level programming language allow the programmer to encode the logic for the application. (eg: Java, C++, VB) • User interface allows programs to interact with the person using the application. Can be simple text based interface or GUI. • DBMS is used to create, access, and administer the data. (eg: Oracle, SQL Server, DB2) • Data Lanaguage is used to manipulate the data in the DBMS (eg: SQL) • Operating System Access mechanism to interface with the OS. This is provided through OS specific calls on the host computer.

  3. DataBase Programming • Options for accessing Oracle from an application program: • Embedded SQL. In this approach SQL statements are placed in the program source code, a source code pre-comiler then converts the SQL into the proper syntax for compilation and linkage.Examples: Pro*C, Pro*COBOL, SQLJ (Java) Java • API. A second option is to use an Application Program Interface (API) to develop your program. Oracle provides the OCI (Open Call Interface) which is a run-time library that can be called directly from within the application program to access the database. • PL/SQL. The third option is to use PL/SQL. It has the advantage of being tightly integrated with the database, but it is not a standard language and is Oracle specific.

  4. DataBase Programming • Embedded SQL: • Advantages: • SQL is easy to understand, • standardized and commonly used. • Disadvantage: • Takes an extra step to use the Pro*C precompiler. • Example, after pre-compile step: • /* • EXEC SQL • DELETE FROM emp • WHERE CURRENT OF emp_cursor; • */ • { • sqlstm.stmt = “DELETE FROM EMP WHERE ROWID=:b1”; • sqlstm.iters = (unsigned long)1; • sqlstm.offset = (unsigned short)193; • sqlstm.cud = sqlcud0; • sqlstm.sqlest = (unsigned char *)&sqlca; • sqlstm.sqlety = (unsigned short)0; • sqlcex(&sqlctx, &sqlstm, &sqlfpn); • }

  5. DataBase Programming

  6. DataBase Programming • DB program using an API: • Advantages: • Avoids the use of a pre-compiler like Pro*C. • Disadvantage: • Not as intuitive as embedded SQL . • Database vendor specific. • Simple C and OCI example: • SetupUpdate(szUpdate) • if (oopen(DML_CURS, LDA, (char far *)0, -1, -1, • (char far *)0, -1)) • { /* place error-handler here */ • return rc; • } • if (oparse(DML_CURS, (char far *)szUpdate, -1)) • { /* place error-handler here */ • return rc; • } • if (obndrv(DML_CURS, (char far *)”:rid_row”, -1, • (char far *)&((SEL_CURS)->rid), 14, ROWID, • -1, (char far*)0, (char far*)0, -1, -1)) • { /* place error-handler here */ • return rc; • } • if (obndrv(DML_CURS, (char far *)”:nEmployee”, -1, • (char far *)emp_empno, sizeof(emp_empno), NULLTERM, -1, (char far*) &ind_emp_empno, (char far*)0, -1, -1)) • { /* place error-handler here */ • return rc; • }

  7. DataBase Programming • Microsoft APIs: • ODBC • Open Database Connectivity API. • Slower than OCI • Not always Oracle friendly • DAO, RDO, ADO • Other Microsoft APIs to access databases. • DAO = Data Access Objects • RDO = Remote Data Objects • ADO = Advanced Data Objects • All these APIs are faster and generally simpler to program with than the ODBC API.

  8. DataBase Programming • The choice between ODBC and DAO is often difficult to make. In general, you will find that DAO provides more flexibility, with support for Data Definition Language (DDL) and Data Manipulation Language (DML). • The first consideration when deciding whether DAO or ODBC is the best solution for your application is to determine whether the data source that you are using is supported by the technology. • The following table shows many of the similarities and differences between the DAO and ODBC sets of classes. • CharacteristicDAO ClassesODBC Classes • Access .MDB files Yes Yes • Access ODBC data sources Yes Yes • Available for 16-bit No Yes • Available for 32-bit Yes Yes • Database compaction Yes No • Database engine support MS Jet DB engine Target DBMS • DDL support Yes Only via direct ODBC calls • DML support Yes Yes • Optimal for .MDB files Any DBMS for which you have (Microsoft Access) a driver, especially in client/server • Transaction support Per "workspace” or, Per database for ODBC data, per database • Thread-safe DAO 3.0 – No. Yes, if used with a thread-safe DAO 3.5 supports ODBC driver.apartment model threading • Updatable joins Yes No • Note In most situations, DAO will be more efficient than ODBC, with one major exception. Use the ODBC classes if you are working strictly with ODBC data sources, particularly in client-server situations. Here, the MFC ODBC classes provide better performance.

  9. DataBase Programming • Oracle the Java Way • SQLJ • Embedded SQL for Java • similar to Pro*C • JDBC API and Oracle • ODBC. • You can use Sun’s JDBC-ODBC Bridge software. • Also requires appropriate Oracle ODBC driver on client installation. • Oracle “Thin” Driver. • Typically used in Java applets in browsers. • Oracle's JDBC Thin driver is a Type 4 driver that uses Java sockets to connect directly to Oracle. It provides its own implementation of a TCP/IP version of Oracle's SQL*Net. • Because it is 100% Java, this driver is platform independent. • Oracle OCI7 and OCI8 drivers. • Typically used in Java applications, not in browsers. • Oracle's JDBC OCI drivers are Type 2 JDBC drivers. They provide an implementation of the JDBC interfaces that uses the OCI to interact with an Oracle database. • You must use a JDBC OCI driver appropriate to your Oracle client installation.

  10. DataBase Programming Using SQLJ • SQLJ Runtime Environments

  11. DataBase Programming The JDBC Classes to Call:

  12. DataBase Programming • Steps in making a JDBC Program. • Bring in the JDBC API into your program.import java.sql.*; • load the driver into memory.Class.forName(“driver”);or new Driver; • Get a connection to the database. • specify the data source using JDBC URL format. • jdbc:<subprotocol>:<subname> • subprotocol = data source type like odbc or oracle • subname = //hostname:port/database • Use the getConnection method of the DriverManager.String url = “@134.68.140.211:1521:WG73”; Connection con = DriverManager.getConnection(url, “scott”, “tiger”); • Create a Statement:Statement stmt = con.createStatement();

  13. DataBase Programming • Run the query, get a result set back.String s = “select emp_name from emp”;ResultSet rs = stmt.executeQuery(s); • Navigate through the results, one row at a time, one column at a time.while(rs.next()) { a = rs.getInt(1); b = rs.getString(2); c = rs.getFloat(3);} • Most all JDBC calls “throw exceptions”, this means that all JDBC code must be in try blocks.try { /* JDBC code goes here */} catch ( Exception e ) { e.printStackTrace();}

  14. Acknowledgements • Loney, Oracle Database 10g The Complete Reference

More Related