Java Beans and JDBC
1 posts in topic
Flat View  Flat View

Posted By:   Joel_Powell
Posted On:   Wednesday, July 31, 2002 01:05 PM

I've created code for connection to my database using a controller servlet and a JSP that actually opens and closes the connection. I was told it is better (part of the MVC pattern) to use Beans to accomplish this. Could I get a simple example of how to implement beans with my servlet and JSP.

Re: Java Beans and JDBC

Posted By:   Erik_Runia  
Posted On:   Wednesday, July 31, 2002 01:16 PM

I hide all my database connection code (as well as data loading) in my bean, and simply initialize it in my jsp at which point it connects, loads my data into the bean and disconnects. The jsp code is as follows:

<%@ page import = "com.blah.beans.*" %>

where myBeanClass is the name of your bean class file, myBean is the name of your bean if you need to use it in the jsp file and session will keep your bean alive for the life of the session object. the import should point to the package you keep your beans in.

if you put methods in your bean to manipulate the data stored in it you can access them in the jsp like so:

In your beans init code simply create the connections and do what you need to do and it will be ready to go for use within your jsp, just do the useBean call up top of your jsp file.

<%= myBean.myMethod(); %>

Hope this helps,


About | Sitemap | Contact