dbms & prog lang

37
DBMS & PROGRAMMING LANGUAGE

Upload: techmx

Post on 23-Jun-2015

948 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Dbms & prog lang

DBMS & PROGRAMMING LANGUAGE

Page 2: Dbms & prog lang

Contents

Object persistence Object serialization Using database Embedded SQL Database drivers

Page 3: Dbms & prog lang

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

Page 4: Dbms & prog lang

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

Page 5: Dbms & prog lang

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

Page 6: Dbms & prog lang

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

Page 7: Dbms & prog lang

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

Page 8: Dbms & prog lang

EMBEDDED SQL

Page 9: Dbms & prog lang

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.

Page 10: Dbms & prog lang

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

Page 11: Dbms & prog lang

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)

Page 12: Dbms & prog lang

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

Page 13: Dbms & prog lang

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.

Page 14: Dbms & prog lang

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 recent times, the database-program link is shown in the program much more explicitly, and the need for precompilers has been greatly reduced.

Editor

Pre compile r

Linke r

Compile r

hos t program + e mbe dde d SQL

e xe cutable program

obje ct (binary) program

hos t program + trans late d SQL

D B M S and othe r librarie s

Page 15: Dbms & prog lang

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.

Page 16: Dbms & prog lang

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 build-in 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.

Page 17: Dbms & prog lang

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

Page 18: Dbms & prog lang

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 – Microsoft’s version of CORBA – not as robust as CORBA over multiple platforms

Page 19: Dbms & prog lang

Middleware Software which allows an application to interoperateinteroperate 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”“glue” that holds client/server applications together

Page 20: Dbms & prog lang

ODBC – OPEN DATABASE CONNECTIVITY

Page 21: Dbms & prog lang

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 name2. Database name3. User id and password

Page 22: Dbms & prog lang

ODBC Architecture

Each DBMS has its own ODBC-compliant driver

Client does not need to know anything about the DBMS

Application Program Interface (API) provides common interface to all DBMSs

Page 23: Dbms & prog lang

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

Page 24: Dbms & prog lang

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!

Page 25: Dbms & prog lang

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.

Page 26: Dbms & prog lang

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

Page 27: Dbms & prog lang

Close Connection

To close the database connection call Close()

$db->Close();

Page 28: Dbms & prog lang

JDBC –Java DataBase Connectivity

Page 29: Dbms & prog lang

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.

Page 30: Dbms & prog lang

JDBC Architecture

Page 31: Dbms & prog lang

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

Page 32: Dbms & prog lang

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

Page 33: Dbms & prog lang

Executing SQL Statements

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

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

Page 34: Dbms & prog lang

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  );    }

Page 35: Dbms & prog lang

Close connection

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

con.close();

Page 36: Dbms & prog lang

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

Page 37: Dbms & prog lang

THANK YOU