Instructions to access MySQL from Tomcat

 

A. To setup Tomcat in your UNIX account follow the instructions below (can also be found at /home/ocelot/tomcat/instructions.txt).

 

How to set up your Tomcat Environment

==================================================

 

1) Log into ocelot.aul.fiu.edu. Do not use any other machines for this class.

 

2) Make sure your JAVA_HOME environment variable is set to /depot/J2SE-1.5

 

   Using the tech shell (most users use this):

 

     setenv JAVA_HOME /depot/J2SE-1.5

 

   Using the bourne shell:

 

     export JAVA_HOME=/depot/J2SE-1.5

 

3) Run this script:

 

   /home/ocelot/tomcat/install-tomcat-cop4540.sh

   The script will provide additional instructions. It will also tell you

   which port is assigned to you.

 

Note that if you do not have your JAVA_HOME environment variable set correctly,

you will have problems running Tomcat.

 

 

B. Write down the info output by the script. For example in my case it is:

Welcome to the COP4540 Jakarta Tomcat installation script!

 

This script will install Jakarta Tomcat into your home directory so you can

develop JSP/Servlet applications. Tomcat will be installed into the directory:

 

/home/tiger5/vagelis/tomcat-cop4540

 

Now installing Tomcat... Done!

 

Tomcat installation is complete. The web page for your server is:

 

http://ocelot.aul.fiu.edu:5004/

 

Application directory: /home/tiger5/vagelis/tomcat-cop4540/webapps

 

To start the server, run the script:

/home/tiger5/vagelis/tomcat-cop4540/bin/startup.sh

 

To stop the server, run the script:

/home/tiger5/vagelis/tomcat-cop4540/bin/shutdown.sh

 

C. Copy mysql-connector-java-3.1.7-bin.jar to the common/lib folder of tomcat, e.g., in my case in /home/tiger5/vagelis/tomcat-cop4540/common/lib. This is the JDBC driver.

 

 

D. Create a jsp page to access your MySQL database

Put the file in the ROOT folder in the Application directory, that is, for me : /home/tiger5/vagelis/tomcat-cop4540/webapps/ROOT

Sample file (testmysql.jsp)

 

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

This program test connectivity of MySQL from JSP<br>

Contents of students table (created by  create table students (SSN char(9), name char(30),

primary key(SSN)) ) <br>

SSN  name<br>

-----------<br>

 

<%

String connectionURL = "jdbc:mysql://ocelot.aul.fiu.edu/vagelis_db?user=vagelis&password=892ngn2i";

 

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 students");

 

while (rs.next()) {

out.println(rs.getString("SSN")+" "+ rs.getString("name")+"<br>");

}

 

rs.close();

%>

 

</body></html>