You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by A Df <ab...@yahoo.com> on 2011/07/25 22:40:45 UTC

Tomcat 6 not working with JDBC driver for MySQL

Dear All:

I have read numerous posts and documentation and now I really need help.


I am using the following:

Product Version: NetBeans IDE 7.0 (Build 201104080000)
Java: 1.6.0_26; Java HotSpot(TM) Client VM 20.1-b02
System: Windows 7 version 6.1 running on x86; 
Apache Tomcat 6.0.26
MySQL Version: 5.5.14
MySQL Connector/J JDBC Driver 5.1.17


I performed the steps below as follows:

I have added the MySQL Connector/J JDBC Driver to the $CATALINA_HOME/lib directory

I have modify the web application deployment descriptor (/WEB-INF/web.xml) 
<CODE>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <resource-ref>
        <description>
    Resource reference to a factory for java.sql.Connection
    instances that may be used for talking to a particular
    database that is configured in the Context
   
 configurartion for the web application.
        </description>
        <res-ref-name>
    jdbc/myDB
        </res-ref-name>
        <res-type>
    javax.sql.DataSource
        </res-type>
        <res-auth>
    Container
        </res-auth>
    </resource-ref>
</web-app>
</CODE>

I have modified the XML file (META-INF/context.xml) found in the project’s folders
<CODE>
<?xml version="1.0" encoding="UTF-8"?>
<Context crossContext="true" debug="5" docBase="TweetCollector" antiJARLocking="true" path="/TweetCollector"
 reloadable="true">  
    <Resource name="jdbc/myDB"
            auth="Container"
            type="javax.sql.DataSource"
            username="root"
            password="administrator"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/myDB?autoReconnect=true"
            maxActive="8"
            maxIdle="4"/>
</Context>
</CODE>

I have modified the code in the java class for the database
 connection
<CODE>
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
  envCtx.lookup("jdbc/myDB");

setConnectionDB(ds.getConnection());
</CODE>

I
 read the steps at 
http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html#JDBC_Data_Sources.
 I have also tried modifying the server.xml file. 

The connection in the log prints as
jdbc:mysql://localhost:3306/myDB?autoReconnect=true, UserName=root@localhost, MySQL-AB JDBC Driver

However, it gives the error:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver
OR
java.sql.SQLException: No suitable driver

Is
 it that the version for the server needs to match specific version for 
MySQL and its driver? How can I solve the JDBC issue? I have tried so 
many posts and made so many changes that i am really frustrated and dont
 know
 what else to do. Thanks.

Cheers

Re: Tomcat 6 not working with JDBC driver for MySQL

Posted by Mark Eggers <it...@yahoo.com>.
----- Original Message -----

> From: Rainer Frey <ra...@inxmail.de>
> To: Tomcat Users List <us...@tomcat.apache.org>
> Cc: 
> Sent: Tuesday, July 26, 2011 5:01 AM
> Subject: Re: Tomcat 6 not working with JDBC driver for MySQL
> 
> Please stop top posting.
> 
> On 26.07.2011, at 12:02, A Df wrote:
>>>  On 25.07.2011, at 22:40, A Df wrote:
>>> 
>>>>  Dear All:
>>>> 
>>>>  I have read numerous posts and documentation and now I really need 
> help.
>>>> 
>>>> 
>>>>  I am using the following:
>>>> 
>>>>  Product Version: NetBeans IDE 7.0 (Build 201104080000)
>>>> 
>>>>  I performed the steps below as follows:
>>>> 
>>>>  I have added the MySQL Connector/J JDBC Driver to the 
> $CATALINA_HOME/lib directory
>>>>  However, it gives the error:
>>>>  org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC 
> driver
>>>>  OR
>>>>  java.sql.SQLException: No suitable driver
>>> 
>>>  Are you launching Tomcat from within Netbeans? Then I'd suspect 
> that Netbeans sets it 
>>>  up with its own configuration, and esp. with a project specific class 
> path. Most instructions
>>>  out there imply that you launch Tomcat with the supplied scripts or 
> service utilities, and not with 
>>>  an IDE plugin.
>>> 
>>>  Try
>>>  * launching Tomcat manually, by shell/batch scripts OR
>>>  * add the JDBC driver also to your Netbeans project class path
> 
>>  Hi Rainer:
>> 
>>  I had already added the JDBC driver to my Netbeans project class path and 
> that worked for awhile then stopped.
> 
> Well sorry, no idea, as I don't use Netbeans. Maybe ask on a Netbeans 
> list/forum.
>> 

>>  I will have to do some reading on launching Tomcat manually by shell/batch 
> scripts but I don't have much time as I have a deadline to meet which is my 
> major concern.
> 
> Not much to read. See RUNNING.txt in a tomcat distribution. $CATALINA_HOME/lib 
> is the correct place for the driver.
> 
> Rainer


NetBeans is pretty benign when it comes to changing the Tomcat environment. It changes the JRE (uses the $JDK_HOME/jre/bin/java instead of JRE_HOME), and captures catalina.out to display in the IDE.

Other than that, it adds nothing to the CLASSPATH, nor does it add any additional defines to JAVA_OPTS. You can change JAVA_OPTS defines in the server properties dialog, but I normally just use $CATALINA_HOME/bin/setenv.sh for that. That way my environment is consistent both in and out of NetBeans.

When you start the server in debugging or profile mode, NetBeans adds the appropriate properties, but does not alter those you have defined in $CATALINA_HOME/bin/setenv.sh.

When you're building a project in NetBeans and have it assigned to a server, NetBeans will add the content of that server's library to your project's CLASSPATH. There is no need to add the JDBC driver to your project's CLASSPATH if it already exists in $CATALINA_HOME/lib. In fact, coupled with the default NetBeans server property of Enable JDBC driver deployment, this is almost certainly a bad thing. Also, adding the library or JAR file to your project and having NetBeans package that in the WAR file is a bad thing.

Just add the JAR file to $CATALINA_HOME/lib, use the Tomcat supplied database pooling, and everything just works.

Permission problems may be an issue. If you're on Linux and the copied MySQL JAR file cannot be read by the owner of the Tomcat process, that would create problems. If you're on Windows, I imagine there are similar access issues, especially with Windows Vista or Windows 7.

It would be interesting to see the following:

1. Use NetBeans to just build the project
   right-mouse button on the project and select Build
2. Start the Tomcat server using startup.sh (or startup.bat if you're on Windows)
   a. Open a command line window
   b. Change directory to $CATALINA_HOME/bin (%CATALINA_HOME%\bin on Windows)
   c. ./startup.sh (startup.bat on Windows)
3. Open a browser to localhost:8080
4. Start up the Manager application (use the same userid / password defined in NetBeans)
5. Choose a file to deploy
   a. Navigate to <Project_Name>/dist in the browser
   b. Select the war file
   c. Click on the Deploy button
6. Open the application in a new browser window or tab
7. Post the contents of the log files
   a. catalina.out
   b. catalina.<date>.log
   c. any logging your application does (you do catch and log exceptions, yes?)

I use this combination on Linux and Windows/XP Professional all the time.

. . . . just my two cents.

/mde/

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat 6 not working with JDBC driver for MySQL

Posted by Rainer Frey <ra...@inxmail.de>.
Please stop top posting.

On 26.07.2011, at 12:02, A Df wrote:
>> On 25.07.2011, at 22:40, A Df wrote:
>> 
>>> Dear All:
>>> 
>>> I have read numerous posts and documentation and now I really need help.
>>> 
>>> 
>>> I am using the following:
>>> 
>>> Product Version: NetBeans IDE 7.0 (Build 201104080000)
>>> 
>>> I performed the steps below as follows:
>>> 
>>> I have added the MySQL Connector/J JDBC Driver to the $CATALINA_HOME/lib directory
>>> However, it gives the error:
>>> org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver
>>> OR
>>> java.sql.SQLException: No suitable driver
>> 
>> Are you launching Tomcat from within Netbeans? Then I'd suspect that Netbeans sets it 
>> up with its own configuration, and esp. with a project specific class path. Most instructions
>> out there imply that you launch Tomcat with the supplied scripts or service utilities, and not with 
>> an IDE plugin.
>> 
>> Try
>> * launching Tomcat manually, by shell/batch scripts OR
>> * add the JDBC driver also to your Netbeans project class path

> Hi Rainer:
> 
> I had already added the JDBC driver to my Netbeans project class path and that worked for awhile then stopped.

Well sorry, no idea, as I don't use Netbeans. Maybe ask on a Netbeans list/forum.
> 
> I will have to do some reading on launching Tomcat manually by shell/batch scripts but I don't have much time as I have a deadline to meet which is my major concern.

Not much to read. See RUNNING.txt in a tomcat distribution. $CATALINA_HOME/lib is the correct place for the driver.

Rainer

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat 6 not working with JDBC driver for MySQL

Posted by A Df <ab...@yahoo.com>.
Hi Rainer:

I had already added the JDBC driver to my Netbeans project class path and that worked for awhile then stopped. The previous problem is that it works occasionally then stopped. Now it works and inserts one row then stops even if restart Tomcat.

I will have to do some reading on launching Tomcat manually by shell/batch scripts but I don't have much time as I have a deadline to meet which is my major concern.

I don't really know much about Tomcat so do you have any suggestions on:
	* should i start the web application from netbeans or from Tomcat?
	* how would i test the web application from Tomcat alone?

	* do you know how to prevent netbeans from launching Tomcat?
	* which locations do I need to have the jar file in?


Sorry for asking so many questions but as I mentioned I don't really know much about Tomcat and this is my first time using netbeans to create a web application usually I do windows/desktop apps in Eclipse.


Cheers,
 A Df




>________________________________
>From: Rainer Frey <ra...@inxmail.de>
>To: Tomcat Users List <us...@tomcat.apache.org>
>Sent: Tuesday, 26 July 2011, 7:04
>Subject: Re: Tomcat 6 not working with JDBC driver for MySQL
>
>
>On 25.07.2011, at 22:40, A Df wrote:
>
>> Dear All:
>> 
>> I have read numerous posts and documentation and now I really need help.
>> 
>> 
>> I am using the following:
>> 
>> Product Version: NetBeans IDE 7.0 (Build 201104080000)
>> 
>> I performed the steps below as follows:
>> 
>> I have added the MySQL Connector/J JDBC Driver to the $CATALINA_HOME/lib directory
>> However, it gives the error:
>> org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver
>> OR
>> java.sql.SQLException: No suitable driver
>
>Are you launching Tomcat from within Netbeans? Then I'd suspect that Netbeans sets it 
>up with its own configuration, and esp. with a project specific class path. Most instructions
>out there imply that you launch Tomcat with the supplied scripts or service utilities, and not with 
>an IDE plugin.
>
>Try
>* launching Tomcat manually, by shell/batch scripts OR
>* add the JDBC driver also to your Netbeans project class path
>
>Rainer
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>
>

Re: Tomcat 6 not working with JDBC driver for MySQL

Posted by Rainer Frey <ra...@inxmail.de>.
On 25.07.2011, at 22:40, A Df wrote:

> Dear All:
> 
> I have read numerous posts and documentation and now I really need help.
> 
> 
> I am using the following:
> 
> Product Version: NetBeans IDE 7.0 (Build 201104080000)
> 
> I performed the steps below as follows:
> 
> I have added the MySQL Connector/J JDBC Driver to the $CATALINA_HOME/lib directory
> However, it gives the error:
> org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver
> OR
> java.sql.SQLException: No suitable driver

Are you launching Tomcat from within Netbeans? Then I'd suspect that Netbeans sets it 
up with its own configuration, and esp. with a project specific class path. Most instructions
out there imply that you launch Tomcat with the supplied scripts or service utilities, and not with 
an IDE plugin.

Try
* launching Tomcat manually, by shell/batch scripts OR
* add the JDBC driver also to your Netbeans project class path

Rainer
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat 6 not working with JDBC driver for MySQL

Posted by A Df <ab...@yahoo.com>.
Hello Everyone:

[SOLVED]

I finally got it to work. Thanks to everyone that helped!! BTW, should I put the word solved in the subject?


I had to create a Base class to setup the connection and then have another class extend it. The subclass only dealt with methods for executing statements. See more at:
http://forums.netbeans.org/viewtopic.php?p=110485


Here is my latest configuration and software details:

Using:
java version 1.6.0_26
Apache Tomcat 6.0.26

JDBC Driver:
MySQL Connector/J JDBC Driver (mysql-connector-java-5.1.17-bin.jar) is in $CATALINA_HOME/lib directory

I am also posting the content of my other files as follows:

content.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" crossContext="true" path="/TweetCollector" reloadable="true">
  <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="8" maxIdle="4" name="jdbc/myDB" password="administrator" type="javax.sql.DataSource" url="jdbc:mysql://127.0.0.1/myDB?autoReconnect=true" username="root" validationQuery="SELECT 1"/>
</Context>


web.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <resource-ref>
        <description>
    Resource reference to a factory for java.sql.Connection
    instances that may be used for talking to a particular
    database that is configured in the Context
    configurartion for the web application.
        </description>
        <res-ref-name>
    jdbc/myDB
        </res-ref-name>
        <res-type>
    javax.sql.DataSource
        </res-type>
        <res-auth>
    Container
        </res-auth>
    </resource-ref>
</web-app>

Thanks again :D

Cheers,
A Df




>________________________________
>From: Christopher Schultz <ch...@christopherschultz.net>
>To: Tomcat Users List <us...@tomcat.apache.org>
>Sent: Wednesday, 27 July 2011, 23:38
>Subject: Re: Tomcat 6 not working with JDBC driver for MySQL
>
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>To whom it may concern,
>
>On 7/25/2011 4:40 PM, A Df wrote:
>> I have added the MySQL Connector/J JDBC Driver to the
>> $CATALINA_HOME/lib directory
>
>Good.
>
>> I have modify the web application deployment descriptor
>> (/WEB-INF/web.xml)
>> 
>> <resource-ref> <res-ref-name> jdbc/myDB </res-ref-name> <res-type> 
>> javax.sql.DataSource </res-type> <res-auth> Container </res-auth> 
>> </resource-ref>
>
>Okay.
>
>> I have modified the XML file (META-INF/context.xml) found in the
>> project’s folders <Context crossContext="true" debug="5"
>> docBase="TweetCollector" antiJARLocking="true"
>> path="/TweetCollector" reloadable="true">
>
>Okay, the following attributes should not be used:
>
>- - debug (ignored)
>- - docBase (context.xml is already in a WAR/WARdir)
>- - path (Tomcat determines the deployment path from the WAR/WARdir name)
>
>> <Resource name="jdbc/myDB" auth="Container" 
>> type="javax.sql.DataSource" username="root" password="administrator" 
>> driverClassName="com.mysql.jdbc.Driver" 
>> url="jdbc:mysql://localhost:3306/myDB?autoReconnect=true" 
>> maxActive="8" maxIdle="4"/>
>
>That looks good to me.
>
>> The connection in the log prints as 
>> jdbc:mysql://localhost:3306/myDB?autoReconnect=true,
>> UserName=root@localhost, MySQL-AB JDBC Driver
>
>What connection in what log?
>
>> However, it gives the error: 
>> org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC
>> driver OR java.sql.SQLException: No suitable driver
>
>Do you have JDBS drivers in multiple places?
>
>Try stopping Tomcat, deleting the whole "work" directory and any cached
>deployment descriptors under conf/Catalina/localhost/[webapp].xml
>
>> Is it that the version for the server needs to match specific
>> version for MySQL and its driver?
>
>No.
>
>> How can I solve the JDBC issue? I have tried so many posts and made 
>> so many changes that i am really frustrated and dont know what else 
>> to do. Thanks.
>
>Historically, that message ("No suitable driver") means that your
>configuration is slightly wrong. Since Tomcat caches the deployment
>descriptor, yours might be out-of-date, hence my suggestions above.
>
>- -chris
>-----BEGIN PGP SIGNATURE-----
>Version: GnuPG v1.4.10 (MingW32)
>Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
>iEYEARECAAYFAk4wk3kACgkQ9CaO5/Lv0PB7kgCfS2nlgOkLMkQX5COyzXviIXW1
>oFwAoKe2K0ceu6Vw+XiMpGCORYD55x3u
>=IqxP
>-----END PGP SIGNATURE-----
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>
>

Re: Tomcat 6 not working with JDBC driver for MySQL

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

To whom it may concern,

On 7/25/2011 4:40 PM, A Df wrote:
> I have added the MySQL Connector/J JDBC Driver to the
> $CATALINA_HOME/lib directory

Good.

> I have modify the web application deployment descriptor
> (/WEB-INF/web.xml)
> 
> <resource-ref> <res-ref-name> jdbc/myDB </res-ref-name> <res-type> 
> javax.sql.DataSource </res-type> <res-auth> Container </res-auth> 
> </resource-ref>

Okay.

> I have modified the XML file (META-INF/context.xml) found in the
> project’s folders <Context crossContext="true" debug="5"
> docBase="TweetCollector" antiJARLocking="true"
> path="/TweetCollector" reloadable="true">

Okay, the following attributes should not be used:

- - debug (ignored)
- - docBase (context.xml is already in a WAR/WARdir)
- - path (Tomcat determines the deployment path from the WAR/WARdir name)

> <Resource name="jdbc/myDB" auth="Container" 
> type="javax.sql.DataSource" username="root" password="administrator" 
> driverClassName="com.mysql.jdbc.Driver" 
> url="jdbc:mysql://localhost:3306/myDB?autoReconnect=true" 
> maxActive="8" maxIdle="4"/>

That looks good to me.

> The connection in the log prints as 
> jdbc:mysql://localhost:3306/myDB?autoReconnect=true,
> UserName=root@localhost, MySQL-AB JDBC Driver

What connection in what log?

> However, it gives the error: 
> org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC
> driver OR java.sql.SQLException: No suitable driver

Do you have JDBS drivers in multiple places?

Try stopping Tomcat, deleting the whole "work" directory and any cached
deployment descriptors under conf/Catalina/localhost/[webapp].xml

> Is it that the version for the server needs to match specific
> version for MySQL and its driver?

No.

> How can I solve the JDBC issue? I have tried so many posts and made 
> so many changes that i am really frustrated and dont know what else 
> to do. Thanks.

Historically, that message ("No suitable driver") means that your
configuration is slightly wrong. Since Tomcat caches the deployment
descriptor, yours might be out-of-date, hence my suggestions above.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk4wk3kACgkQ9CaO5/Lv0PB7kgCfS2nlgOkLMkQX5COyzXviIXW1
oFwAoKe2K0ceu6Vw+XiMpGCORYD55x3u
=IqxP
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org