The Remarkable Everyday

Struts – Database Access with struts-config.xml

Posted by leejeok on May 26, 2008

A sample program which connecting to the MySQL database to display the data. I am using the Struts framework to developed this program. The main idea is of this post is to use the struts-config.xml to define the datasource and using Action class to access and select the data.

Please bear in mind, the Action class which represent as controller layer, should not contain any business logic code and database access code. The best pratice is to apply the DAO mechanism by putting your business logic or database access code within the model layer.

The datasource should only define within the struts-config.xml when there is a limitation of connectivity to the database within the model layer. Alternative, you can use the JNDI to define the datasource and that would be the best recommendation in best pratice. However, that will be my next post on my blog.


Sample of the datasource used within the struts-config.xml:

        <data-source type=”org.apache.commons.dbcp.BasicDataSource” key=”dbSource”>
            <set-property property=”driverClassName” value=”com.mysql.jdbc.Driver” />
            <set-property property=”url” value=”jdbc:mysql://localhost:3306/bookstore” />
            <set-property property=”username” value=”someuser” />
            <set-property property=”password” value=”somepassword” />


Here are the summary of development been used.

  • Struts 1.2.9 framework
  • index.jsp, selectBook.jsp, SelectBookActionForm.java – view layer
  • SelectBookAction.java – controller layer
  • Book.java – data layer
  • Defining datasouce at struts-config.xml
  • MySQL database

Source Code: Click Here


5 Responses to “Struts – Database Access with struts-config.xml”

  1. […] Struts – Database Access with struts-config.xml […]

  2. I’m having trouble with my database test app. As a background, I’m writing
    my Action and ActionForm classes in Visual Age for Java, then exporting my
    classes to the WEB-INF\classes directory of my application. Within the lib
    directory I include struts.jar and the jdbc thin drivers in .zip format (if
    these are not needed here please tell me).

    I have tried to run this two ways. The first was using the datasource tag in
    the struts-config.xml file. I included it right after my action-mappings and
    form-beans tag, all of which are included inside the struts-config tags.
    When I start my Tomcat server, I receive an error in the console that says
    basically that there are illegal characters after my action-mapping tag ends
    (I.e. the datasource tag). The console will also give me a class not found
    exception at java.sql.Datasource. After I point my browser at the war file I
    get an error stating that it can’t find the ActionMappings or
    ActionFormBeans collection.

    The second way I tried to run this was by simply establishing a connection
    to the database manually in my Action class and using
    Class.forName(jdbcUrl). However, this time, when I try run the app it will
    crap out when I try to do the actual query and give me a class not found
    exception at Class.forName(jdbcUrl) at the Tomcat console. The browser gives
    me a null pointer exception. I think I have all the required imports within
    my classes.

    Using the datasource tag looks like it should be very straight forward but
    I’m having no joy. For anyone who has read this far, I apologize for the
    length of the message. I have included my code from the struts-config.xml
    file and my Action class perform() method.

  3. moeen said

    I tried this, but 404 error was generating. I found http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html which says it do not support datasource in strut.xml.

    I put simple connection and driver string in my action class, and it seems working.

    What’s the best. I m using Netbeans, Mysql.

    One more thing, How to pass the result set of many records to the view level.

  4. leejeok said

    Within the struts-config.xml, have you change the datasource name for the username and password?

    You had code the ResultSet within the Action class (“controller”). The best database practice is to open and close the ResultSet and database connection. After you pass the ResultSet from controller or model to view, you will get an error saying ResultSet been closed (if you tried to accessing it).

    For your case, perhaps you can execute the ResultSet and put the return values into list (eg: ArrayList, Map) of instances which is bean (model). In Action, put the list into scope (eg: session) and use the JSP to access the scope.

    By the way, you shouldn’t code the ResultSet within the controller. This would violate the MVC rules. Consider to put ResultSet within the model. Hope it help.

  5. wilson said

    if there will be a problem, like you can’t run it. You may try to add the libraries:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: