This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do you use a bean for creating database connection?

 
Raghavan Chockalingam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wondering whether people develop a bean for making a database connection. I was thinking of creating of a bean that would return a Connection or Statement object and I came up with this java program but there is a problem when I try to include the bean in my jsp program.

java program...DbBean.java...placed in web-inf/classes/Utilities


Jsp file...carBean.jsp



Errors....taken from log file..

[ January 03, 2006: Message edited by: Raghavan Chockalingam ]
 
Balasubramani Dharmalingam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghav,
you have used default access to DbBean class so it is not visible to the JSP. so change access specifier for DbBean class as public.
 
Raghavan Chockalingam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that...now I have changed it to public and compiled again but I have the same error even then...

I want to know the .java file is a valid bean???


[ January 03, 2006: Message edited by: Raghavan Chockalingam ]
 
Balasubramani Dharmalingam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghav,
I tried the samething here. I am also getting same error.But after changing the access as public the error has gone.
did u restart the server after compiling the DbBean class ?
 
Amarender Reddy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavan,
Can you make sure that the given error is thrown excatly the line :



cheers
 
Raghavan Chockalingam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks both of you guys for your help and I did not know earlier that I should restart the server.

1. Is it always necessary to restart the server whenever there is a revision with the bean class?

2. I now have a logical problem, if the bean creates a Connection or a Statement object then how do I use those objects in the .jsp file???

For example....
<jsp:getProperty name="myConnection" property="stmt" />

the above statement gives a Statement object but how do I assign this object to a Statement object created in jsp scriptlet.



If you think I am totally wrong with the way I am thinking, can you tell me how do you establish/ use database connections in your applications. It would be very kind if you can me a working example of yours...thank you...
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to 1) It is not necessary to restart the server if a class changes, but it is necessary to restart the web app that contains that class.

to 2) It is considered bad design to be using java.sql objects in a JSP page (and has practical drawbacks as well, like making exception handling hard). At a minimum, perform all the DB access in the bean class, and just retrieve the results from it in the JSP.
 
Raghavan Chockalingam
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf for your advice, can you tell me how to restart the Web application alone???
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghavan Chockalingam:
Thanks Ulf for your advice, can you tell me how to restart the Web application alone???


The Manager app that ships with Tomcat can be used for restarting applications.
http://tomcat.apache.org/tomcat-5.5-doc/manager-howto.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic