3. 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
4. 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
5. 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
6. 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
7. 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
9. 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.
10. 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
11. 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)
12. 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
13. 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.
14. SQL Precompiler
Precompilers are used to translate SQL
statements embedded in a host language
E dito r ho s t p ro g ra m + e m be dde d S Q L
into DBMS library calls which can be
implemented in the host language.
P re co m pi l e r
In recent times, the database-program
link is shown in the program much more
ho s t p ro g ra m + tra ns la te d S Q L
explicitly, and the need for precompilers
has been greatly reduced.
C o m pil e r
o b j e ct (bi n a ry ) pro g ra m
L ink e r D B M S a nd o the r l i b ra ri e s
e x e cuta bl e pro g ra m
15. 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.
16. 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.
17. 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
18. 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
19. 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
21. 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
22. 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
23. 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
24. 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!
25. 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.
26. 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
27. Close Connection
To close the database connection call Close()
$db->Close();
29. 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.
31. 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
32. 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
33. Executing SQL Statements
We can run any type of query again database to
perform database operatons.
ResultSet res = st.executeQuery("SELECT * FROM FOO" );
34. 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 );
}
35. 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();