Click here to read the latest newsletter! This is an iEntry.com Website
Search iEntry News
Submit Your Site For Free!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

DatabaseProNews
SecurityProNews
ITmanagement








Hooking Up To A Database Using Servlets

By Dragos Mincinoiu
Expert Author
Article Date: 2002-03-07

In the first part of this article, I did not present any installation procedures and I felt that I left you in the cold. Therefore, I will do it here. Please keep in mind that the program presented here in not very difficult and if you cannot connect to the database, it is probably because of your setup.

The Database
The following example describes a command line application, (no GUI for you yet). We are going to use the following classes of the java.sql package: Connection, DriverManager, SQLException. We are connecting to a MySQL database that has the following structure:


As you can tell, the structure of the database is extremely simple; besides the name and email we store the VW models chosen, and some good stuff like the IP address and OS just for kicks.

My environment:
Windows 2000, JDK1.3.1, MySQL. The servlet container is JSDK2.1. The JDBC driver is mm.mysql-2.0.4-bin.jar.CLASSPATH is a User variable you need to create for your machine Control Panel/System/System properties/Environment variables/User variables (the upper box)
Set your CLASSPATH to: C:jdbc_driversmm.mysql-2.0.4-bin.jar;

jdbc_drivers is an actual directory on your drive c: the semicolon is extremely important; do not forget it. Well, there is some reasoning behind this; you are trying to tell your JVM look in that directory ( in this case C:jdbc_drivers ) There you will find your JAR file. So your directory can be Foo (the name really doesn't matter).

Variable "Path" is a System variable on your machine. You will find it under "System variables" Control Panel/System/System properties/Environment variables/System variables (the lower box) The Path variable for your machine should include C:jdk1.3in; do not forget the semicolon, it is really important. Before C: there should be another semicolon. So on Windows2000 you should use JDK1.3

Now, what is the structure of the program?



JSDK2.1 and the HTML code
The code for your guestbook.html should include the link to your servlet. The servlet host name is usually localhost; I call mine dragos. I have also changed the port number from 8080 to just 80; guess what? You don't have to type 8080 anymore; you just type the name of the local host. After you modify and recompile your code you will have to make sure you have the fresh version of your servlet class in your
webpagesWEB-INFservlets directory if you work in JSDK 2.1. If you have problems configuring Tomcat, you can try JSDK2.1 (an early version of Tomcat) or JRun.



I hope you found this useful and have enjoyed it. Java and JDBC offers very fast and convenient way handle database connectivity. You may say, "I can do this in PHP or ASP so why do it in Java?". If you are new to Java and know PHP, ASP or any other server side scripting language, use the language you know. I like Java, I had to write this, so I used Java.

About the Author:
Dragos Mincinoiu is a staff writer for iEntry.


Newsletter Archive | Article Archive | Submit Article | Advertising Information | About Us | Contact