Connecting to a Database through Programs Database Systems.

25
Connecting to a Database through Programs Database Systems

Transcript of Connecting to a Database through Programs Database Systems.

Page 1: Connecting to a Database through Programs Database Systems.

Connecting to a Database through Programs

Database Systems

Page 2: Connecting to a Database through Programs Database Systems.

DB Connections

• Typically only DBA ‘talk’ directly to a database

• End users generally connect to a database through some type of program– Program code– Forms

Page 3: Connecting to a Database through Programs Database Systems.

Connection Part 1

• Database tables are not ‘part’ of a program– So you have to establish a connection to them

• Connections are categorized in 2 categories– READ– Insert, Update and Delete

Page 4: Connecting to a Database through Programs Database Systems.

Not all DBs are Alike

• Not all databases act the same way, you have to know which way your database acts

• This determines the facilities needed for your connection

• Then you have to remember that relational database systems speak SQL

Page 5: Connecting to a Database through Programs Database Systems.

Connection Introduction

• Because database applications today reside in a complicated environment, various standards have been developed for accessing database servers.

• Some of the important standards are– OBDC (Open Database Connectivity) is the early standard for

relational databases.– OLE DB is Microsoft’s object-oriented interface for relational and

other databases.– ADO.NET (Active Data Objects) is Microsoft’s standard

providing easier access to OLE DB data for the non-object-oriented programmer.

Page 6: Connecting to a Database through Programs Database Systems.

The Role of the ODBC Standard

Page 7: Connecting to a Database through Programs Database Systems.

Open Database Connectivity (OBDC)

• The Open Database Connectivity (ODBC) standard provides a DBMS-independent means for processing relational database data.

• It was developed in the early 1990s by an industry committee and has been implemented by Microsoft and many other vendors.

• The goal is to allow a developer to create a single application that can access databases supported by different DBMS products without needing to be changed or recompiled.

Page 8: Connecting to a Database through Programs Database Systems.

ODBC Architecture

Page 9: Connecting to a Database through Programs Database Systems.

OBDC Components (Continued)

• OBDC consists of a data source, an application program, a driver manager, and a DBMS driver.

• A data source is the database and its associated DBMS, operating system, and network platform.– An ODBC data source can be a relational database, a

file server, or a spreadsheet.

• An applications program issues requests to create a connection with a data source.

Page 10: Connecting to a Database through Programs Database Systems.

OBDC Components

• A driver manager determines the type of DBMS for a given ODBC data source and loads that driver in memory.

• A DBMS driver processes ODBC requests and submits specific SQL statements to a given type of data source.

Page 11: Connecting to a Database through Programs Database Systems.

Conformance Levels

• Levels of conformance balance the scope of the OBDC standard.

• There are two types of conformance levels:– ODBC conformance levels concern the features and

functions that are made available through the driver’s application program interface (API).

• A driver API is a set of functions that the application can call to receive services.

– SQL conformance levels specify which SQL statements, expressions, and data types a driver can process.

Page 12: Connecting to a Database through Programs Database Systems.

Summary ofOBDC Conformance Levels

Page 13: Connecting to a Database through Programs Database Systems.

Summary ofSQL Conformance Levels

So, why do conformance levels matter?

You pick the ODBC driver you use based on your needs

Page 14: Connecting to a Database through Programs Database Systems.

How do you connect?

• Depends on the database and the programming environment

• Access can establish a DNS so that Access may be used as a front end to a database (ie front end to an oracle db)

• Java establishes a ODBC connection via the JDBC library

• C#, VB.Net use a Microsoft provided ‘Managed Provider’, ADO.Net or OBDC

Page 15: Connecting to a Database through Programs Database Systems.

The Steps whatever the case

• Establish a connection• Open the connection• Select data using an Object with SQL statement• Retrieve the Data

– Data is not used directly from the database – Retrieved into hold place such as a variable or object– Display the data

• OR Issue a Insert, Update or Delete command– Send data to the database– Usually number of rows affected is returned

Page 16: Connecting to a Database through Programs Database Systems.

Example

• Sample Code– http://science.kennesaw.edu/~mcmurray/dbTa

bles/labjava/Customers.java

• JDBC walkthrough– http://www.javacoffeebreak.com/articles/jdbc/i

ndex.html

Page 17: Connecting to a Database through Programs Database Systems.

Where does the data go?

• Insert, Update, Delete– Pushing data out to the Database– What is returned is the number of rows

affected

• What happens when you read [select] data?– Somehow you have to store the data in

memory

Page 18: Connecting to a Database through Programs Database Systems.

Data retrieval

• How much data are you retrieving?– One row – it’s fairly trivial

• Use variables

– Lots of Rows• Considerations to make and it will depend on the

technology being used

Page 19: Connecting to a Database through Programs Database Systems.

Cursor

• Used by Oracle PL/SQL is known as ‘cursor’

– Way data is accessed row by row

– Can be utilized in Java

Page 20: Connecting to a Database through Programs Database Systems.

Example

• To retrieve data with SQL one row at a time you need to use cursor processing.

• Cursor processing is done in several steps:1. Define the rows you want to retrieve. This is called

declaring the cursor. 2. Open the cursor. This activates the cursor and loads

the data. • Note that declaring the cursor doesn't load data, opening

the cursor does.

3. Fetch the data into variables. 4. Close the cursor.

http://www.fluffycat.com/SQL/Cursor-Oracle/

Page 22: Connecting to a Database through Programs Database Systems.

.NET

• Microsoft’s .NET uses– Datareader

• Processing row by row

– Datasets• Declared space in memory (data structure that

looks like a table)• Defined using XML schema• Loads all results of select statements into memory

for processinghttp://quickstart.developerfusion.co.uk/quickstart/howto/doc/adoplus/GetDataFromDB.aspx

Page 23: Connecting to a Database through Programs Database Systems.

Labs

• ACCESS connecting to Oracle– http://aspalliance.com/893_Creating_an_MS_Access

_2003_Front_End_for_an_Oracle_10g_Express_Edition_Database

• ADO.Net using Visual Studio (for IS and ISA majors)– Need an Oracle Managed Provider

• One is includes with Visual Studio but many problems reported so need to get one from Oracle

• Oracle 11g is has managed provided for ADO.Net built in

• JDBC for CS majors

Page 24: Connecting to a Database through Programs Database Systems.

SOME JAVA Examples

• http://java.sun.com/developer/codesamples/jdbc.html

• http://www.cs.ubc.ca/~ramesh/cpsc304/tutorial/JDBC/jdbc1.html

• http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/GettingStartedTOC.fm.html

• http://www.jdbc-tutorial.com/• http://www.javacoffeebreak.com/articles/

jdbc/index.html

Page 25: Connecting to a Database through Programs Database Systems.

Some MySQL PHP Resources• Connecting to a MySQL database via PHP

– http://www.php-mysql-tutorial.com/connect-to-mysql-using-php.php

– http://dev.mysql.com/tech-resources/articles/ddws/21.html

• Resources for graphical support of forms development PHP– http://phpmyadmin.qarchive.org/

• MySQL with JDBC– http://www.developer.com/java/data/article.php/3417381