Database
Clients
Peter Komisar ©
Conestoga College version 1.0
reference: Core
MySQL, Leon Atkinson, Prentice-Hall Publishing,
ODBC Overview, Why Was ODBC invented, Microsoft .NET Dev Center,
http://msdn2.microsoft.com/en-us/library/ms714587(VS.85).aspx
VBScript Database Tutorial
http://www.electronics.dit.ie/staff/srooney/Output/VBScript%20Database%20Tutorial.pdf
Recall
we started the course with a survey of different
databases. We have some good news to conclude the
course. The world of SQL
and databases has a mature
and open connectivity architecture that enables all kinds
of applications written in different languages and running
on different platforms to access virtually all the active
databases in use today.
This good fortune, is largely based on the adoption of
ODBC by Microsoft which used it's clout, in this case,
in a very good direction.
ODBC Overview
ODBC
is a Microsoft invention that facilitates application
connection to any of a number of
Database Management
Systems. The following paragraphs
are quoted from the
Microsoft site which describe ODBC very concisely.
"Open
Database Connectivity (ODBC) is a widely accepted
application
programming interface (API) for database access.
It is based on the
Call-Level Interface (CLI) specifications from
Open Group and ISO/IEC
for database APIs and uses Structured
Query Language (SQL) as its
database access language.
ODBC is designed for maximum interoperability
- that is, the
ability of a single application to access different
database
management systems (DBMSs) with the same source code."
Drivers
"Database
applications call functions in the ODBC interface,
which are
implemented in database-specific modules called
drivers. The
use of drivers isolates applications from database-
specific calls in
the same way that printer drivers isolate word
processing programs from
printer-specific commands. Because
drivers are loaded at run time, a
user only has to add a new driver
to access a new DBMS; it is not
necessary to recompile or relink
the application."
Why
Was ODBC Created?
The following is also quoted from the Microsoft site.
"Historically,
companies used a single DBMS. All database
access was done either
through the front end of that system
or through applications written to
work exclusively with that
system. However, as the use of computers
grew and more
computer hardware and software became available,
companies
started to acquire different DBMSs. The reasons were many:
People bought what was cheapest, what was fastest, what
they already
knew, what was latest on the market, what worked
best for a single
application. Other reasons were reorganizations
and mergers, where
departments that previously had a single
DBMS now had several."
// companies migrated from using single to
multiple DBMSs.
The
issue grew even more complex with the advent of
personal computers.
These computers brought in a host
of tools for querying, analyzing, and
displaying data,
along with a number of inexpensive, easy-to-use
databases.
From then on, a single corporation often had data scattered
across a myriad of desktops, servers, and minicomputers,
stored in a
variety of incompatible databases, and accessed
by a vast number of
different tools, few of which could get at
all of the data.
// PCs and inexpensive databases led to
'data scatter'
The
final challenge came with the advent of client/server
computing, which
seeks to make the most efficient use of
computer resources. Inexpensive
personal computers (the
clients) sit on the desktop and provide both a
graphical front
end to the data and a number of inexpensive tools, such
as
spreadsheets, charting programs, and report builders.
// two and three tier systems provided the
need to connect
// front end resources easily with
mini and mainframe
// computers hosting the DBMSs.
Minicomputers
and mainframe computers (the servers) host
the DBMSs, where they can
use their computing power and
central location to provide quick,
coordinated data access.
// the question became as follows
How then was the front-end software to be
connected to the
back-end databases?
A similar problem faced
independent software vendors (ISVs).
Vendors writing database software
for minicomputers and
mainframes were usually forced to write one
version of an
application for each DBMS or write DBMS-specific code for
each DBMS they wanted to access.
// vendors
also were having trouble writing platform and software
// specific drivers
Vendors writing software for personal
computers had to write
data access routines for each different DBMS
with which they
wanted to work. This often meant a huge amount of
resources
were spent writing and maintaining data access routines
rather
than applications, and applications were often sold not on their
quality but on whether they could access data in a given DBMS.
// Summary of the need
What
both sets of developers needed was a way to access
data in different
DBMSs. The mainframe and minicomputer
group needed a way to merge data
from different DBMSs
in a single application, while the personal
computer group
needed this ability as well as a way to write a single
application
that was independent of any one DBMS. In short, both groups
needed an interoperable way to access data; they needed
open database
connectivity.
Microsoft's
UDA in Relation to JDBC 2.0
Microsoft has a number of APIs such as OLE ( Object
Linking and Embedding)
DB, ADO (ActiveX Data Objects)
and RDS (Remote
Data Service). OLE DB
and ADO are
object-oriented interfaces to databases
that can do SQL.
ADO is
a newer version with many functionalities that
are
similar to JDBC
2. RDS is a database API written
in
Microsoft's Java which is not portable.
Microsoft
has created an umbrella term
UDA or Universal
Data Access to encompass and streamline these
APIs.
JDBC 2.0 contains all
the functionality described in UDA
and goes a step further, for instance, by providing
support
for SQL3.
The JDBC API Tutorial and Reference'
by White et
al. puts the different
Microsoft APIs in context relative
to the JDBC
API.
Comparing
Microsoft Database APIs to JDBC
UDA // Microsoft
<--> JDBC 2.0 //
Java
|___OLE
|___DB
|___ADO
// should check to see what SQL3 is now
present
// in Microsoft offerings
Connector
Overview
Connectors
Connectors are drivers that supply connectivity to
databases. MySQL, for instance can use five types
of connectors.
MySQL Connector Types
// descriptions
from the MySQL Manual
connects to a MySQL server via Open
Database
Connectivity (ODBC) API. Support is available for
ODBC connectivity for Windows, Unix and Mac
OS X platforms.
The MySQL manual states this connector
"enables developers
to create .NET applications that use data stored in a MySQL
database. Connector/NET implements a fully-functional
ADO.NET interface and provides support for use with
ADO.NET aware tools. Applications that want to use
Connector/NET can be written in any of the supported
.NET languages.
- The MySQL
Visual Studio Plugin
"works with Connector/NET and Visual Studio 2005. The
plugin is a MySQL DDEX Provider, which means that you
can use the schema and data manipulation tools within
Visual Studio to create and edit objects within a MySQL
database".
provides driver support for connecting to MySQL from
a Java application using the standard Java Database
Connectivity (JDBC) API. Connector/MXJ is a tool that
enables easy deployment and management of MySQL
server and database through your Java application.
a Windows-only connector for PHP that provides the mysql
and mysqli extensions for use with MySQL 5.0.18 and later.
MySQL also supports connectivity for Perl, Python and the
PHP on other platforms.
Details
Regarding Connector/ODBC
Typically, you need to install
Connector/ODBC only
on Windows machines. For Unix and Mac OS X
you the native MySQL network is used. To install
the Connector/ODBC connector on a Unix host, an
ODBC manager must also be installed.
Connector/ODBC
Versions
Two version of Connector/ODBC are available
at the time of this writing:
This connector is currently in beta status, and is designed
to incorporate full support for the functionality in the MySQL
5.0 server release, including stored procedures and views.
This connector is the current release of the 32-bit ODBC
driver, also known as the MySQL ODBC 3.51 driver.
Components
of the Connector Architecture
There are five components in the Connector Architecture
listed below.
Connector/ODBC Components
- Application
- uses the ODBC API
- which communicates with the Driver Manager
- the database is identified by it's DSN
- Driver Manager
- manages application / driver(s) communication
- Resolves Data Source Names (DSN).
- Loads and unloads the driver required
- Processes ODBC function calls or
- passes them to the driver for processing.
- Connector/ODBC
Driver
- implements the functions supported by the ODBC API.
- processes ODBC function calls
- submits SQL requests to MySQL server
- returns results back to the application
- DSN
Configuration
- The ODBC configuration file stores
- the driver and database information
- required to connect to the server.
- used by the Driver Manager
- to decide which driver to load
- according to the definition in the DSN.
- MySQL Server
- The database where the information is stored
Installing the Connector
The binary installation is the easiest and most
straightforward
method of installation. . You can get a copy of the latest version
of Connector/ODBC binaries and sources from the MySQL AB
Web site
MySQL AB Web Site
http://dev.mysql.com/downloads/
Connector/ODBC
Configuration
Before you connect to a MySQL database using the
Connector/ODBC driver an ODBC Data Source Name
must be configured. The DSN associates configuration
parameters that are required to communicate with a
database to a specific name.
The DSN is used to communicate with the database,
rather than specifying the individual parameters within
the application.
A DSN typically consists of the following parameters:
Data Source Names Parameters
- Name
- Hostname
- Database Name
- Login
- Password
Types of DSNs
There are three types of DSN
- A System DSN
- a global DSN definition
- available to any user and application on a system.
- normally configured by a system administrator
- or by a user with permissions
- A User DSN
- specific to an individual user
- stores database connectivity information for that user
- A File DSN
- uses a simple file to define the DSN configuration.
- File DSNs can be shared between users and machines
- practical when installing or deploying DSN information
- as part of an application across many machines.
DSN information is stored in different locations depending
on platform and environment.
//
Data Source Administrator is used to set up DSNs
SQL
Clients in Different Languages
The existence of connector architecture enables SQL
clients to be created in a number of programming
languages. This survey does not attempt to teach us
how to write clients in different languages. It does
show that basically the same actions are being taken
to connect to the database, just using different
'vocabulary'.
MySQL
C Client
The MySQL C Client doesn't use
the connector
architecture described above. It takes advantage of
MySQL's 'C' API, a set of programming libraries
available to developers to enable applications to
interface directly to the MySQL database.
The mysql.h file must be included and linked to the
C library. This allows an #include statement to be
used in the source code.
You need to compile the client. On Linux the compile
command might look like the following. The -I option
helps to find headers and the -L options assists in
finding libraries.
Example //
from Core MySQL
gcc-o testmy testmy.c -L/user/local/lib/mysql -lmysqlclient
On Windows, 'libmysql.lib acts as a wrapper for
'libmysql.dll'.
First Leon Atkinson, in his book, Core MySQL, shows
minimal client which does nothing except to initialize
the connection framework, then open and close a
connection on a database. Some of the key elements
here are the MYSQL struct, a C datatype that
represents a connection to a database server and
the mysql_real_connect function which makes the
actual network socket connection between the client
and the database.
Example //
from Core MySQL
#include <stdio.h>
#include <mysql/mysql.h>
int main(int argc, char *argv[]){
MYSQL mysql;
/*
* Initialize connection
framework
*/
mysql_init(&mysql);
/*
* connect to database
with default values
*/
myslq_real_connect
(&mysql, NULL, NULL,NULL,NULL, 0,NULL, 0);
/*
* close connection
*/
mysql_close(&mysql);
}
He then expands the example to include a
SELECT query and includes some error
handling code.
Example //
from Core MySQL
#include <stdio.h>
#include <mysql/mysql.h>
int main(int argc, char *argv[]){
MYSQL mysql;
MYSQL_RES *result;
MYSQL_ROW row;
uint num_fields, i;
ulong *lengths;
/*
*
Initialize connection framework
*/
if (!mysql_init(&mysql)
{
fprintf(sterr, "Unable to initialize MYSQL struct! \n");
exit( ) ;
}
/*
*
connect to database with default values
*/
if (!myslq_real_connect(&mysql, "localhost", "root",
"password","mysql",0,NULL,0 ))
{
fprintf(stderr, "d: %s\n",
mysql_errno(&mysql),
mysql_error(&mysql));
exit( );
);
if (mysql_query(&mysql,
"SELECT User,Host FROM user ORDER BY 1,2 ))
{
/*
** Query failed!
*/
fprintf(sterr, "%d: %s\n,
mysql_errno(&mysql), mysql_error(&mysql_error));
}
else
{
/*
*
Buffer results, count fields
*/
result =
mysql_store_result(&mysql);
num_fields = mysql_num_fields(result);
while
(row=mysql_fetch_row(result)){
{
lengths=mysql_fetch_lengths(results);
for(i=0;i<num_fields;i++)
{
printf("[%.*s]",
(int)lengths[i],
row[i] ? row [i] : "NULL");
}
printf("\n");
}
/*
*
Free result set
*/
mysql_free_result(result);
}
/*
* close connection
*/
mysql_close(&mysql);
}
In all the clients,
regardless of language there are
common characteristics that repeat themselves.
The database needs to be located, connected to
via a socket connection, queried and the result set
read, typically a compound loop where the outer
loops iterates the rows, and the inner loop reads
the fields from each row.
Background To JDBC
The JDBC API is a
set of Java classes and interfaces
which provide a
standard specification that developers
can use to create Java database applications.
The
primary
function of JDBC is to send SQL statements to database
systems.
This functionality is being extended in JDBC 2
to include different
data types.
//
JDBC lets Java programs send SQL statements to databases
The great advantage
of JDBC is that it allows a single,
portable Java program to communicate with several
different databases regardless of who the vendor is,
whether
it be Oracle, IBM, Microsoft or MySQL, to
name a few.
//
one Java JDBC program can communicate with many
// databases from different vendors
JDBC is part of the
standard library that accompanies
the Standard Edition of Java Development Kit. JDBC is
relatively easy to program allowing the creation of secure
and robust database applications. The ease and speed
with
which programs can be developed and deployed
makes using Java's JDBC API very economical.
//
JDBC is 'built into' the Java Development Kit making
// building database applications easy, fast and economical
The basic
operations that are performed in a JDBC
program include establishing a connection
to a data
source (a database) , sending queries in the form of
SQL statements and processing the results that are
returned.
JDBC, then, can be
thought of as an environment
created inside a Java program that allows a database
to be queried using SQL statements. JDBC also is
designed
to
provide a foundation on which other
database applications can be built.
SQLJ
An example of an API that builds on JDBC is SQLJ.
SQLJ is a specification
that has been developed by a
consortium which
includes IBM, Oracle and Sun.
SQLJ
supplies a preprocessor that allow a programmer
to
intersperse SQL
statements into Java code. The SQLJ
preprocessor
sorts the mix and restores
the code to a
form that can be executed using
regular JDBC calls.
Other schemes
where objects are mapped to relational
databases
also take advantage of JDBC
at a lower
level.
JDBC & ODBC
JDBC builds
on
some of ODBC's basic design features.
Both ODBC and JDBC are
based on Open Group's earlier
design called SQL
CLI, Call Level Interface.
(Open Group
was
formerly known as X/Open.)
While ODBC is written in the C
programming language,
JDBC is
written all in Java code.
ODBC is a widely used
interface for accessing relational
databases, offering the
ablility to
connect to almost all databases.
The
JDBC-ODBC Bridge
The JDBC development team have supplied the
'JDBC-
ODBC Bridge' to take
advantage of ODBC's wide spread
adoption. The
bridge was created for several
reasons.
Because ODBC is written in 'C' it could
not be used
directly in Java.
Because of the extensive use of pointers
in ODBC,
the program did not translate
well directly into
Java.
// ODBC written in C with many pointers
didn't translate
// well into Java
In addition, calling ODBC directly
using Java's Native
Interface was
not a good solution as Java's portability
was
lost. There were also security concerns
to this
approach as the C code is 'outside'
Java's security
perimeter. ODBC
is also considered complicated and
hard to learn. Java's JDBC greatly
simplifies connecting
to a database making it easy to learn by comparison.
For these reasons, the Java team created the JDBC-
ODBC
Bridge that encapsulates
a Java connection to
the functionality of ODBC.
This was a clever way of
leveraging
the large support for ODBC for Java
developers.
Java
Code Sample
The following code
sample is from Peter Van der
Linden's 'Just Java' text. It is good as it ties in the
use of Microsoft's DSN, Java and Microsoft's Access
which is available on the machines at the school and
probably a large number of machines in the reader's
home.
Using Access
1) Run
Peter Van der Lindens simple class. First you will
have to
set up a Microsoft type DSN. Here are the steps if you are
not
familiar with it.
Following is a
guide to setting up a DSN to use with Access.
Setting up a
Microsoft DSN
1) open odbcad32.exe
in
c:\windows\system32
2) when the screen
opens,
press add
3) highlight
Microsoft Access
Driver (*mdb) & press Finish
4) in ODBC Microsoft
Access
Setup enter myDSN
5) the other field
is not
important
6) Press 'select' to
make
a database file to associate with the DSN
7) The file to find is under c:-> Program Files->
Microsoft Office -> Office 11
-> Samples -> Press
Enter ( Northwind.mdb shows up in left column )
8) Press OK
and it
should
be ready to go!
. |
Following is the Java code sample. It needs to be
compiled then run.
Java JDBC
Code Example
// from ' Just Java ' by Peter Van der Linden,
// Sun Microsystems Press
import
java.sql.*;
class
simple{
public static void main(String[]args){
String url = "jdbc:odbc:myDSN" ;
String query = "SELECT * FROM
Customers"
+ " WHERE CustomerID = 'QUICK' ";
try{
//
Load the jdbc-odbc bridge driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//
Attempt to connect to a driver
Connection con =
DriverManager.getConnection(
url," "," " );
//
Create a Statement object so we can submit SQL statements to the driver
Statement stmt =
con.createStatement(
);
//
Submit a query, creating a ResultSet object
ResultSet rs =
stmt.executeQuery(query);
//
Display all columns and rows from the result set
printResultSet(rs);
rs.close( );
stmt.close( );
con.close( );
}
catch(SQLException ex){
while (ex != null){
System.out.println("SQL Exception: " + ex.getMessage( ));
ex = ex.getNextException();
}
}
catch(java.lang.Exception
ex){
ex.printStackTrace( );
}
}
private
static void printResultSet(ResultSet rs) throws
SQLException{
int
numCols = rs.getMetaData( ).getColumnCount( );
while( rs.next( ) ){
for ( int i = 1; i < numCols; i ++ ){
System.out.print(rs.getString(i) + " | " );
}
System.out.println( );
}
}
}
To
Run The Java Example
Microsoft Access must be installed on your machine.
The DSN for the target database must be set up. Also,
you need to have the Java JDK installed and running.
If the 'javac' and 'java' commands are set up for use at
the command line the compilation would appear as
follows.
Example
>javac simple.java
If it returns without error report the compilation went
OK. To run the command would be as follows.
> java simple
The output is data from the Northwind database.
Running Java with MySQL
To run Java apps with MySQL, the driver needs to be
obtained from the MySQL site. The
jar file may then be
added to jdk1.5.0_04/jre/lib/ext directory of the Java
JDK.
Following is the change to the code noted above that
would reference the MySQL driver.
Example
Class.forName("com.mysql.jdbc.Driver");
In the case of MySQL the other change would be to
reference via a JDBC URL, the location of the MySQL
database.
Following is an example of what this location might
look like.
Example
String url =
"jdbc:mysql:///mysql/data/DB/";
VBScript
& ASP Connection to a Database
In the world of Microsoft programming, ASPs are popularly
used to connect to databases from the context of HTML
pages. The site can be set up using Access or FoxPro
and later migrated to MS SQL Server or other databases.
If the application is used online, a dedicated SQL server
may soon be needed.
To run ASPs with MySQL you will need to install the
MyODBC driver. To run ASPs, IIS or Personal Web
Server (PWS) is needed, provided free from the Microsoft
site.
Note an ADO database connection is being made. ADO
abbreviates Active X Data Objects.
The <% %> syntax 'escapes' the VBScript code into
the HTML domain. A single comma is a comment form
in VBScript.
The online tutorial, VBScript Database Tutorial shows
us the following essential pieces.
//
in the next complete example we see variables
// are declared as 'dim' type
Example // from VBScript Database Tutorial
<%
Session.timeout = 15
Set conn =
Server.CreateObject("ADODB.Connection")
conn.open
"ODBCDSNName","username","password"
Set Session("MyDB_conn") = conn
%>
Here is
one of the SQL Select examples that is
given in the tutorial. Notice the script supplies an
Open function for viewing the result set.
Example // from VBScript
Database Tutorial
<%
strSQLQuery = "SELECT * FROM tablename "
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strSQLQuery, conn, 3, 3
%>
Finally, the closing connection code is shown.
The tutorial recommends that closing should
be done at the end of each page for each
data source that is opened.
Example // from VBScript Database Tutorial
<%
conn.close
set conn = Nothing
%>
Putting the pieces together
Following is a complete example found in Leon
Atkinson's 'Core MySQL.
Example // from, Core MySQL, Leon Atkinson
<%@ LANGUAGE="VBSCRIPT %>
<% Option Explicit %>
<HTML>
<HEAD>
<TITLE>select.asp</TITLE>
</HEAD>
<BODY>
<%
dim connection
'Connection to database
dim
query
' Query
dim
rs
'Result Set
dim num_fields ' Fields in
result set
dim
i ' Loop variable
'Connect to server
set connection = server.createobject("adodb.connection");
connection.open "DSN=myserver"
'Execute query
query = "SELECT User,Host FROM user ORDER BY 1,2"
set rs=connection.execute(query)
'Get maximum
field number
last_field=rs.fields.count -1
'Start table
response.write("<table border=""1"" cellspacing=""0"">"
&vbcr)
'Print column
names
response.write("<tr> & vbcr)
for i=0 to last_field
response.write("<td><b>" & rs(i).name
&"</b/></td>" & vbcr)
next
response.write("</tr>" & vbcr)
'Fetch all
records
do while not rs.eof
response.write("<tr>" & vbcr)
'Print each
column value
for i = 0 to num_fields
response.write("<td valign=""top"">")
response.write(rs(i) & " ")
response.write("</td>" & vbcr)
next
response.write("</tr>" & vbcr)
'Get next
record
rs.movenext
loop
'Free result
set
rs.close
set rs=nothing
'Close
connection
connection.close
set connection = nothing
response.write ( "</table>" & vbcr)
%>
</BODY>
</HTML>
Mixing Languages
The VB script used as an example above points
out a problem, when scripts are intermingled with
other code. In fact we have three dialects going
on in the above. HTML, VBScript and SQL!
Two much mixing gets to be a maintenance
problem. In the world of HTML, Custom Tags
are being used to keep the script world isolated
from the HTML syntax.
Assignment
No assignment!
If there were an assignment it would be to
investigate the latest in creating database
clients using Microsoft's .NET environment.
An alternate option would be to investigate
creating PHP clients for MySQL. PHP has
long provided built-in support for MySQL.
Good Luck on the test!