aspose file tools*
The moose likes JSP and the fly likes Problem accessing Database from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Problem accessing Database from JSP" Watch "Problem accessing Database from JSP" New topic
Author

Problem accessing Database from JSP

Gaurav Pawar
Ranch Hand

Joined: Sep 12, 2006
Posts: 37
Hi all,
I have a sample Java Program that manipulates a MS Access Database. I have now written JSP code to perform the same functions (insert/ delete.) on the database.

I have my class files under WEB-INF/classes and the database in webapps/MyApp/myDB.mdb

Could you please help me with the following.
1. Is the database placed in the right path.
2. Do i have to place the driver in the WEB-INF/lib? if so which drivers. I did search a lot but could not find a good reference

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dataSourceName = strDSN;
String dbURL = "jdbcdbc:" + dataSourceName;
Connection con = DriverManager.getConnection(dbURL, "","");
Statement s = con.createStatement();
s.execute(strQuery);

This is how the code in my java class looks like. This class is called from the JSP.

Can anyone please give me guidelines/ sample example on a JSP accessing a (ms acess) database deployed in Tomcat?

Thanks.
Gaurav
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

It is inappropriate to do DB access from a JSP. You should be using the very non-UI classes that you've already set up in a reusable manner. But yes, once you find out what the correct drivers are, they will go in WEB-INF/lib.
[ March 17, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

If the driver you plan to use is "sun.jdbc.odbc.JdbcOdbcDriver" then you don't have to put it anywhere. It is already part of the JRE. (Unless you are using a JRE not written by Sun, in which case you will not be able to use it at all.)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

So what error are you getting that leads you to believe that you have a driver issue?
Gaurav Pawar
Ranch Hand

Joined: Sep 12, 2006
Posts: 37
Hi,
Well my code for accessing the database is in a java class called Starter.java which has a function




and this is what getData() does.


This class is in webapps/MyApp/WEB-INF/classes/core

In my JSP i have the following code.

<%@page import="core.Starter"%>
<%

Starter StMap = new Starter();
StMap.InitializeMap();

%>

The Error: org.apache.jasper.JasperException: Array index out of range: 0


The problem is this works fine as a standalone application.. but after deployment

I have put the database in webapps/MyApp

When i run this in my browser as http://localhost:8080/MyApp/Map.jsp
it gives the error


org.apache.jasper.JasperException: Array index out of range: 0
[ March 18, 2007: Message edited by: Gaurav Pawar ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

When posting code to the forums, please use UBB code tags. Read this for more information.

It's just too hard to read unformatted code.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

You can go back and add them to previous posts by clicking the icon. Hint, hint.
Gaurav Pawar
Ranch Hand

Joined: Sep 12, 2006
Posts: 37
Hi,

I got the solution to this one. I had declared the 'Data Source Name' as User DSN. I then tried declaring it as a System DSN and it worked.

Thanks,
Gaurav
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem accessing Database from JSP