Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Make Connection to Database in JSP

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mohamed Yousuff
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you for useful and helpful suggestions.
 
vasudevang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic