MySQL Database Connectivity with JSP (Windows)

Submitted on: 1/2/2015 12:02:00 PM
By: Daniel M. Hendricks (from psc cd)  
Level: Intermediate
User Rating: By 31 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2)
Views: 22635
     A tutorial on how to get started with JavaServer pages using Sun's Tomcat web server (a Jakarta variant) and connecting to a MySQL database to retrieve data. Provided as a jumpstart for practicing with real-world applications. Tutorial is intended for users who may have had previous web/database experience but would like to get their feet wet in JSP. (Updated October 04, 2002)


This tutorial will show you how to connect to a MySQL database using JSP (JavaServer Pages) under Windows and Tomcat web server.  If you run into problems or find errors, please let me know so I can fine-tune this document.  This document will probably be most useful for those who have done web scripting with MySQL databases before but would like to get started with JSP/Servlets programming.  Database connectivity provides a good foundation for learning any new language, as you can practice making real-world applications in a database environment.


  1. MySQL
  2. Tomcat - version 4.1.12 Standard used for this tutorial
  3. Java 2 JRE - version 1.4.1 used for this tutorial
  4. MySQL Connector/J - version 2 used for this tutorial


  1. It is assumed that you already have a MySQL database installed and a table to pull data from.
  2. It assumes you understand SQL, and probably have done some web/database scripting with other languages.
  3. The author uses the folder C:\Tomcat as the folder where Tomcat will be extracted, however, you can place the distribution files anywhere you wish.
  4. You know the basics of programming Java.  If you do not, I highly recommend you check out Sun's Java Tutorial

Section A - Installation

  1. Install the Java 2 JRE.  I put mine in C:\java\jre, which will be used in this tutorial, but you can put your anywhere you like. 
  2. Extract the Tomcat distribution files to a folder.  The default is jakarta-tomcat-4.1.12, but I chose to put the files in C:\Tomcat.
  3. Copy the MySQL Connector JAR file to the C:\Tomcat\common\lib folder (ie, mysql-connector-java-2.0.14.jar).

Section B - Environmental Variables

Add the following environmental variables to Windows:


You can set environmental variables in Windows 2000/XP by going to:
Righy-click My Computer -> Properties -> Advanced -> Environmental Variables

You can set environmental variables in Windows NT 4 by going to:
Righy-click My Computer -> Properties -> Environment 

Section C - Working with Tomcat

To start the server, execute startup.bat.  To stop the server, execute shutdown.bat in the C:\Tomcat\bin folder.

By default, the Tomcat web server is access at this URL:

The root folder of the server is located in:

The root and default port can be changed in this file:

Section D - Write Your Code!

You can now start writing your JSP scripts.  Save it in a file with a JSP extension and place it in the ROOT folder.  I have provided a simple JSP script that demonstrates how to connect to a list data from a MySQL database.

<%@ page import="java.sql.*" %>

String connectionURL =
Connection connection =
Statement statement =
ResultSet rs =


connection = DriverManager.getConnection(connectionURL,
"", "");
statement = connection.createStatement();
rs = statement.executeQuery(
"SELECT * FROM mytable");

while ( {



Obviously, you will want to change the username and password to match your database.  Also, the mydatabase value in the connectionURL represents the name of the MySQL database.  Change appropriately.  Finally, change the mytable and myfield values in the script to match a table and field that exist within your database.  If the public is granted access to the database, you can use this as your connectionURL:

String connectionURL = "jdbc:mysql://localhost:3306/mydatabase";

Happy coding!

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.