Archive for category JSP Core
Make MySQL Connector/J Work with Tomcat
I have been using Tomcat with MySQL for a while, I remember when I first started using Tomcat, it took me a while to make my JSP script connect to MySQL using Connector/J.
1. Locate your Tomcat directory. If you have the recent version of Tomcat installed, you should be able to locate your Tomcat webapps folder:
for my Tomcat 5.5 installation, it looks like: tomcat5.5-webapps/
for my previous Tomcat 5 installation, it looks like: tomcat5/webapps/
2. Create a folder in your webapps, for instance: tomcat5.5-webapps/test/ or tomcat5/webapps/test/
3. Download Connector/J at http://dev.mysql.com/downloads/connector/j/5.1.html
4. Create a directory tomcat5.5-webapps/test/WEB-INF/lib/ or tomcat5/webapps/test/WEB-INF/lib/
5. Unzip or Untar the file, and copy the file mysql-connector-java-5.1.6-bin.jar
6. Paste it inside tomcat5.5-webapps/test/WEB-INF/lib/ or tomcat5/webapps/test/WEB-INF/lib/
7. Now create a test file named sql.jsp inside tomcat5.5-webapps/test/ or tomcat5/webapps/test/
8. Point browser to the url http://localhost:8080/test/sql.jsp, you should be able to see if it’s sucessfully connected to the MySQL database.
Tomcat JSP MySQL Connection Test Script
If you have JSP, MySQL and Jconnector installed, and wanna test if everything works okay, here is a simple script you can use to test.
<%@ page import = "java.sql.*"%>
<html>
<head>
<title>Obtaining a Connection </title>
</head>
<body>
<%
Connection conn=null;
ResultSet result=null;
Statement stmt=null;
ResultSetMetaData rsmd=null;
try
{
Class c=Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e)
{
out.write("Error!!!!!!" + e);
}
try
{
conn=DriverManager.getConnection("jdbc:mysql://192.168.110.5:3306/databasename","username","password");
out.write("Connected!");
}
catch(SQLException e)
{
System.out.println("Error!!!!!!" + e);
}
Change 192.168.110 from the above script to your url
databasename, username and password to your databasename, username and password.
Install and Configure Tomcat on Ubuntu
To install and configure Tomcat on Ubuntu Feisty or Hardy can be easily done.
1. Download and install the Java packages need for Tomcat to work (run in terminal):
sudo apt-get install sun-java6-bin sun-java6-jdk sun-java6-jre
2. Download and install the Tomcat packages (run in terminal):
sudo apt-get install libapache2-mod-jk libservlet2.4-java libtomcat5.5-java tomcat5.5 tomcat5.5-admin tomcat5.5-webapps
3. Open the .bashrc file (run in terminal):
sudo gedit ~/.bashrc
4. Add the following line in the opened file:
export JAVA_HOME=/usr/lib/jvm/java-6-sun
save and close the file
5. make the logs directory writable (run in terminal):
sudo chmod -R 777 /var/lib/tomcat5.5/logs
6. if your terminal is opened, close it and open up again
7. to start the tomcat, run the following command:
sh /usr/share/tomcat5.5/bin/startup.sh
8. now you can test to see if it’s working, point your browser at:
http://localhost:8180/
you should be able to see the tomcat splash page :)
9. to shutdown tomcat, use the following command:
sh /usr/share/tomcat5.5/bin/shutdown.sh










































