my dog learned polymorphism*
The moose likes Tomcat and the fly likes Need help setting up jdbc driver for mysql to work with tomcat 6 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Need help setting up jdbc driver for mysql to work with tomcat 6" Watch "Need help setting up jdbc driver for mysql to work with tomcat 6" New topic
Author

Need help setting up jdbc driver for mysql to work with tomcat 6

Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
Hi,
Ok, I'm new at coding web stuff... just wanted to get that out of the way.
but in my defense I have googled like crazy trying to figure this stuff out before I bugged anyone.
So all said, if anyone of you could offer some good advice, I'll try not to disapoint.

here's what I'm working with:
1.) tomcat 6.0.36
2.) mySql 5.5.23
3.) java SDK 6
4.) Eclipse IDE, Version: Juno Service Release 1
5.) JDBC Driver: mysql-connector-java-5.1.22

I've created a couple servlets:
1.) is basically displaying an html page with several forms which do regular the sign-in or join the site stuff.
When you fill out the join form and click submit I have it calling test servlet which gets the information from
that form and calls another class which checks to see if the user information already exists in the database.

here's the form snipets I'm using to test my database connection:
this top servlet code is from my package "package certstudy".


Then from here my AddNewMember servlet is called when I click on submit:\
this is from my package "package certstudy"


here's my database connection and retrival methods:
this class is in my package "package certstudy.dbconnect"


In my console of my IDE I'm getting the following errors:

SQLException: getUserID -> No suitable driver found for jdbc:mysql://localhost:8080/certstudy
SQLException: getUserID -> No suitable driver found for jdbc:mysql://localhost:8080/certstudy

I've placed a copy of my jdbc driver: "mysql-connector-java-5.1.22.jar" in all of the following places:
1.) C:\Users\Joseph\workspace\certstudy\certstudy\WebContent\WEB-INF (folder)
2.) C:\apache-tomcat-6.0.36\lib (folder)

But still it is not working?
I been searching online for hints from every source I could think of....
but nothing worked as of yet.
I even read through a lot of the tomcat documentation and went to read through the developer's guide
that came with the tomcat 6 download i have: http://tomcat.apache.org/tomcat-6.0-doc/appdev/introduction.html
but the following link it gave does not seem to be supported since the Oracle purchase:
http://java.sun.com/j2ee/blueprints/

Any help pointing me in the right dirrection to get my tomcat working with my database
would be greatly appreciated.

I hope to reciprocate this help as I grow more knowledgable on this.
Thanks in advance.


Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16233
    
  21

Well, first of all, it's not considered good practice to obtain your JDBC Connection directly in your webapp code. You can reduce dependency on a particular database and driver - AND make more efficient use of resources - if you set up a Connection Pool in Tomcat and use JNDI to locate it and use it to get a pooled connection.

However, the reason for your actual failure is that you've put the driver in multiple locations and none of them is the right one for what you're doing. You need to put it in your WAR's WEB-INF/lib directory.


Customer surveys are for companies who didn't pay proper attention to begin with.
Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
Thanks Tim,

Would you know the path to the war WEB-INF/lib directory on tomcat?

Also, after thinking it over I really think your right, anything worth doing, should be done right.
So, if you have any info or links on where I could uptain JNDI or if it comes with the tomcat download
that would be greatly appreciated as well.

Regards,
Joe
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16233
    
  21

Tomcat doesn't enter into WEB-INF. The WAR format is part of the J2EE and JEE specs, irrespective of what appserver you use, and the specs define its format and the location and contents of the WEB-INF directory are defined as part of that spec.

Technically, a WAR is a Web ARchive, which is an extended form of a JAR file, which, in its turn is an enhanced kind of ZIP file. Actual disk directories are not part of what makes a WAR. Directories inside a WAR are really just resource paths.

What makes that confusing is that Tomcat, like many other J2EE servers, has the ability to deal with what are known as "exploded" WARs, which is what you get when you unzip a WAR file. In fact, by default, if you drop a WAR file into the TOMCAT_HOME/webapps directory, Tomcat will explode the WAR file, then use the exploded WAR directory structure to create actual filesystem directories and files, thereafter ignoring the original WAR file, even if you update it later.

Tomcat can host multiple WARs. Again, by default, each of the exploded WARs will be rooted in a directory whose name is the same as the .war file that was exploded (minus the .war extension). Or, you can skip the warfile entirely and just drop the exploded WAR directory into the webapps directory. Where, by default, the URL context that directs requests to that webapp will be the same as the name of the WAR.

As you can see, Tomcat is very flexible about such things. My development projects usually don't use the Tomcat directory. I build the WAR in my IDE's project directory and have a Tomcat configuration (Context) file tell Tomcat where to find the WAR and what URL context name to use. That way I don't have to do a build-and-move every time I test. Just a build.
Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
Good to know Tim,

I think my setup using an eclipse IDE is doing the same as you mentioned in your last paragraph.

On the JNDI solution you mentioned, I just found this resource and i am in the process of testing it.
http://numberformat.wordpress.com/2009/09/01/jndi-configuration-on-apache-tomcat-6-0/

Thought I'd post the link incase anyone else following this is in need of more information as well.
I'll let you know how my test goes.

Thanks again,
Joe
Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
I may have given that link to quickly...
It seems the java code examples given may be out of date for version 6 and up when it comes to the context portion.

Regards,
Joe
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help setting up jdbc driver for mysql to work with tomcat 6