aspose file tools*
The moose likes Servlets and the fly likes Tomcat and Mysql Configuration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Tomcat and Mysql Configuration " Watch "Tomcat and Mysql Configuration " New topic
Author

Tomcat and Mysql Configuration

Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
I have very simple question.
I have installed Mysql on my Windows 98 system and it is running fine.
My question is how to set the classpath in tomcat.sh file as Mysql is in the format of zip file.
So I should either rename it (e.g.mysql.zip to mysql.jar) and then add to the tomcat.sh file as
Set classpath = c:\tomcat\mysql\mysql.jar
A bit confused.
And the book I am reading it says you hvae to make changes to server.xml file also and add
<context path = bla...etc. />
I have no idea what the book is trying to say and how to add the classpath for mysql driver.
I am sure anyone will be able to help me.
Any help will be appreciated.
Thanks

Maky Chopra
Ranch Hand

Joined: Apr 11, 2001
Posts: 149
Hi
I dont have much exposure to mysql but i do not think you would have to add the mysql.zip / jar to your classpath.
What you would need is the mysql jdbc driver.. Then you should be loading the driver in your servlet/jsp/javabean/java app using similar code

mm.mysql is a very popular JDBC driver for mysql which is available from the mysql site.. You would need the driver jar files in your classpath while compiling and running.
Dont think any changes are required for your server.xml file.
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
What do you mean by <db-url>?
Thanks once again for looking into my problem.
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
Forget about my first question. I figured it out.
It gives error at this line.
DriverManager.registerDriver(new Driver());
I don't know why???
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
Exact error message is as follows:
org.apache.jasper.JasperException: Unable to compile class for JSPC:\Tomcat\jakarta-tomcat-3.2.1\work\localhost_8080%2Fanshu\_0002fdbqueryP_0002ejspdbqueryP_jsp_1.java:65: interface java.sql.Driver is an interface. It can't be instantiated.
DriverManager.registerDriver(new Driver());
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
How to register the driver for mysql?
Any idea???
Maky Chopra
Ranch Hand

Joined: Apr 11, 2001
Posts: 149
db-url would be the url of the database.. for ex. if you have an access db, and you create a DSN connection to it (thru control panel -> odbc) called anshudb, your db-url would be anshudb
if you dont create a DSN connection, it could be the physical path.. saw, c:\database\anshudb.mdb
Remove the driver register statement.. class.forname does whatever is necessary.
*****************************************
Here's some data I found..
Establishing a Connection
The first thing that needs to be done in order to work with JDBC is to obtain a Connection object that represents the network connection to the database that contains the information you want to work with.
With JDBC, the DriverManager manages the establishment of Connections. The DriverManager needs to be told which JDBC drivers it should try to make Connections with. The easiest way to do this is to use Class.forName() on the Class that implements the java.sql.Driver interface. With MM.MySQL, the name of this Class is org.gjt.mm.mysql.Driver.
Example 3-1. Registering the Driver with the DriverManager
import java.sql.*;
// Notice, do not import org.gjt.mm.mysql.*
// or you will have problems!
public class LoadDriver
{
public static void main(String[] Args)
{
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception E) {
System.err.println("Unable to load driver.");
E.printStackTrace();
}
.....


Once the driver is registered, a Connection can be established. Obtaining a Connection requires a URL for the database. This URL is constructed using the following syntax, where items contained in sqaure brackets are optional:

jdbc:mysql://[hostname][ ort]/dbname["> aram1=value1][=value2]...



The following parameters can be specified after the "?" in the URL:
Table 3-1. URL Parameters
Name Use Default Value
user The user to connect as none
password The password to use when connecting none
autoReconnect should the driver attempt to re-connect if the connection dies? (true/false) false
maxReconnects if autoReconnect is enabled, how many times should the driver attemt to reconnect? 3
initialTimeout if autoReconnect is enabled, the initial time to wait between re-connects (seconds) 2
maxRows The maximum number of rows to return (0 means return all rows) 0
useUnicode should the driver use Unicode character encodings when handling strings? (true/false) false
characterEncoding if useUnicode is true, what character encoding should the driver use when dealing with strings? none
Once the URL has been specified, it is passed to the DriverManager.getConnection() method to obtain a Connection Object:
Example 3-2. Obtaining a Connection from the DriverManager
...
try {
Connection C = DriverManager.getConnection(
"jdbc:mysql://localhost/test?user=monty=greatsqldb");

// Do something with the Connection

....
}
catch (SQLException E) {
System.out.println("SQLException: " + E.getMessage());
System.out.println("SQLState: " + E.getSQLState());
System.out.println("VendorError: " + E.getErrorCode());
}


Once a Connection is established, it can be used to create Statements and PreparedStatements, as well as retrieve metadata about the database. These functions are explained in the next section.
for the java.sql error, could you post your code here ?
[This message has been edited by Mak Bhandari (edited May 04, 2001).]
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
Error is something like this:
Unable to load driver.
java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
at mysql.LoadDriver.main(LoadDriver.java:19)
When I try to execute the first code as mentioned by you.
Where did u pulled out this info?
If u any link I can read it.
I think I am again going to stuck as I was stuck for the first time for running my first servlet.I hope you remember.
Thanks for helping me.

Maky Chopra
Ranch Hand

Joined: Apr 11, 2001
Posts: 149
No problem... its friday and I have an hour and half to kill before i leave this place
could you post your code here ?
[This message has been edited by Mak Bhandari (edited May 04, 2001).]
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
Well I have mysql running fine on my system but not able to access it thru the java code.
Here is my code.
package mysql;
import java.sql.*;

public class LoadDriver
{
public static void main(String[] Args)
{
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception E) {
System.err.println("Unable to load driver.");
E.printStackTrace();
}

}
}
Thanks for your help.
clyde jones
Ranch Hand

Joined: Dec 02, 2000
Posts: 46
Seema,
What book are you reading for MySQL? I have a windows 98 machine and plan on using MySQL with a personal jsp project after I configure Apache/Tomcat to work together. I would like to know the name of the book you are reading for MySQL.
Thanks,
Clyde
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
Well I am not reading any book on mysql but I figured it out how to install and run it of my own.
Today only I figured out basic stuff i.e. Create database, tables, insert data into tables et. and I can see on screen the data inserted but unable to figure out how to configure with JAVA and JSP.
Once I come to know how to configure the driver with JAVA and JSP then I am done with mysql.
Rest if you need any info about basic stuff do let me know.
Have u installed Tomcat on your machine? If you need any help in that also do let me know as I have gone thru the installation problem.
Maky Chopra
Ranch Hand

Joined: Apr 11, 2001
Posts: 149
Your code looks fine to me.. I had the mm.mysql jdbc driver and it executed fine.. Before you java LoadDriver from c:\, set CLASSPATH=%CLASSPATH%;c:\jdbc (i unzipped the mm.mysql JDBC driver into c:\jdbc) This should work ! Post back if it doesnt.
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
Well I was able to compile it but when I try the same code in a jsp page it gives error.
Here is the code of JSP page:
<html>
<head>
<title>
MyCo Directory
</title>
</head>
<%@ page language="java" import="java.sql.*" %>
<body>
<h1>MyCo Employees</h1>
<table border="1" width="400">
<tr>
<td><b>ID</b></td><td><b>Last Name</b></td>
<td><b>First Name</b></td><td><b>Extension</b></td>
</tr>
<%
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost/wroxjsp?user=admin");
Statement stmt = myConn.createStatement();
ResultSet myResultSet = stmt.executeQuery("select * from employee");
if (myResultSet != null)
{
while (myResultSet.next()) {
String eid = myResultSet.getString("empid");
String last = myResultSet.getString("lastname");
String first = myResultSet.getString("firstname");
String ext = myResultSet.getString("extension");
%>
<tr>
<td><%= eid %></td>
<td><%= last %></td>
<td><%= first %> </td>
<td><%= ext %> </td>
</tr>
<%
} /* of while */
} /* of if */
stmt.close();

myConn.close();
%>
</table>
<a href="control.html">Go back to admin control</a>
</body>
</html>

I am really very thankful to u for helping me out.I mean it.
Thanks

Maky Chopra
Ranch Hand

Joined: Apr 11, 2001
Posts: 149
No problem.. Cant help you unless you paste the jsp error that you get on the browser though..
FYI.. to execute this over JSP, the c:\jdbc should be in your system classpath.. so try adding set CLASSPATH=%CLASSPATH%;c:\jdbc to autoexec.bat and restart your comp.
If this doesn't help, post your error here and we'll take it from there.
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
Well to make sure I ahve the zip file of mysql utility and first step I did is I copied the zip file into c:\mysql and unzipped it into c:\Mysql.
I had also made a jar file which contains the same files as in zip file and added to c:\Mysql.
So to the autoexec.bat I should add to the classpath c:\mysql.
I hope you understand ....
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
The error on browser is:
javax.servlet.ServletException: Unable to load class org.gjt.mm.mysql.Driver
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:459)
at _0002fdbqueryP_0002ejspdbqueryP_jsp_7._jspService(_0002fdbqueryP_0002ejspdbqueryP_jsp_7.java:120)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:177)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:318)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:391)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:484)
Root cause:
java.lang.ClassNotFoundException: Unable to load class org.gjt.mm.mysql.Driver
at org.apache.jasper.servlet.JasperLoader.findClass(JasperLoader.java:223)
at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:147)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:120)
at _0002fdbqueryP_0002ejspdbqueryP_jsp_7._jspService(_0002fdbqueryP_0002ejspdbqueryP_jsp_7.java:64)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:177)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:318)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:391)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:210)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:484)
Maky Chopra
Ranch Hand

Joined: Apr 11, 2001
Posts: 149
I beleive you are confused
The zip file that you keep referring to is the mysql database files. These files are required to run mysql and to access it using the commandline mysql commands.
The error leads me to belive that you dont have the mm.mysql JDBC driver on your system.
To access the database using java/jsp you need to have a JDBC compliant driver for mysql. One that I'm referring to is called mm.mysql and is downloadable at http://www.mysql.com/Downloads/Contrib/mm.mysql.jdbc-1.2c.tar.gz
Download it and extract (unzip) to c:\ (it will form a directory called c:\mm.mysql.jdbc-1.2) Rename this to jdbc (simplicity ) and then add this to your system classpath
Hope this works.. If not, i think I'm confused
[This message has been edited by Mak Bhandari (edited May 04, 2001).]
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
Yes I also think that I made you confused. I thought that once you hvae mysql running by extracting the file from zip file you get your mm.mysql driver.
First time installation always messes me up.

After unzipping it and adding to the classpath I think so it will get executed.
I am downloading it.BTW what is the time there at CT?
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
And one more question quick...
Where do you see on Operating System that mm.mysql driver is installed?
Thanks.
Maky Chopra
Ranch Hand

Joined: Apr 11, 2001
Posts: 149
you dont "install" it.. just unzip it and add to your classpaths. once you edit your autoexec.bat and restart, at the dos prompt you could do a set | more and see all the env. variables (classpath among them)
Originally posted by Seema Bhatia:
And one more question quick...
Where do you see on Operating System that mm.mysql driver is installed?
Thanks.


[This message has been edited by Mak Bhandari (edited May 04, 2001).]
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
I have downloaded the file but unable to unzip it.
I ahve winzip8.0 and I do't think it is a valid zip file with the name mm_mysql_jdbc-1_2c_tar.
If you have time reply else will see u at 9 EDT
Maky Chopra
Ranch Hand

Joined: Apr 11, 2001
Posts: 149
open the file in winzip8.0 no problem.. winzip can handle tar files.. it will first extract to some temp directory and then you will be able to extract to c:\
if you have to, rename it to .zip and open it.. worst case, rename it to .jar, move it to say, c:\tempjdbc and jar -xvf mm....jar
[This message has been edited by Mak Bhandari (edited May 04, 2001).]
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
Unable to unzip it with winzip 8.0
I will try and let u know. Have not unzipped this type of file ever.
Have a great weekend.
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
It creates a file by .uue extension but further I am stuck.
Seema Bhatia
Ranch Hand

Joined: Apr 27, 2001
Posts: 36
My procedure is as follows:
1)I downloaded the file mm_mysql_jdbc-1_2c_tar.gz
2) I unzip it using Winzip8.0 and it prompts me for the
Please enter the full name of file conatined in the archive
mm_mysql_jdbc-1_2c_tar.gz.For example if the contents is a doc file a recommended file name is mm_mysql_jdbc-1_2c_tar.gz.doc
I don't know what to do and what to enter??sad I could not figure it out.

Help me...
clyde jones
Ranch Hand

Joined: Dec 02, 2000
Posts: 46
Seema,
Thanks so much for your response. I have successfully installed Apache,Tomcat, and MySQL. Now I need to confiure Apache and Tomcat to work together (Apache deliver Static documents and Tomcat deliver jsps). I think all I need to do is configure the server.xml for contexts, install the web sever and configure the Apache conf file to do this. If there's more, please let me know. After that is done, I paln on using MySQL for my database and need to configure it to work with Apache/Tomcat. Thanks for your willingness to help me. I've read a bunch on all three pieces of software already but it's nice to know there's someone you can ask questions of. I will post my email address if that would be easier for you. Thanks again.
I'll be talking to you soon.
Clyde
Jayson Falkner
Author
Ranch Hand

Joined: May 07, 2001
Posts: 57
Originally posted by Seema Bhatia:
I have very simple question.
I have installed Mysql on my Windows 98 system and it is running fine.
My question is how to set the classpath in tomcat.sh file as Mysql is in the format of zip file.
So I should either rename it (e.g.mysql.zip to mysql.jar) and then add to the tomcat.sh file as
Set classpath = c:\tomcat\mysql\mysql.jar
A bit confused.
And the book I am reading it says you hvae to make changes to server.xml file also and add
<context path = bla...etc. />
I have no idea what the book is trying to say and how to add the classpath for mysql driver.
I am sure anyone will be able to help me.
Any help will be appreciated.
Thanks

In the future you can add jar files to your classpath by simply placing them in your TOMCAT_HOME/lib directory.
After the fact, but it saves headaches for the next resource you 'add to classpath' .
Jayson Falkner
V.P./CTO, Amberjack Software LLC
Jayson@jspinsider.com
www.jspinsider.com


[This message has been edited by Jayson Falkner (edited May 07, 2001).]


Jayson Falkner<br />jayson@jspinsider.com<br />Author of <a href="http://www.jspbook.com" target="_blank" rel="nofollow">Servlets and JavaServer Pages; the J2EE Web Tier</a>
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
Just wanted to quickly post a note of thanks here.....Ive been watching this post as I also have been setting up tomcat & MySql on a win98 machine. Took a bit of doing & a few dents in the forehead (& desk ) but I now have it up & running perfectly thanks to you folks!!! Keep up the outstanding work!!
When I opened the aforementioned tar file with winzip 8.0 it said it contained one file - would I like to open & view it? to which I said yes. I then extracted it to my temp folder, moved it to C: & then renamed it C:\jdbc. After that everything worked fine. I have to think that if your winzip won't open the file you might have a bad download of the driver file or when you installed winzip you didn't associate it with these type files. If you open winzip & go to options-> configuration & then click on the system tab you will then get a screen that has an associations button on it. If you click on that you can set up asscociations. Both .tar & tgz are listed there. I also tryed the connection string two ways one using the ?user=""&password="" or without it (just to see if either wouldnt work)Both worked the same. If I can be of any other help feel free to yell (dave@dcddesigns.com)....it did take me quite a few days to get to this point, not fun but well worth the effort. Once again thanks to all!
[This message has been edited by DC Dalton (edited June 20, 2001).]
 
jQuery in Action, 2nd edition
 
subject: Tomcat and Mysql Configuration