Instructions to access MySQL from Tomcat

 

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

 

How to set up your Tomcat Environment for COP 5725

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

 

1) Log into margay.cs.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/margay/tomcat/install-tomcat-cop5993.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 COP5993 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-cop5993

 

Now installing Tomcat... Done!

 

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

 

http://margay.cs.fiu.edu:5004/

 

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

 

To start the server, run the script:

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

 

To stop the server, run the script:

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

 

C. Copy mysql-connector-java-5.1.8-bin.jar to the common/lib folder of tomcat, e.g., in my case in /home/tiger5/vagelis/tomcat-cop5993/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-cop5993/webapps/ROOT

If you can view it from a browser, create a folder inside ROOT and put it there.

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://margay.cs.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>