my dog learned polymorphism*
The moose likes Struts and the fly likes Database Pooling in Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Database Pooling in Struts" Watch "Database Pooling in Struts" New topic
Author

Database Pooling in Struts

Jose Esteban
Ranch Hand

Joined: Nov 28, 2004
Posts: 102
I'm following the tutorial from the book "Jakarta Struts Live� and I have problems connecting to my database (Struts 1.1):

================================
This is the code inside the Action class:
...
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

UserRegistrationForm userForm = (UserRegistrationForm) form;
DataSource dataSource = getDataSource(request, "userDB"); // *** ERROR: GETS A NULL ***
Connection conn = dataSource.getConnection();
...
================================
The data-sources in struts-config.xml:
...
<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource" key="userDB">
<set-property property="driverClassName" value="sun.jdbc.odbc.JdbcOdbcDriver" />
<set-property property="url" value="jdbc dbc atosStruts" />
<set-property property="username" value="jose" />
<set-property property="password" value="jose" />
</data-source>
</data-sources>
...
================================
The init-param in web.xml:
...
<init-param>
<param-name>config</param-name>
<param-value>WEB-INF/struts-config.xml</param-value>
</init-param>
...
================================

The problem seems to be that the code in the Action class (above)

DataSource dataSource = getDataSource(request, "userDB");

returns a null.

I've debugged the application and I've realized that, inside the Struts getDataSource method, the ServletContext doesn't contain the "userDB" attribute.

Any help?

Thanks.
Jose Esteban
Ranch Hand

Joined: Nov 28, 2004
Posts: 102
Never mind, everything was correct. It was my IDE which deleted the <data-source>.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database Pooling in Struts
 
Similar Threads
Nullpointer exception while accessing datasource in struts
Could not get the connection in struts database application
problem in accessing database
Database Access problem
Problem in inserting values to database from action class