Reviews :

Building a sample Web App with STRUTS Part 2 :

Setup the JDBC Driver

We will access the database using JDBC. For that, we need a suitable JDBC driver. Make sure that the jar file containing the driver is in your CLASSPATH. For simplicity, you could just drop the driver jar file under the WEB-INF/lib directory of your application.

Optional: JDBC Driver Setup for MySQL

Download the JDBC driver package - mysql-connector-java-2.0.14-bin from the MySQL download page. Extract the jar files out of the downloaded tar/zip file under the WEB-INF/lib directory, under your web app. For this article we are using the JDBC 2 API as we are not looking at the advanced features of JDBC 3.

Setup the database - create sample table

We will be adding Database access to the existing pages we created in part 1. We keep the part 1 functionality as it is and will display data from a table using struts.

We will create a sample table in the database, a simple EMPLOYEE table having these three fields: shortname CHAR(5), firstname VARCHAR(25), lastname VARCHAR (30). You can create this table directly in your database.

Optional: Create sample table in MySQL using the script

We have provided a sample SQL file, 'emp.sql' to create the table in MySQL and add some test data. To start the MySQL client, type 'mysql' at the prompt after going to the bin directory under MySQL install directory. Then at MySQL prompt type - " source emp.sql" to create table using this script. Make sure the emp.sql file is in the directory from where you started MySQL client.

OK! Now we are ready to change our web app for database access !!!

Adding the Database access code to the Action class

Add <data-sources> entry in strutsconfig.xml. It must be added prior to <action-mapping> or <form-bean>. This entry describes DataSources that are managed by the ActionServlet. Here is a sample entry for the MySQL driver we downloaded:
	<data-sources>
		<data-source>
		<set-property property ="driverClass"
    			value = "org.gjt.mm.mysql.Driver" />
		<set-property property ="url" 
		value = "jdbc:mysql://localhost/openstack" />
		</data-source>
	</data-sources>
If you are using another Database & driver, you will need to change the values for driverClass (the name of the driver class with complete path) and 'url' (access information). The 'url' will depend on the database you are using. For the MySQL database we are using a database instance called 'openstack' and no user id / password. Some other 'url' strings may have the RDBMS server - hostname, port number, instance name, along with the other information. Please refer to your driver documentation for this.

Now this data source is available to our web application in the "ServletContext", We will extract that in the ActionHandler.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.