Dbms & Prog Lang

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 37

DBMS & PROGRAMMING LANGUAGE

Contents

Object persistence Object serialization Using database Embedded SQL Database drivers

Object Persistence

One of the most critical tasks that applications have to perform is to save and restore data Persistence is the storage of data from working memory so that it can be restored when the application is run again

In object-oriented systems, there are several ways in which objects can be made persistent
The choice of persistence method is an important part of the design of an application

Object Serialization

Simple persistence method which provides a program the ability to read or write a whole object to and from a stream of bytes Allows Java objects to be encoded into a byte stream suitable for streaming to a file on disk or over a network The class must implement the Serializable interface (java.io.Serializable), which does not declare any methods, and have accessors and mutators for its attributes

Using Databases

Most Client-Server applications use a RDBMS as their data store while using an object-oriented programming language for development Objects must be mapped to tables in the database and vice versa Applications generally require the use of SQL statements embedded in another programming language Impedance mismatch

Goal of object-oriented design is to model a process Goal of relational database design is normalisation The mapping from objects to tables can be difficult if the model contains complex class structures large unstructured objects object inheritance The resulting tables may store data inefficiently, or access to data may be inefficient

There are essentially three approaches which have been developed for the management of object storage in databases:

the Object-Oriented Database Management System (OODBMS) the Object-Relational Database Management System (ORDBMS) Object Relational Mapping

EMBEDDED SQL

Interactive SQL

SQL
SQL is a non-procedural language SQL specifies WHAT is required, not HOW this requirement is to be met.

INTERACTIVE SQL is good for:


defining database structure generating low-volume, ad hoc queries Prototyping

INTERACTIVE SQL is not good for the more sophisticated applications for which a programming language with links to SQL might be better.

Embedded SQL
SQL can be embedded within procedural programming languages. These language (sometimes referred to as 3GLs) include C/C++, Cobol, Fortran, and Perl. Thus the embedded SQL provides the 3GL with a way to manipulate a database, supporting:

highly customized applications background applications running without user intervention database manipulation which exceeds the abilities of simple SQL applications linking to Oracle packages, e.g. forms and reports applications which need customized window interfaces

Embedded SQL

SQL statements placed within a program. The source program is called the host program, and the language in which it is written is called the host language. We can execute any SQL statement using embedded SQL statements just as if we were in SQL*Plus. CREATE, ALTER and DROP database tables

SELECT, INSERT, UPDATE and DELETE rows of data COMMIT transactions (make any changes to the database permanent)

Embedded SQL Statements

Embedded SQL statements incorporate DDL, DML, and transaction control statements within a procedural language program. They are used with the Oracle pre-compilers, e.g. Pro*C. Embedded SQL statements enable to define, allocate, and release cursors declare a database name and connect to Oracle assign variable names initialize descriptors specify how error and warning conditions are handled parse and execute SQL statements retrieve data from the database

Executable and Declarative Statements

Embedded SQL includes all the interactive SQL statements plus others that allow to transfer data between Oracle and a host program. There are two types of embedded SQL statements: Executable: used to connect to Oracle, to define, query and manipulate Oracle data, to control access to Oracle data and to process transactions. They can be placed wherever host-language executable statements can be placed. Declarative: do not operate on SQL data. Use them to declare Oracle objects, communication areas and SQL variables which will be used by Oracle and host program. They can be placed wherever host-language declarations can be placed.

SQL Precompiler
Precompilers

are used to translate SQL statements embedded in a host language into DBMS library calls which can be implemented in the host language.
In

Editor Precompiler

host program + embedded SQL host program + translated SQL

recent times, the database-program link is shown in the program much more explicitly, and the need for precompilers has been greatly reduced.

Compiler Linker object (binary) program DBMS and other libraries executable program

Cursors - SELECT many rows

A cursor provides a pointer to a single row in the result of a selection query (which may return may rows) One row at a time is accessed through the cursor, which is moved to the next row before each data transfer The columns of that one row are fetched into the program variables which can then be manipulated in the normal way by the host program.

A cursor can also be used to update values in tables if it is linked to an INSERT SQL command rather than a SELECT query.

Cursors provide a means of integrating traditional 3GL procedural languages and databases by enabling row-at-a-time access. Languages such as Visual Basic and Visual C++ also have buildin statements that enable this type of processing with a dedicated database, like that provided by MS-Access Java has a well-defined interface to enable easy access to databases through a Database Connectivity library - JDBC. Perl makes use of the DBI standard, which is fast becoming the main contender to ODBC.

Database Drivers

Think of a database as just another device connected to your computer


Like other devices it has a driver program to relieves you of having to do low level programming to use the database The driver provides you with a high level API to the database

Database middleware

ODBC Open Database Connectivity - most DB vendors support this OLE-DB - Microsoft enhancement of ODBC JDBC Java Database Connectivity - Special Java classes that allow Java applications/applets to connect to databases CORBA Common Object Request Broker Architecture specification of object-oriented middleware DCOM Microsofts version of CORBA not as robust as CORBA over multiple platforms

Middleware

Software which allows an application to interoperate with other software, without requiring the user to understand and code the low-level operations required to achieve interoperability With Synchronous systems, the requesting system waits for a response to the request in real time Asynchronous systems send a request but do not wait for a response in real time the response is accepted whenever it is received .
The glue that holds client/server applications together

ODBC OPEN DATABASE CONNECTIVITY

What is ODBC ?

Open Database Connectivity (ODBC) is an API that provides a common language for application programs to access and process SQL databases independent of the particular RDBMS that is accessed.

ODBC driver needs the following : 1. Back-end server name 2. Database name 3. User id and password

ODBC Architecture
Client does not need to know anything about the DBMS
Application Program Interface (API) provides common interface to all DBMSs

Each DBMS has its own ODBC-compliant driver

Client application requests that a connection is established with a data source Driver manager identifies appropriate ODBC driver to use Driver selected will process the requests received from the client and submit queries to the RDBMS in the required version of SQL Java Database Connectivity (JDBC) is similar to ODBC built specifically for Java applications

DSN: Data Source Name


All database connections begin with a DSN Named database configuration Three types: User DSN

System DSN File DSN Win 95/98 only understand User and File When used as a CGI/ASP script with a web server always use System DSN!

Establish a Connection

Create a new Win32::ODBC object:

$db = new Win32::ODBC( "My DSN" );

The DSN can either be the name of a DSN or it can be a full connect string: My DSN DSN=My DSN;UID=Foo;PWD=Bar If the DSN passed in is really a Win32::ODBC object then that object is cloned

$db2 = new Win32::ODBC( $db );

$db2 is identical to $db. Some database systems do not like such clones.

Executing SQL Statement

Submit a text based SQL query

$Result = $db->Sql( SELECT * FROM Foo );

This is the only method call which returns a non-false value upon failure Returns error number (ODBC driver specific; not really valuable)

Call $db->Error() for more error details

Close Connection

To close the database connection call Close()

$db->Close();

JDBC
Java DataBase Connectivity

What is JDBC?

An API that lets you access virtually any tabular data source from the Java programming language JDBC is oriented towards relational database.

JDBC Architecture

Basic steps to use a database in Java


1.Establish a connection 2.Create JDBC Statements 3.Execute SQL Statements 4.GET ResultSet 5.Close connections

Establish a connection

import java.sql.*; Load the vendor specific driver Class.forName("oracle.jdbc.driver.OracleDriver");

Dynamically loads a driver class, for Oracle database

Make the connection Connection con = DriverManager.getConnection( "jdbc:oracle:thin:@oracle-prod:1521:OPROD", username, passwd);

Establishes connection to database by obtaining a Connection object

Executing SQL Statements

We can run any type of query again database to perform database operatons.

ResultSet res = st.executeQuery("SELECT * FROM FOO" );

Getting Result

In this step we receives the result of execute statement. In this case we will fetch the employees records from the recordset object and show on the console. while (res.next()) { String FOOName = res.getIn( " FOO_name " ); System.out.println( FOOName ); }

Close connection

Finally it is necessary to disconnect from the database and release resources being used. If you dont close the connection then in the production environment your application will fail due to hanging database connections.

con.close();

Integrating Programming Languages & Databases: What's the Problem??

THANK YOU

You might also like