You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by Michael Murray <mi...@gmail.com> on 2008/11/28 17:19:00 UTC

Tomcat derby JSP "java.sql.SQLException: No suitable driver"

hi guys,
I'm having a bit of trouble configuring apache tomcat to work with a  
derby database on mac OS. Below are the steps I took. The error I get  
when trying to acces the page from a jsp page is  
"java.sql.SQLException: No suitable driver". Can anyone please help,  
i'd be very very grateful.


Thanks,

Mike


I've created a derby database using the ij tool at location:

home/derby/db

I've installed Tomcat 6.0.18 at

home/applications/apache-tomcat-6.0.18/

and created a webapp at /apache-tomcat-6.0.18/webapps/ROOT

i've attempted to integrate the db with tomcat by:

sever.xml. adding:

     <Resource name="UserDatabase" auth="Container"
               type="org.apache.catalina.UserDatabase"
               description="User database that can be updated and saved"
                
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
               pathname="conf/tomcat-users.xml" />

and to webapps/ROOT/server.xml

   <context-param>
      <param-name>
         javax.servlet.jsp.jstl.sql.dataSource
      </param-name>
      <param-value>jdbc/db</param-value>
   </context-param>

   <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/db</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
   </resource-ref>


in startup.sh I have added:

DERBY_HOME=/derby/db

CATALINA_OPTS="-Dderby.system.home=/derby/db"

CLASSPATH=\
Applications/apache-tomcat-6.0.18/lib/derbyclient.jar:\


I have then attempted to access the db from a jsp page using the  
following script:

<%@ page language="java" import="java.sql.*" %>

<%
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String dbName="/db";
String connectionURL = "jdbc:derby://localhost:8080/" + dbName;
Connection conn = null;
try{
     Class.forName(driver);
} catch(java.lang.ClassNotFoundException e) {
     e.printStackTrace();
	out.println(e);
}
try {
     conn = DriverManager.getConnection(connectionURL);
	out.println("CONNETED<br>");

     //body of code to go here

}  catch (Throwable e)  {
    out.println(e);
    e.printStackTrace();
} finally {

}

%>



This returns the error

"java.sql.SQLException: No suitable driver"


Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by Emmanuel Cecchet <ma...@frogthinker.org>.
Mike,
> Thanks for your reply. I have tried using derby.jar instead of
> derbyclient.jar and the result is exactly the same. Is there any more advice
> you can give me?
>   
This is certainly a classpath issue. Be careful with classloader 
inheritance in Tomcat.
Where did you copy derby.jar? What version of Tomcat are you using?

Emmanuel

>
>
>
>
>
>
>
>
>
>
>
>
>
> Emmanuel Cecchet-5 wrote:
>   
>> Michael,
>>
>> You should have derby.jar in the classpath if you want to use embedded 
>> mode.
>>     
>>> CLASSPATH=\
>>> Applications/apache-tomcat-6.0.18/lib/derbyclient.jar:\
>>>       
>> derbyclient.jar contains only the JDBC driver code 
>> (http://db.apache.org/derby/docs/dev/adminguide/tadminappschangingyourclasspath.html)
>>     
>>> <%
>>> String driver = "org.apache.derby.jdbc.EmbeddedDriver";
>>>       
>> Hope this helps,
>> Emmanuel
>>
>> -- 
>> Emmanuel Cecchet
>> FTO @ Frog Thinker 
>> Open Source Development & Consulting
>> --
>> Web: http://www.frogthinker.org
>> email: manu@frogthinker.org
>> Skype: emmanuel_cecchet
>>
>>
>>
>>     
>
>   


-- 
Emmanuel Cecchet
FTO @ Frog Thinker 
Open Source Development & Consulting
--
Web: http://www.frogthinker.org
email: manu@frogthinker.org
Skype: emmanuel_cecchet


Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by Kristian Waagan <Kr...@Sun.COM>.
teamderby wrote:
> Emmanuel,
>
> Thanks for your reply. I have tried using derby.jar instead of
> derbyclient.jar and the result is exactly the same. Is there any more advice
> you can give me?
>   

Hi Mike,

The error message you gave, means one of two things;
 1) The URL is not a Derby URL (i.e. typos, wrong protocol etc).
 2) The required Derby driver (derby.jar or derbyclient.jar) isn't in 
the classpath.

By looking at the code you posted, the only way I can see it failing as 
you describe, is that you are loading the wrong driver.
For the embedded driver (derby.jar: 
org.apache.derby.jdbc.EmbeddedDriver) the URL must start with 
"jdbc:derby:DBNAME".
For the client driver (derbyclient.jar: 
org.apache.derby.jdbc.ClientDriver) the URL must start with 
"jdbc:derby://HOSTNAME[:PORT]/DBNAME".

If you are sure you have specified the correct path, and it still 
doesn't work, this sounds like a classloader issue to me.
I don't know Tomcat, but have you placed the jar in the correct directory?
Are you sure it actually uses the environment variable you have modified?

It's also possible that a ClassNotFoundException is thrown and you 
overlooked the error message, but if you're sure you have looked in the 
correct log file that's not what's going on either...


Regards,
-- 
Kristian

> Thanks,
>
> Mike
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Emmanuel Cecchet-5 wrote:
>   
>> Michael,
>>
>> You should have derby.jar in the classpath if you want to use embedded 
>> mode.
>>     
>>> CLASSPATH=\
>>> Applications/apache-tomcat-6.0.18/lib/derbyclient.jar:\
>>>       
>> derbyclient.jar contains only the JDBC driver code 
>> (http://db.apache.org/derby/docs/dev/adminguide/tadminappschangingyourclasspath.html)
>>     
>>> <%
>>> String driver = "org.apache.derby.jdbc.EmbeddedDriver";
>>>       
>> Hope this helps,
>> Emmanuel
>>
>> -- 
>> Emmanuel Cecchet
>> FTO @ Frog Thinker 
>> Open Source Development & Consulting
>> --
>> Web: http://www.frogthinker.org
>> email: manu@frogthinker.org
>> Skype: emmanuel_cecchet
>>
>>
>>
>>     
>
>   


Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by teamderby <mi...@gmail.com>.
Emmanuel,

Thanks for your reply. I have tried using derby.jar instead of
derbyclient.jar and the result is exactly the same. Is there any more advice
you can give me?

Thanks,

Mike














Emmanuel Cecchet-5 wrote:
> 
> Michael,
> 
> You should have derby.jar in the classpath if you want to use embedded 
> mode.
>> CLASSPATH=\
>> Applications/apache-tomcat-6.0.18/lib/derbyclient.jar:\
> derbyclient.jar contains only the JDBC driver code 
> (http://db.apache.org/derby/docs/dev/adminguide/tadminappschangingyourclasspath.html)
>> <%
>> String driver = "org.apache.derby.jdbc.EmbeddedDriver";
> 
> Hope this helps,
> Emmanuel
> 
> -- 
> Emmanuel Cecchet
> FTO @ Frog Thinker 
> Open Source Development & Consulting
> --
> Web: http://www.frogthinker.org
> email: manu@frogthinker.org
> Skype: emmanuel_cecchet
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Tomcat-derby-JSP-%22java.sql.SQLException%3A-No-suitable-driver%22-tp20776666p20840073.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.


Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by Emmanuel Cecchet <ma...@frogthinker.org>.
Michael,

You should have derby.jar in the classpath if you want to use embedded 
mode.
> CLASSPATH=\
> Applications/apache-tomcat-6.0.18/lib/derbyclient.jar:\
derbyclient.jar contains only the JDBC driver code 
(http://db.apache.org/derby/docs/dev/adminguide/tadminappschangingyourclasspath.html)
> <%
> String driver = "org.apache.derby.jdbc.EmbeddedDriver";

Hope this helps,
Emmanuel

-- 
Emmanuel Cecchet
FTO @ Frog Thinker 
Open Source Development & Consulting
--
Web: http://www.frogthinker.org
email: manu@frogthinker.org
Skype: emmanuel_cecchet


Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by Bryan Pendleton <bp...@amberpoint.com>.
> Michael Murray wrote:
> hi guys,
> I'm having a bit of trouble configuring apache tomcat to work with a 
> derby database on mac OS. 

You might start by following some of the existing tutorials, such
as http://db.apache.org/derby/papers/fortune_tut.html, and let us
know at what step in the tutorial you start having trouble.

Once you've successfully followed the tutorial, it should be easier
to see how you want to vary from the tutorial in your own application.

thanks,

bryan

Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by "Stephan van Loendersloot (LIST)" <st...@republika.nl>.
teamderby wrote:
> Hi Stephan,
>
> Excellent, it worked!
>
> i'm extremely grateful, you've ended a lot of frustration on my part and i
> cannot thankyou enough for the help you've supplied.
>
> the tutorial is excellently produced and i am sure will be a welcome
> resource for other people attempting to set up Derby in the future!
>
> once again many thanks for your time and effort on this.
>
> Mike.
>
>   

Great to hear that Mike! It took me a little longer than expected, but 
the outcome is well worth the effort!

OT: You do know that it's considered bad practice to program directly in 
your JSP's, don't you? You should look at using tag-libraries, JSTL, 
etc. to save yourself the hassle of maintenance problems. Maybe later 
(once you understand how things work according to the Servlet 
Specification) you can move on to using a web-framework of your choice.

Happy coding!

    Stephan.

Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by teamderby <mi...@gmail.com>.
Hi Stephan,

Excellent, it worked!

i'm extremely grateful, you've ended a lot of frustration on my part and i
cannot thankyou enough for the help you've supplied.

the tutorial is excellently produced and i am sure will be a welcome
resource for other people attempting to set up Derby in the future!

once again many thanks for your time and effort on this.

Mike.


Stephan van Loendersloot (LIST) wrote:
> 
> 
> Let me know if it works for you.
> 
> 
> Regards,
> 
>     Stephan.
>    
> 

-- 
View this message in context: http://www.nabble.com/Tomcat-derby-JSP-%22java.sql.SQLException%3A-No-suitable-driver%22-tp20776666p20995429.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.


Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by "Stephan van Loendersloot (LIST)" <st...@republika.nl>.
Hi Mike,

I didn't quote your reply, to ensure this tutorial-like thing is as 
clean as possible.


I assume that you've already downloaded db-derby-10.4.2.0-bin.tar.gz and 
apache-tomcat-6.0.18.tar.gz *to your home directory*. Every step shown 
below is somewhere in or below the home directory. For me, this 
directory on my Mac is '/Users/Stephan'. You should have the default 
user-rights in this directory. Try not to use spaces or any other 
strange characters in the path, just to avoid weird circumstances.

I included the terminal-prompt with each command, so you can see in 
which directory I was when taking these steps. Everything after the 
dollar sign ($) is the actual command.

We'll start with Derby. Unpack it:

    osx:~ Stephan$ tar xzvf db-derby-10.4.2.0-bin.tar.gz

I omitted the output for brevity, but the directory 
'/Users/Stephan/db-derby-10.4.2.0-bin' has been created. Let's simply 
rename it to 'derby' for convenience:

    mv osx:~ Stephan$ mv db-derby-10.4.2.0-bin derby
   
Switch to the 'derby/bin' directory to edit the startup script.

    osx:~ Stephan$ cd derby/bin
    osx:~/derby/bin Stephan$ pico startNetworkServer
   
On line 18 you'll find the following:

    # under the License.

    if [ -z "$DERBY_HOME" ]; then

This needs one extra line. Remember, that *MY* Derby was unpacked to 
/Users/Stephan/Derby. You must change it to yours.

    # under the License.
    export DERBY_HOME=/Users/Stephan/Derby
    if [ -z "$DERBY_HOME" ]; then

Save the file after you made your changes, and repeat this step for the 
shutdown script 'stopNetworkServer', and the Derby command-line tool 
'ij'. They're all in the same directory.

After you also changed the other two scripts, let's start Derby:
   
    osx:~/derby/bin Stephan$ ./startNetworkServer
   
You should see something like this:

    Security manager installed using the Basic server security policy.
    Apache Derby Network Server - 10.4.2.0 - (689064) started and ready
    to accept connections on port 1527 at 2008-12-12
    20:26:37.753 GMT

Next, open a new terminal and start the Derby command line tool:

    osx:~ Stephan$ cd derby/bin
    osx:~ Stephan$ ./ij

Now, we're at the command-prompt of ij, whichs displays : ij>
Everything after the bracket is the actual command. Let's create a database:

   
    ij> CONNECT 
'jdbc:derby://localhost/mydatabasename;user=DATABASEUSERNAME;password=DATABASEPASSWORD;create=true';
    ij> DISCONNECT;
    ij> QUIT;

That was Derby. Move on to Tomcat by changing to our home directory:

    osx:~/derby/bin Stephan$ cd ~

Next, unpack tomcat:

    osx:~ Stephan$ tar xzvf apache-tomcat-6.0.18.tar.gz

Again, rename to output directory to 'tomcat' for convenience:

    osx:~ Stephan$ mv apache-tomcat-6.0.18 tomcat

Copy the required Derby libraries to Tomcat:

    osx:~ Stephan$ cp ~/derby/lib/derbyclient.jar ~/tomcat/lib
    osx:~ Stephan$ cp ~/derby/lib/derby.jar ~/tomcat/lib

Remove the ROOT directory and all of its content in tomcat/webapps:

    osx:~ Stephan$ rm -r tomcat/webapps/ROOT

Create a new ROOT directory:

    osx:~ Stephan$ mkdir tomcat/webapps/ROOT
   
Create a META-INF directory in the ROOT directory;

    osx:~ Stephan$ mkdir tomcat/webapps/ROOT/META-INF
   
Go to that directory and create the file 'context.xml':

    osx:~ Stephan$ cd tomcat/webapps/ROOT/META-INF
    osx:~ Stephan$ pico context.xml
   
Paste the following content in the editor, and save the file afterwards.
Note that there should be no leading spaces, the file must start with 
the <?xml declaration:

<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
    <Resource name="jdbc/mydatabasename" auth="Container"
        type="javax.sql.DataSource"
        driverClassName="org.apache.derby.jdbc.ClientDriver"
        url="jdbc:derby://localhost:1527/db"
        username="DATABASEUSERNAME"
        password="DATABASEUSERPASSWORD"
    />
</Context>

Return to your home directory:

    osx:~/tomcat/webapps/ROOT/META-INF Stephan$ cd ~

Create a WEB-INF directory in the ROOT directory;

    osx:~ Stephan$ mkdir tomcat/webapps/ROOT/WEB-INF
   
Go to that directory and create the file 'web.xml':

    osx:~ Stephan$ cd tomcat/webapps/ROOT/WEB-INF
    osx:~ Stephan$ pico web.xml

Paste the following content in the editor, and save the file afterwards.
Note that there should be no leading spaces, the file must start with 
the <?xml declaration:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:j2ee="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">
    <display-name>root</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <resource-ref>
        <description>JNDI www.mydomain.com Connection</description>
        <res-ref-name>jdbc/db</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
</web-app>

To finish, we need to create the file ROOT/index.jsp:

    osx:~/tomcat/webapps/ROOT/WEB-INF Stephan$ cd ..
    osx:~/tomcat/webapps/ROOT Stephan$ pico index.jsp
     
Copy the following content in the editor, and save the file afterwards:

<%@page language="java" import="java.sql.*, javax.sql.*, javax.naming.*" %>
<%
    Context ctx = new InitialContext();
    DataSource dataSource = (DataSource) 
ctx.lookup("java:comp/env/jdbc/db");
    if (dataSource == null) {
        throw new JspException(
            "java:comp/env/jdbc/db returned NULL");
    }

try {
    Connection conn = dataSource.getConnection();
    out.println("CONNECTED<br>");
    //body of code to go here

}  catch (Throwable e)  {
   out.println(e);
   e.printStackTrace();
} finally {

}

%>

Now let's start tomcat:

    osx:~/tomcat/webapps/ROOT/WebContent Stephan$ cd ~/tomcat/bin
    osx:~/tomcat/bin Stephan$ ./startup.sh
   
And surf with your browser to http://localhost:8080/

You should see: CONNECTED

Every step was taken and copied in a text-editor, to make sure I didn't 
forget anything.

Let me know if it works for you.


Regards,

    Stephan.
   



Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by teamderby <mi...@gmail.com>.
Hi Stephan,

Thanks for the response. I have tried your suggestions but still no joy. The
same error message.

Is it possible I am putting the database in the wrong place?!

Thanks again,

Mike

-- 
View this message in context: http://www.nabble.com/Tomcat-derby-JSP-%22java.sql.SQLException%3A-No-suitable-driver%22-tp20776666p20918400.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.


Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by "Stephan van Loendersloot (LIST)" <st...@republika.nl>.
Emmanuel Cecchet wrote:
>
>>> Just to check, which JVM are you using? What is the output of 'java 
>>> -version'?
>>> Did you make sure that both Tomcat and Derby where using the same JVM?
>>>
>>
>> This seems to be irrelevant.
>
> Well, if he uses a default Kaffee or gij, there are many classloader 
> issues with Tomcat.
> I already had several users having problems with Sequoia and Derby 
> because of the wrong JVM.
>
> Emmanuel
>

Hi Emmanuel,

Mike told us he uses a Mac, so I think there's little chance that he 
uses a custom installed JVM on his box and besides, Tomcat has to read 
the configuration files before it can even know what driver to use. If 
it can't find the required class, an error message like 'Cannot load 
JDBC driver class 'org.apache.derby.jdbc.ClientDriver'' would be issued.

Then again, your right that it's better to actually know what JVM you're 
using, even if it's just to rule out that possibillity.


Regards,

    Stephan.

Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by Emmanuel Cecchet <ma...@frogthinker.org>.
>> Just to check, which JVM are you using? What is the output of 'java 
>> -version'?
>> Did you make sure that both Tomcat and Derby where using the same JVM?
>>
>
> This seems to be irrelevant.

Well, if he uses a default Kaffee or gij, there are many classloader 
issues with Tomcat.
I already had several users having problems with Sequoia and Derby 
because of the wrong JVM.

Emmanuel

Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by "Stephan van Loendersloot (LIST)" <st...@republika.nl>.
Emmanuel Cecchet wrote:
> Mike,
>
> Just to check, which JVM are you using? What is the output of 'java 
> -version'?
> Did you make sure that both Tomcat and Derby where using the same JVM?
>
Hi Emmanuel,

This seems to be irrelevant. Since the network driver is used to make 
connections to the database, Tomcat and Apache don't have to run in the 
same JVM. Also, Mike is able to start Tomcat v6, which requires at least 
Java 5, whereas Derby requires at least 1.4 and they both seem to be 
running.


>> I am unfortunately still having problems, the jsp message:
>> org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC 
>> driver of
>> class '' for connect URL 'null'
>>
>> I have followed your instructions above exactly with a clean download of
>> Tomcat. I can therefore assume it is something silly I am doing in 
>> setting
>> up the database or launching tomcat. I can not see how you have set the
>> environment variables or the classpath.

Hi Mike,

It looks like Tomcat is unable to locate the configuration files for 
your ROOT web-application. It doesn't even get the connection URL and 
the name of the driver-class.

Except for exporting DERBY_HOME, no extra CLASSPATH or environment magic 
is needed for this example, because Tomcat scans the directory 
CATALINA_HOME/lib for common and/or shared libraries by default. The 
same goes for Derby with the default startup scripts. You use 2 separate 
terminals to start each program, so they can operate independantly, 
which is fine, you just need to start both of them from their 'bin' 
directories.

The only thing that Derby needs is Java, the only things that Tomcat 
needs are Java and the Derby client driver, so they should be okay. 
Something's wrong with the configuration.

The problem can be, that Tomcat doesn't have the necessary user rights 
to read some of the files under CATALINA_HOME/webapps, though it should 
present a different error message in that case.

Again, just to be sure, (not a very smart thing to do in a production 
environment), try to give the files all the rights they may need:

chmod 0777 /home/applications/apache-tomcat-6.0.18/lib/derbyclient.jar
chmod -cR 0777 /home/applications/apache-tomcat-6.0.18/webapps/ROOT

Then try again.

When the above doesn't help, I'll wipe the dust off my Mac and write-up 
an even more detailed walk-through by this evening (local time) ;-)


Regards,

    Stephan.

Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by Emmanuel Cecchet <ma...@frogthinker.org>.
Mike,

Just to check, which JVM are you using? What is the output of 'java 
-version'?
Did you make sure that both Tomcat and Derby where using the same JVM?

manu

teamderby wrote:
> Stephan,
>
> Thanks a lot for your response. Your time and effort is very much
> appreciated.
>
> I am unfortunately still having problems, the jsp message:
> org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of
> class '' for connect URL 'null'
>
> I have followed your instructions above exactly with a clean download of
> Tomcat. I can therefore assume it is something silly I am doing in setting
> up the database or launching tomcat. I can not see how you have set the
> environment variables or the classpath.
>
>
> After following your instructions exactly I have placed a clean derby
> installation at /derby. I opened two terminals. in one:
>
>  export DERBY_HOME=/derby
>  export PATH="$DERBY_HOME/bin:$PATH"
>  cd derby/bin
>  setNetworkClientCP                       // this apparently sets the
> classpath for the client driver
>  ij
>  ij> CONNECT 'jdbc:derby:db;create=true';     
>  ij> CREATE TABLE DB.....                 // and then SQL to create the
> tables.
>  java -jar $DERBY_HOME/lib/derbyrun.jar server start     // this seems to
> successfully start the db listening to port 1527
>
>
>
> In terminal two:
>
> cd CATALINA_HOME/bin
> ./startup.sh
>
> I then tried accessing localhost:8080 with your index.jsp page in ROOT. I
> understand I must be misunderstanding something. your help would be much
> appreciated. thanks,
>
> Mike


-- 
Emmanuel Cecchet
FTO @ Frog Thinker 
Open Source Development & Consulting
--
Web: http://www.frogthinker.org
email: manu@frogthinker.org
Skype: emmanuel_cecchet


Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by teamderby <mi...@gmail.com>.
Stephan,

Thanks a lot for your response. Your time and effort is very much
appreciated.

I am unfortunately still having problems, the jsp message:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of
class '' for connect URL 'null'

I have followed your instructions above exactly with a clean download of
Tomcat. I can therefore assume it is something silly I am doing in setting
up the database or launching tomcat. I can not see how you have set the
environment variables or the classpath.


After following your instructions exactly I have placed a clean derby
installation at /derby. I opened two terminals. in one:

 export DERBY_HOME=/derby
 export PATH="$DERBY_HOME/bin:$PATH"
 cd derby/bin
 setNetworkClientCP                       // this apparently sets the
classpath for the client driver
 ij
 ij> CONNECT 'jdbc:derby:db;create=true';     
 ij> CREATE TABLE DB.....                 // and then SQL to create the
tables.
 java -jar $DERBY_HOME/lib/derbyrun.jar server start     // this seems to
successfully start the db listening to port 1527



In terminal two:

cd CATALINA_HOME/bin
./startup.sh

I then tried accessing localhost:8080 with your index.jsp page in ROOT. I
understand I must be misunderstanding something. your help would be much
appreciated. thanks,

Mike


  




-- 
View this message in context: http://www.nabble.com/Tomcat-derby-JSP-%22java.sql.SQLException%3A-No-suitable-driver%22-tp20776666p20905681.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.


Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by "Stephan van Loendersloot (LIST)" <st...@republika.nl>.
Excuse me, something went wrong while copying the content of web.xml...


The following line:

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml ns/javaee/web-app_2_5.xsd">

Should be:

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">




Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by "Stephan van Loendersloot (LIST)" <st...@republika.nl>.
Below is the forwarded message that Mike sent to my e-mail address. I 
wanted to reply on the list, so others may benefit from it as well:

<quote>

mikehmurray@gmail.com wrote:
 thanks for all the replys,
 Steven, at this point i am simply trying to set up an tomcat webserver, 
and run a derby database on it. i will then write .jsp webpages that 
will contain sql commands that will operate on the derby database.

 i have managed to set up the webserver to then extent which it runs jsp 
pages that i can access.
 i have managed to set up the derby database to the extent which runs 
commands from the command line.

 i cannot however, get code in .jsp files to connect to the derby database.

 thanks.

</quote>



This may be a long post and it's mostly Tomcat-related configuration, so 
please bear with me:

The directories you posted for configuration files seem to be totally 
mixed up, so I suggest you start with a fresh Tomcat 
(http://tomcat.apache.org/download-60.cgi).

You unpacked Tomcat in the directory 
home/applications/apache-tomcat-6.0.18, from now on I'll refer to this 
as CATALINA_HOME.

Please note! All given paths and filenames in the following section are 
case-sensitive!



The first step is to copy derbyclient.jar to CATALINA_HOME/lib

Leave CATALINA_HOME/conf/server.xml alone for now, you don't need it 
(for this example).

In the directory CATALINA_HOME/webapps there's a directory ROOT. This is 
the default website that comes with Tomcat out-of-the-box (e.g. 
http://localhost:8080).

Make sure Tomcat is NOT started and delete everything in this directory, 
but not the directory itself.

Create a directory: CATALINA_HOME/webapps/ROOT/META-INF
Create a file here: CATALINA_HOME/webapps/ROOT/META-INF/context.xml

The content of context.xml file may look like this:

<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
    <Resource name="jdbc/db" auth="Container"
        type="javax.sql.DataSource"
        driverClassName="org.apache.derby.jdbc.ClientDriver"
        url="jdbc:derby://localhost:1527/db"
        username="DATABASEUSERNAME"
        password="DATABASEUSERPASSWORD"
        maxActive="10"
        maxIdle="5"
        removeAbandoned="true"
        removeAbandonedTimeout="60"
        logAbandoned="true"
        testOnBorrow="true"
        validationQuery="VALUES(1)"
    />
</Context>

This will give you connection-pooling and a lot of other configurable 
options from the start and you don't have to rewrite your application 
when you change these options or databases or... etc. etc.


Create another directory: CATALINA_HOME/webapps/ROOT/WEB-INF
Next, create the file CATALINA_HOME/webapps/ROOT/WEB-INF/web.xml

The minimal content of web.xml for this example:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:j2ee="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">
    <display-name>root</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <resource-ref>
        <description>JNDI www.mydomain.com Connection</description>
        <res-ref-name>jdbc/db</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
</web-app>


Finally, your index.jsp should be something like this:

<%@page language="java" import="java.sql.*, javax.sql.*, javax.naming.*" %>
<%
    Context ctx = new InitialContext();
    DataSource dataSource = (DataSource) 
ctx.lookup("java:comp/env/jdbc/db");
    if (dataSource == null) {
        throw new JspException(
            "java:comp/env/jdbc/db returned NULL");
    }

try {
    Connection conn = dataSource.getConnection();
    out.println("CONNECTED<br>");
    //body of code to go here

}  catch (Exception e)  {
   out.println(e);
   e.printStackTrace();
} finally {

}

%>


Now, start Derby, then start Tomcat.


This is tested, working code, but please drop us a line if doesn't work 
for you.


Regards,

    Stephan.





Re: Tomcat derby JSP "java.sql.SQLException: No suitable driver"

Posted by "Stephan van Loendersloot (LIST)" <st...@republika.nl>.

Michael Murray wrote:
> hi guys,
> I'm having a bit of trouble configuring apache tomcat to work with a 
> derby database on mac OS. Below are the steps I took. The error I get 
> when trying to acces the page from a jsp page is 
> "java.sql.SQLException: No suitable driver". Can anyone please help, 
> i'd be very very grateful.
>

Hello Michael,

I'll help, but first, it would be helpful if you told us *exactly* what 
it is that you're trying to do. It's not very clear right now, since you 
posted very unrelated things, while confusing the configuration of 
global resources in Tomcat with the configuration of the ROOT web 
application.

It seems (hard to guess) that you'd like to use Derby to control 
Tomcat's roles/groups and users (in Tomcat, this is called a Realm).

Please let me know the specifics and we'll work this out.

Regards,

    --Stephan.