Quick Search for:  in language:    
JSP,tutorial,started,with,JavaServer,pages,us
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Java/ Javascript Stats

 Code: 220,465. lines
 Jobs: 89. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login





Latest Code Ticker for Java/ Javascript.
Gobang
By Geniusbob Xu Qiang on 11/27


Click here to see a screenshot of this code!Salary
By Vikram Ivatury on 11/25

(Screen Shot)

Click here to see a screenshot of this code!A (part10) Powerful Swing Code to Maintain CD Database
By James Smith K on 11/25

(Screen Shot)

String Calculator
By MadokaCoder on 11/24


Chobi Dekha
By ShuvoRim on 11/23


Click here to see a screenshot of this code!A basic Client Server application II
By Ronald Holland on 11/23

(Screen Shot)

Bookmark image
By darren kurn on 11/22


myFT
By Owolabi Oyapero on 11/22


Click here to see a screenshot of this code!Simple Socket example
By Steven McElrea on 11/20

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

MySQL Database Connectivity with JSP (Windows)

Print
Email
 

Submitted on: 4/27/2001 4:08:25 PM
By: Daniel M. Hendricks  
Level: Intermediate
User Rating: By 17 Users
Compatibility:Java (JDK 1.1), Java (JDK 1.2)

Users have accessed this article 45674 times.
 

 
     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)


 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

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.

Requirements:

  1. MySQL
    http://www.mysql.com
  2. Tomcat - version 4.1.12 Standard used for this tutorial
    http://jakarta.apache.org/site/binindex.html
  3. Java 2 JRE - version 1.4.1 used for this tutorial
    http://java.sun.com/j2se/1.4.1/download.html
  4. MySQL Connector/J - version 2 used for this tutorial
    http://www.mysql.com/downloads/api-jdbc.html

Assumptions:

  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:

  JAVA_HOME=C:\java\jre
TOMCAT_HOME=C:\Tomcat

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:
http://localhost:8080/

The root folder of the server is located in:
C:\Tomcat\webapps\ROOT

The root and default port can be changed in this file:
C:\Tomcat\conf\server.xml

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 =
"jdbc:mysql://localhost:3306/mydatabase?user=;password=";
Connection connection =
null;
Statement statement =
null;
ResultSet rs =
null;
%>

<html><body>

<%
Class
.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL,
"", "");
statement = connection.createStatement();
rs = statement.executeQuery(
"SELECT * FROM mytable");

while (rs.next()) {
out.println(rs.getString(
"myfield")+"<br>");
}


rs.close();
%>

</body></html>

 
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 notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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
5/4/2001 9:59:33 PM:ahfook
very good tutorial for JSP beginner.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/13/2001 12:57:44 PM:hui
it would be perfect if there is some concise explaination on each line of code since the codes are not too much. Thanks and keep up the good work!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/16/2001 12:00:54 PM:Shawn
hi, i'm new to MySQL and JSP, i am currently using JSWDK instead of tomcat, but the codes doesn't seem to work for me..
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/3/2002 8:34:16 PM:allaine
Hi! I'm using jdk1.2 and win98. How can I set environment variables for win 98?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/16/2002 1:58:16 AM:Me
Hi, I've tried to test the above codes, but somehow I got this error message "javax.servlet.ServletException: org.gjt.mm.mysql.Driver" on the first line. Do you know what is wrong?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/12/2002 3:58:05 AM:Vincent Chan
Excellent tutorial for beginners who want to setup their web-based application. Thank you very much.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/2/2002 9:41:38 AM:Missing Info
The only thing missing is you need to put the mm.mysql jar files in the /WEB-INF/lib directory. This is probably why people are getting the ServletException.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/13/2002 3:31:52 PM:Bogdan Pomerlyan
You are the man: Add the following environmental variables to Windows: JAVA_HOME=C:\java\jdk TOMCAT_HOME=C:\To mcat You can set environmental variables in Windows 2000 by going to: Righy-click My Computer -> Properties -> Advanced -> Environmental Variables I have been searching where I could add that for the past hour, thanks for putting that in your work. I greatly appreciate it.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/1/2002 2:28:24 PM:Mike
Hi I've seemed to have followed those instructions above correctly but i'm still get that same error "javax.servlet.ServletException: org.gjt.mm.mysql.Driver" does anyone know why? I think it's the placement of the .jar file. I'm not exactly sure what this file does or where it's supposed to go, and if there is anything else i'm supposed to do to get this file to work with MySQL and Tomcat. Any help or ideas would be appreciated . THanks
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/28/2002 8:01:04 AM:
hi, the code does not work with my server.i think that is because of the older version of tomcat and jre. but i am not sure. can you help me? thanx
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/6/2002 10:31:00 AM:
I can use this driver code for select query but when I use Add query it's show me error that Unable to load driver can you help me? Thank you
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/17/2002 8:29:30 PM:
Wondrful! I implanted it into java code (I call it Sql.java)using in Tomcat Servlet engine. It works very well. (I spent long time on the web and didn't figure it out until I read this article.) Thanks a lot!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/3/2002 9:20:34 PM:
Do we just need to
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/3/2002 9:22:41 PM:
Do I just need to "Copy the MySQL Connector JAR file to the C:\Tomcat\common\lib folder (ie, mysql-connector-java-2.0.14.jar)."? How about the rest of the files in the "mysql-connector-java-2.0.14" folder? pls help!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/8/2002 10:16:10 PM:
can i save my file to folder other than the ROOT folder in tomcat?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/10/2002 11:05:30 AM:
Nice and Concise!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/14/2002 3:40:43 PM:
It worked for me perfect!! altho I didnt have a common/lib folder. I figured that out though. Probably different versions. YAY I can do some coding now.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/18/2002 1:06:08 PM:
Its great. I'm a new and I manage to get this thing works at once. Keep up the good work. This sample is easy to understand. =)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/21/2002 11:10:45 AM:
I got exceptions at first, but realized in the connection URL, you need a '
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/21/2002 11:12:00 AM:
I was getting exceptions until I realized that the connection URL requires a & between the user=username;password=password, not a semicolon. Great tutorial, thanks!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/30/2002 4:03:47 AM:
Good Job! It was very helpful in getting started with jsp, mySQL and Tomcat. I had to place the mySQL Connector jar in the jre\lib\ext subdir of j2sdk to work though. For some reason it just wouldn't work from tomcat common\lib subdir. Thanks!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/4/2003 4:42:19 AM:
it is very helpul for begineer like me. Thanks alot
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/21/2003 7:08:03 PM:
Hi, I keep getting error when I try to start tomcat. Something is missing from the java.endorsed.dirs and I don't know what's else I need. Originally, the fold is empty when i download Java 2 JRE - version 1.4.1 What else do I have to put into that directory? Please help
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/30/2003 12:58:26 PM:Tristan
set the java.endorsed.dir environment variable. In xp i have it as variable: java.endorsed.dir value: C:\jwsdp-1.1\jaxp-1.2.2\lib\endorsed if you use tomcat that would be, value: C:\TOMCAT_HOME\jaxp-1.2.2\lib\endorsed
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/12/2003 10:37:44 AM:
This is a very good tutorial...I had a problem where tomcat would shut itself as soon as I access mysql...but now everything works fine as I put the connector jar files in the told directories. Even the feedback from the other users is so helpful!Thanks!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/31/2003 10:43:44 AM:
This is a very good Article Thanks. If any of you need more information go to www.bivius.com
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/20/2003 4:51:59 PM:
Hi, thanks much for the clear
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/31/2003 12:59:28 PM:
Hi I found this code very easy to use the only other thing is where can i find similar tutorial on using Jsps that work for varying queries of a database?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Java/ Javascript Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.