File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Make Connection to Database in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Make Connection to Database in JSP" Watch "Make Connection to Database in JSP" New topic
Author

Make Connection to Database in JSP

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I have the code to connect to databases from Servlet. My question is how we make connection to database in JSP. Do we use those servlet code as scriplet in JSP? I mean simply put those servlet code within <% %>?
sameera kocharekar
Greenhorn

Joined: Apr 23, 2001
Posts: 23
hi there
well yes this is one solution to write the code to connect to the database in <% %> tags
or else
u can create a connection bean wherein u can pass the database name or/and dsn name or/and url(sometimes needed in some drivers)
and the use this bean to create a connection and pass these parameters at runtime
then u can use the bean object further
Sameera


SCJP
Mohamed Yousuff
Ranch Hand

Joined: Jun 23, 2001
Posts: 73

Usually connection to the database is done through connectionpooling packages. If you have done it through servlets, then I hope it will be the init method. If so then you can copy and paste the code as follows :

<%!
Connection con ;
public void jspInit(){
// copy and paste the code here.
}
%>

------------------
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
I strongly recommend that you write ALL of your DB access code as a JavaBean - that way you can test it outside the JSP environment. JSP are hard enough to debug as it is.
There are also some nice Taglib for database access that you might be able to use. Try jakarta.apache.org and/or www.orionserver.com for free taglib.
Bill
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thanks to all of you for useful and helpful suggestions.
vasudevang
Greenhorn

Joined: Jul 10, 2001
Posts: 4
It is better to create a bean for connection and return the connection object,so no need to write connection statement for
every jsp page
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Make Connection to Database in JSP