Connecting JSP page with MS Access :: JDBC ODBC Connectivity
Connecting JSP page with MS Access :: JDBC ODBC
ConnectivityHello Guys .This post is all about Connecting JSP to MS
Access database -> JDBC ODBC Connectivity.
Honestly speaking after struggling upon a lot on jdbc
connectivity , finally at the end of the day , I brought to you the
step by step process to CREATE database and perform DDL and DML
operations on that.
So , Lets begin by creating a MS Access database.
Go to Control Panel > Administrative Tools > ODBC DATABASE
SOURCES. Now an ODBC Data Source Administrator window will open.
Click on Add button .
Now Select Driver do Microsoft Access(*mdb) from the Create New
Database Source window that appears like the image shown on right
and click Finish Button..
Now create your Database source name . You can give any name you
want. THIS NAME IS CALLED YOUR DNS ( Database Sourcse Name ). Click
on "Create " button.
NOW THIS STEP IS Worth IMPORTANT. PEOPLE USUALLY GIVES C:\ DRIVE
PATH ( the NTFS where windows is installed generally).. There's no
problem but on some computers / windows version this POSE A
SECURITY PERMISSION PROBLEM SO WHATSOEVER CORRECT PROGRAM YOU WRITE
YOU WILL GOT AN EXCEPTION BECAUSE IT MAKES DSN READ-ONLY SO
EXCEPTION OCCURS. So to rectify this choose other drive for
database creation .Now once created choose Select Button and select
the database path you just created.This is it ! your DNS IS
CREATED.
Now come on to program .Here's a sample program .
NowString
database="e:\\xampp\\tomcat\\webapps\\ROOT\\lms\\lmshare.mdb";is
the path to database Source Name you just created .Notice I have
used e drive for the security problems I just discussed.String
url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" +
database + ";"; defines Ms Access and initializes its driver.- a
significant step.
To create database useString create="create table lmsusers(id
integer ,email varchar(20), pwd varchar(20),fname varchar(20),lname
varchar(20),age integer) ;";AdministrativelyRegisterDatabase1. Use
MS Access to create a blank database in some directory.
(eg.Database1.mdb.) Make sure to close the data base after it is
created.
2. Go to: Control panel -> Admin tool -> ODBC
3. Under the System DSN tab (for Tomcat version 5 or later User
DSN for earlier versions), un-highlight any previously selected
name and thenclickon the Add button.
4. On the window that then opens up, highlight MS Access Driver
& click Finish.
5. On the ODBC Setup window that then opens, fill in the data
source name. This is the name that you will use to refer to the
data base in your Java program likearc. This name does not have to
match the file name..
6. Then click Select and navigate to the already created
database in directory.Suppose the file name isDatabase1.mdb. After
highlighting the named file,click OKs all the way back to the
original window.
Connect a JSPpageto an Access Database
Connecting JSP to Microsoft Access
Introduction
All the web applications usually interact with different types
of databases. Databases are basically used to store various types
of information for different purposes. There are also different
types of databases used in the industry now days. But this is very
important that the relational databases are by far the most widely
used.
Generally the relational database uses the tables to represent
the information which it handles. A table consists of rows and
columns; row is identified with record whereas column is identified
with field name, so each column holds a single value of a
predefined data type. Generally these data types can be text data,
numeric data, dates, and binary data such as images and sound as
per our requirement. A dedicated language is called Structured
Query Language (SQL) that is used to access the data. The
Structured Query Language is an ANSI (American National Standards
Institute) standard and is maintained by all the major database
sellers. The Relational database engines come in all the shapes and
sizes. Structured Query Language API (Application Programming
Language) is used to execute structured queries in different
database engine. The standard Java libraries include an API
(Application Programming Language) called the Java Database
Connectivity (JDBC) API. Java Database Connectivity defines a set
of classes that can execute SQL statements, although the complexity
of databases can vary extensively.
Accessing a database from a JSP Page
Java Server Pages has Standard Tag Library which includes the
number of actions for the database access to improve the simple
database-driven Java Server Page applications. Basically these
actions are used to provide the following features:
Using a connection pool for better performance and
scalability.
The features are to support the queries, updates, and insertion
process.
To handle the most common data-type conversions.
To Support a combination of databases.
The Data Source Interface and JDBC Drivers
Applications get access to the database through an instance of
the Java Database Connectivity interface named
javax.sql.DataSource. The DataSource interface is the part of Java
2 Standard Edition (J2SE) 1.4, and for prior versions of the Java2
Standard Edition. Opening a new database connection is very
time-consuming. A nice thing with a DataSource is that it can
represent something which is called a connection pool. Using the
connection pool, a connection to the database is opened once and
uses it again and again. Basically a database action needs a
connection; it gets it from the pool through the DataSource object
and uses it to perform one or extra (Structure Query Language) SQL
statements. In that position when the action closes the connection,
the connection is returned to the pool where it can be used by the
next action that needs it.
The DataSource, the Java Database Connectivity Application
Programming Interface contains other classes and interfaces used by
Java applications to process SQL statements in a
database-independent way. For every database engine, an
implementation of the interfaces defined by the Java Database
Connectivity Application Programming Interface translates the
generic calls to a format specific to the engine. This
implementation is also called a JDBC driver. Through different
drivers which provides same interface allows us to develop our
application on one platform (for illustration, in the following
program a Personal Computer with an Access database), and then
install the application on another platform (for illustration, a
Solaris or Linux server with an Oracle database).
Illustrations shows how to handle the embedded quotes in a
string value, and how to deal with the input and output of date and
time values, semantics for certain data types, and creation of
unique numbers. The Java Server Pages Standard Tag Library actions
take care of some of these, like as the string quoting and the
date/time string format, so if we use these actions and stick to
American National Standard Institute SQL, we should be able to
voyage from one database to alternative database without too much
modification. Basically we should always read our database and
documentation very judiciously and attempt to stay away from the
proprietary things. So to be prepared, spend at least some time to
read the prerequisite to interchange the application from one DB to
another.
The context parameter of the value that contains four pieces of
information is separated by commas: a JDBC URL, a JDBC driver class
name, a database account name, and the account password.
The Java Database Connectivity URL identifies a specific
database. The different JDBC drivers use different Uniform Resource
Locator syntax as per requirement. All the JDBC URLs starts with
jdbc: and followed by a JDBC driver identifier [In JSP program we
are discussing this], such as (Open database connectivity) odbc:
for the JDBC-ODBC bridge driver and the mysql for the most
ordinarily used MySQL driver. The URL identifies the database
instance in the driver-dependent way. If we use an Access database,
we need to create a system DSN for the database using the Open
database connectivity. In the Windows Control Panel we must create
a system DSN as opposed to a user DSN.
Point to be remembered that the web server that executes our JSP
Pages usually runs as a different user account than the account you
use for development. If you specify a user DSN with your
development account, the web container will not be able to find
it.
System Data Source Name definition window and working process is
given below step by step:
1. At first go to Control Panel and Open Administrative
Tool.
2. After that Open ODBC driver by using double click on it.
3. Then click on add button.
Figure 1:Add data source window
1. Then give a name like sgc as shown below.
Figure 2:Provide data source name
2. Then select the database that is present in the specific
folder or path.
Figure 3:Enter database name and path selection
4. After selecting the database file, set the DSN as shown
below.
Figure 4:Set data source
Following the above process user can easily setup the DSN and
this DSN is implemented at the program segment like the
following
Connection
con=DriverManager.getConnection("jdbc:odbc:sgc","","");
Example of Database program using JSP
Here we will now discuss three sample examples like
registerinsert.jsp , updateprogram.jsp and registrationsearch.html.
These three programs are used for inserting record in the database,
updating record in the database, searching record in the database.
Here we are using DSN process which we have already discussed
above. Here we have also used MS Access database through which we
set DSN name sgc.
Listing 1:Sample showing sample registerinsert.jsp
Example of Java Server Page with JDBC
Code explanation:1. The above code is used to insert record in
to the database. Here we are accessing data from the entry form.
After entering the data, user can easily access the record through
the get parameter method.
2. After that, we set odbc driver and through the DSN (Which is
already discussed above) we link to the database.
3. Now the prepareStatement is use d to pass the DML insert
command. This command is used to store the record in the
database.
4. Now we set the string at the particular database filed.
This is the update program that is used to update existing
records in the database.
Listing 2:Sample showing updateprogram.php
JSP Page
Code explanation: This program mostly works as per the previous
system procedure model. Through this program model we can develop a
data uploading structure.
Here we use update command. This update command passes data
through a unique value.
After that if the updating is executed successfully, and then it
will redirect the page.
This is the searching program. Through this program model we can
search all the data that is already present in the database.
Listing 3:Sample showing registrationsearch.jsp
Photo gallery
- home
- about us
- Gallery
- log in
- register
- home
- about us
- Gallery
- Upload iamges
- Update Profile
- logout
Photo Gallery
Images By Category
::
Nature
Sports
Baby
Animals
60. 61. You need to edit the SELECT statement in
theexecuteQuerymethod and the column name in
thegetStringmethod.
Connecting JSP to Access through Apache TomcatYou can run
jsp-ms-access-example.jsp under Apache Tomcat to connect to Access
and retrieve data. For more information, see theJDBC-Access Driver
Getting Started GuideandAccessing ODBC Databases from Apache
Tomcat.