Professional Documents
Culture Documents
Connectivity
Hello 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 .
<%@page import="java.sql.*"%>
<%@ page import ="javax.sql.*" %>
<%String database="e:\\xampp\\tomcat\\webapps\\ROOT\\lms\\lmshare.mdb";
String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + database + ";";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(url,"","");
Statement stmt=con.createStatement();
// CREATION
String create="create table lmsusers(id integer ,email varchar(20), pwd varchar(20),fname
varchar(20),lname varchar(20),age integer) ;";
stmt.executeUpdate(create);
//INSERTION
String adduser="insert into lmsusers(email,pwd,fname,lname,age) values ('"+name+"', '"
+pwd+"','"+fn+"','"+ln+"',"+age
+");";
stmt.executeUpdate(adduser);
out.println("connected");%>
Now String 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 use String create="create table lmsusers(id integer ,email varchar(20),
pwd varchar(20)
,fname varchar(20),lname varchar(20),age integer) ;";
Under the System DSN tab (for Tomcat version 5 or later User DSN for
earlier versions), un-highlight any previously selected name and then click on
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
like
arc. 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 is Database1.mdb. After highlighting the named file,
click OKs all the way back to the original window.
Connect a JSP page to an Access Database
<%@ page import="java.sql.*" %>
<%
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=null;
conn = DriverManager.getConnection("jdbc:odbc:arc", "", "");
out.println ("Database Connected.");
%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>Example of Java Server Page with JDBC</title>
</head>
<body>
<%
String u=request.getParameter("userid");
String p=request.getParameter("password");
String n=request.getParameter("sname");
String e=request.getParameter("eid");
String a=request.getParameter("addr");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:sgc","","");
/* Passing argument through the question mark */
PreparedStatement ps=con.prepareStatement("insert into login
values(?,?,?,?,?)") ;
ps.setString(1,u);
ps.setString(2,p);
ps.setString(3,n);
ps.setString(4,a);
ps.setString(5,e);
int i=ps.executeUpdate(); /*Set the Update query command */
if(i!=0)
{
response.sendRedirect("index.jsp?msg=Thank You
for registering with us in Mrbool !");
}
else
{
response.sendRedirect("registerinsert.jsp?
msg=Insertion Failed!! Please try again!!!
");
}
con.close();
}
catch(Exception ex)
{
out.println(ex);
}
%>
</body>
</html>
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
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
Profile
</html>
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.
%>
</div>
<div id="wrap">
<%
String uid=(String)session.getAttribute("u");
if(uid==null)
{
%> <div class="header">
<div class="logo"><a href="index.jsp"><img
class='imagem_artigo' src="images/pg.gif" width="294" height="73"
/></a></div>
<div id="menu">
<ul>
<li class="selected"><a href="index.jsp"
class="style12">home</a></li>
<li><a href="aboutus.jsp" class="style12">about us</a></li>
<li><a href="gallery.jsp" class="style12">Gallery</a></li>
<li><a href="myaccount.jsp" class="style12">log in</a></li>
<li><a href="register.jsp" class="style12">register</a></li>
<li></li>
<li></li>
</ul>
</div>
<%}
else
{
%>
<div class="header">
<div class="logo"><a href="index.jsp"><img
</div>
<div id="Layer8">
<div class="footer"></div>
</div>
<div class="new_products"></div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<div id="Layer4">
<h1>Photo Gallery</h1>
</div>
<div id="Layer5">
<span class="style4"><font face="Algerian" size="5">Images By
Category</font></span>
::<br />
<font size="3" face="Courier" color="#CC0033" align="center">
</font><font size="3" color="#CC0033" align="center"><span
class="style12"><a href=" registrationsearch.jsp?
Category=Nature">Nature</a><br />
<a href=" registrationsearch.jsp?Category=Sports">Sports</a><br />
<a href=" registrationsearch.jsp?Category=Baby">Baby</a><br />
<a href="search.jsp?Category=Animals">Animals</a></span></font><font
size="3" face="Courier" color="#CC0033" align="center"><br />
</font></div>
<div id="Layer7">
<%
String cat=request.getParameter("Category");
int i=0;
ArrayList ar=new ArrayList();
array list.
ArrayList pi=new ArrayList();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
cn=DriverManager.getConnection("jdbc:odbc:sgc","","");
PreparedStatement ps=cn.prepareStatement("select *
from photoDetails where pct=?");
ps.setString(1,cat);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
i++;
String pUrl02=rs.getString(5).trim();
String
pUrl2=pUrl02.substring(pUrl02.lastIndexOf("\\")+1);
ar.add(pUrl2);
pi.add(rs.getString(1));
}
out.print(i+cat);
cn.close(); /*Close the connection*/
}
catch(Exception e){
out.println(e);
}
%>
<table bgcolor="orange" border="5" width="550" height="220">
<%
int j=0,k=0,t=0;
while(k<4)
{
%>
<tr>
<%
j=0;
while(j<3 && j+t<i)
{ %>
z-index:1;
left: 261px;
top: 181px;
background-color: #9900CC;
}
#Layer4 {
position:absolute;
width:891px;
height:132px;
z-index:3;
left: 264px;
top: 182px;
background-color: #009900;
}
#Layer5 {
position:absolute;
width:217px;
height:266px;
z-index:4;
left: 939px;
top: 318px;
background-color:
#FC3;
}
#Layer6 {
position:absolute;
width:678px;
height:508px;
z-index:5;
left: 145px;
top: 334px;
}
#Layer7 {
position:absolute;
width:673px;
height:1038px;
z-index:5;
left: 263px;
top: 316px;
background-color: #CCCCCC;
}
#Layer8 {
position:absolute;
width:898px;
height:107px;
z-index:6;
left: 262px;
top: 1240px;
background-color: #EFEFEF;
}
#Layer9 {
position:absolute;
width:429px;
height:79px;
z-index:7;
left: 421px;
top: 232px;
}
.style2 {
font-family: Algerian;
color: #000000;
}
.style4 {color: #000000}
.style12 {font-family: algerian}
Code explanation:
1. This is the searching program.
2. Select query is used here for searching the record. Here
we are searching picture wise.
3. rs is the ResultSet in the program segment. Through the
result set process we can easily search the record.
This JSP page can work with all possible Databases like MS
Access, MySql, Oracle, and Microsoft SQL Server. In this Article
it is very much clear that JSP technology can work with JDBC
ODBC driver. Through this driver process we must set up DSN
connection. Software developers and designers use it widely
for more portable and reliable database connection process.
If your JSP capable web server or application server runs on Windows, use
the JDBC-Access Driver.
If your JSP capable web server or application server runs on Linux or Unix,
use the JDBC-ODBC Bridge (JOB).
Contents
ODBC application. Use one of the test applets included in the JOB distribution to
check that you can access the ODBC data source through JOB.
For more information about installing JOB and testing it with Microsoft Access,
see Accessing Microsoft Access Data from any Java Platform. Refer also to the
Installation and Configuration chapters of the JOB User Guide.
Install the Easysoft Java Archive file (.jar) into the application you run JSP
pages under. For example, if you are using Apache Tomcat on Windows, copy
esmdb.jar to $CATALINA_HOME\lib; if you are using Apache Tomcat on Linux or Unix,
copy EJOB.jar to $CATALINA_HOME/common/lib.
esmdb.jar is the Java component of the JDBC-Access Driver. The file is located
in drive:\Program Files\Easysoft Limited\JDBC-Access Driver on the JDBC-Access
Driver machine, where drive is the relevant drive letter, for example C.
EJOB.jar is the Java component of JOB. The file is located in drive:\Program
Files\Easysoft\Easysoft JDBC-ODBC Bridge\jars on the JOB Server machine,
where drive is the relevant drive letter, for example C.
2.
In your application, create a JDBC data source that connects to the target
Microsoft Access data source.
The JDBC data source needs to specify the appropriate Easysoft JDBC driver class:
JDBC Driver
Class
JDBC-Access Driver
easysoft.sql.esMdbDriver
JOB
easysoft.sql.jobDriver
The JDBC data source needs to specify the appropriate Easysoft JDBC driver
connection URL:
JDBC Driver
Connection URL
JDBC-Access jdbc:easysoft:mdb?DBQ=path[;odbc-driver-attribute=value]
Driver
where:
jdbc:easysoft://hostname:port/access_system_data_source
:logonuser=username:logonpassword=password
where:
If you are required to supply a database user name and password when configuring the JD
name and password as the ones in the connection URL.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
file:
Use jdbc/MyDB as the JNDI name for the JDBC data source.
Create a JSP page named jsp-ms-access-example.jsp. Add these lines to the
<html>
<head>
<title>JSP MS Access Example</title>
</head>
<body>
<%@ page import="javax.naming.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<h1>JSP MS Access Example</h1>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our data source
DataSource ds = (DataSource) envCtx.lookup("jdbc/MyDB");
// Allocate and use a connection from the pool
conn = ds.getConnection();
// Fetch and display data
stmt = conn.createStatement();
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.