advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs