Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts 8/24/2015 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI E-mail:

Similar presentations


Presentation on theme: "Dale Roberts 8/24/2015 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI E-mail:"— Presentation transcript:

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

2 Dale Roberts 7-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 Dale Roberts 7-3 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. DataBase Programming

4 Dale Roberts 7-4 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); } DataBase Programming

5 Dale Roberts 7-5 DataBase Programming

6 Dale Roberts 7-6 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; } DataBase Programming

7 Dale Roberts 7-7 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. DataBase Programming

8 Dale Roberts 7-8 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 filesYesYes Access ODBC data sourcesYesYes Available for 16-bitNoYes Available for 32-bitYesYes Database compactionYesNo Database engine supportMS Jet DB engineTarget DBMS DDL supportYesOnly via direct ODBC calls DML supportYesYes Optimal for.MDB files Any DBMS for which you have (Microsoft Access)a driver, especially in client/server Transaction supportPer "workspace” or, Per database for ODBC data, per database Thread-safeDAO 3.0 – No.Yes, if used with a thread-safe DAO 3.5 supports ODBC driver. apartment model threading Updatable joinsYesNo 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. DataBase Programming

9 Dale Roberts 7-9 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. DataBase Programming

10 Dale Roberts 7-10 SQLJ Runtime Environments DataBase Programming Using SQLJ

11 Dale Roberts 7-11 The JDBC Classes to Call: DataBase Programming

12 Dale Roberts 7-12 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 = 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(); DataBase Programming

13 Dale Roberts 7-13 –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(); } DataBase Programming

14 Dale Roberts 8/24/2015 14 Acknowledgements Loney, Oracle Database 10g The Complete Reference


Download ppt "Dale Roberts 8/24/2015 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI E-mail:"

Similar presentations


Ads by Google