Connect to SQL Server

12 views
Skip to first unread message

Juan Sequeda

unread,
Aug 8, 2007, 1:38:05 PM8/8/07
to Google Web Toolkit
Hi.

I am having problems connecting to SQL Server. I always get :

java.lang.ClassNotFoundException:
com.microsoft.sqlserver.jdbc.SQLServerDriver

My connection code works because I have tried it out separately and I
don't get that error.

I have other jar files imported through eclipse and haven't had a
problem with it.

Any help?

Thanks.

Chad Bourque

unread,
Aug 8, 2007, 2:21:50 PM8/8/07
to Google-We...@googlegroups.com
Juan,
 
Are you trying to do that from the server or client? You can't do it from the client. Only javascript runs there and this class is not emulated by GWT (check the online docs for emulated classes). You should be able to do this on the server without problems. I know I and many others are connecting to databases. Oh, and to communicate between the client and server, use RPC (again, just check the docs).
 
HTH,
Chad

 

Juan Sequeda

unread,
Aug 8, 2007, 2:52:41 PM8/8/07
to Google Web Toolkit
HI

Of course I'm doing it with the server...

I import

import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;

and have this:

<snip>
String connectionUrl = "jdbc:sqlserver://*****:
1433;databaseName=***;user=***;password=***";
// Declare the JDBC objects.
int count = 0;
String result = "";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;

try{
// Establish the connection.

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
// Create and execute an SQL statement that returns some
data.
String SQL = "SELECT Name FROM ODB_Terms";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while(rs.next()){
count++;
}
result = ""+count;
}
catch(Exception e){
result = e.toString();
}

return result;

</snip>

and it's returning the catch...

What am I doing wrong?

On Aug 8, 1:21 pm, "Chad Bourque" <chad...@gmail.com> wrote:
> Juan,
>
> Are you trying to do that from the server or client? You can't do it from
> the client. Only javascript runs there and this class is not emulated by GWT
> (check the online docs for emulated classes). You should be able to do this
> on the server without problems. I know I and many others are connecting to
> databases. Oh, and to communicate between the client and server, use RPC
> (again, just check the docs).
>
> HTH,
> Chad
>

> On 8/8/07, Juan Sequeda <juanfeder...@gmail.com> wrote:
>
>
>
>
>
> > Hi.
>
> > I am having problems connecting to SQL Server. I always get :
>
> > java.lang.ClassNotFoundException:
> > com.microsoft.sqlserver.jdbc.SQLServerDriver
>
> > My connection code works because I have tried it out separately and I
> > don't get that error.
>
> > I have other jar files imported through eclipse and haven't had a
> > problem with it.
>
> > Any help?
>
> > Thanks.
>

> --
> There are two types of people in the world:
> * Those who need closure

Dean S. Jones

unread,
Aug 8, 2007, 2:59:16 PM8/8/07
to Google Web Toolkit
Have you added all your required .*jars to you WEB-INF/lib folder, and
not just in Eclipse's run/compile path???

Juan Sequeda

unread,
Aug 8, 2007, 3:22:20 PM8/8/07
to Google Web Toolkit
forgive my ignorance but where is that folder located. That may be the
problem because I only added it to Eclipse's path.

Do I have to change something in my gwt.xml file?

Dean S. Jones

unread,
Aug 8, 2007, 3:32:36 PM8/8/07
to Google Web Toolkit
It's located in your Eclipse project under either www/WEB-INF/lib,
WebRoot/WEB-INF/lib, or WebContent/WEB-INF/lib,
depending on how your web project was created.

Juan Sequeda

unread,
Aug 14, 2007, 3:09:37 PM8/14/07
to Google Web Toolkit
Hi again...

I have all the jars in C:\gwt\tomcat\webapps\ROOT\WEB-INF

but I still can't connect to sql server. Any help?

Juan Sequeda

unread,
Aug 14, 2007, 3:43:14 PM8/14/07
to Google Web Toolkit
The error I am getting is:

java.lang.ClassNotFoundException:
com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at
com.morphster.server.TreeServiceImpl.getRootsBD(TreeServiceImpl.java:
92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at
com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(Remot
eServiceServlet.java:281)
at
com.google.gwt.user.server.rpc.RemoteServiceServlet.doPost(RemoteServ
iceServlet.java:167)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:
709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:
802)
at
com.google.gwt.dev.shell.GWTShellServlet.service(GWTShellServlet.java
:248)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:
802)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:237)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:157)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:214)
at
org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at
org.apache.catalina.core.StandardContextValve.invokeInternal(Standard
ContextValve.java:198)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:152)
at
org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:137)
at
org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:118)
at
org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:102)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
at
org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at
org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)

at
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:16
0)
at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:799)
at org.apache.coyote.http11.Http11Protocol
$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:705)
at
org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java
:577)
at org.apache.tomcat.util.threads.ThreadPool
$ControlRunnable.run(ThreadP
ool.java:683)
at java.lang.Thread.run(Unknown Source)


Thanks in advance for all your help. I have been on this for a while
and now I am kind of desperate. Thanks

Dean S. Jones

unread,
Aug 14, 2007, 3:43:50 PM8/14/07
to Google Web Toolkit
Is your application deployed under the ROOT context? or named the same
as the project?

in YOUR PROJECT's Context add the jar files to WEB-INF/lib ... note
the "lib" at the end...

Juan Sequeda

unread,
Aug 14, 2007, 3:51:19 PM8/14/07
to Google Web Toolkit
My eclipse project is located in C:\gwt (is that wrong??)

And then my packages are in C:\gwt\src\com\morphster

The only WEB-INF/lib I found on my harddrive (I searched it all) was C:


\gwt\tomcat\webapps\ROOT\WEB-INF

The thing is I am also using another jar file and that has no problem
at all.. there is only problems with the sqlserver.

Thanks for your help.

Chad Bourque

unread,
Aug 14, 2007, 4:10:11 PM8/14/07
to Google-We...@googlegroups.com
Juan,
 
You have to manually create the WEB-INF and lib folders manually. You can see some steps on how I create a war file here:
 
 
Basically, after you run the compile script that is created by the appCreator script, you will have a www folder for your output to publish to the web. You create these two folders (and a web.xml file) under that folder.
 
HTH,
Chad

 

Brian Munroe

unread,
Aug 14, 2007, 8:24:25 PM8/14/07
to Google Web Toolkit
On Aug 14, 1:10 pm, "Chad Bourque" <chad...@gmail.com> wrote:
> Juan,
>
> You have to manually create the WEB-INF and lib folders manually. You can
> see some steps on how I create a war file here:
>
> http://www.charisacademy.com/gwt/CreateWar.htm
>
> Basically, after you run the compile script that is created by the
> appCreator script, you will have a www folder for your output to publish to
> the web. You create these two folders (and a web.xml file) under that
> folder.
>

One thing to be careful of, sometimes the Tomcat container needs to
manage the jar files in $CATALINA_HOME/common/lib. I know this is an
issue with Oracle and connection pooling, I had copies of the Oracle
JDBC jar file in the local app's WEB-INF/lib and then I tried
$CATALINA_HOME/shared/lib - both caused a class not found error. It
didn't work until I placed the file in common/lib where the Tomcat
container could use them too.

Only web applications can access and use jar files in shared/lib.

I don't know if the MS SQL driver is having the same issue or not, but
I just wanted to point that out.

Juan, how do you have Tomcat installed?

Brian Munroe

unread,
Aug 15, 2007, 10:30:41 AM8/15/07
to Google Web Toolkit
On Aug 14, 5:24 pm, Brian Munroe <brian.e.mun...@gmail.com> wrote:

>
> One thing to be careful of, sometimes the Tomcat container needs to
> manage the jar files in $CATALINA_HOME/common/lib. I know this is an
> issue with Oracle and connection pooling, I had copies of the Oracle
> JDBC jar file in the local app's WEB-INF/lib and then I tried
> $CATALINA_HOME/shared/lib - both caused a class not found error. It
> didn't work until I placed the file in common/lib where the Tomcat
> container could use them too.
>
> Only web applications can access and use jar files in shared/lib.
>
> I don't know if the MS SQL driver is having the same issue or not, but
> I just wanted to point that out.
>
> Juan, how do you have Tomcat installed?

One thing to clarify about my post. I was thinking a traditional
Tomcat configuration, not running it in hosted mode. But, I think you
can create a common/lib directory structure underneath the Tomcat
directory in your project folder, place your MS JDBC driver files in
there and it should do the same thing.

Juan Sequeda

unread,
Aug 15, 2007, 11:25:34 AM8/15/07
to Google Web Toolkit
Hi all

I need it to work in the hosted mode.

the tomcat directory in the gwt already has the WEB-INF (C:\gwt\tomcat
\webapps\ROOT\WEB-INF\lib) and inside the lib directory I have the
sqljdbc.jar, but it still doesn't work?

Do I have to change something in the web.xml file? If so, what is it?

Thanks a lot!!!!

Chad Bourque

unread,
Aug 15, 2007, 11:33:48 AM8/15/07
to Google-We...@googlegroups.com
Juan,
 
In Eclipse, did you add it to the Classpath as an External JAR in the Run dialog? I believe that's all you should need to do to get it to work in hosted mode. If you want it to work when you run the shell script (generated by the appCreator script), you have to add it to the classpath in that script (I believe).
 
I generally don't worry about changing the shell script as I never run it that way. I always either just Run from Eclipse or deploy to my external Tomcat via a WAR file. So, the only things I do is add it to the Eclipse dialog as stated above for development and debugging, and I follow my steps listed on the web (link provided earlier in this thread) for deployed testing.
 
HTH,
Chad

 

Juan Sequeda

unread,
Aug 15, 2007, 11:39:56 AM8/15/07
to Google Web Toolkit
Wait.. I don't use the Run dialog in eclipse. I just save and then
double click on Ontobrowser-shell in the gwt directory. Is that my
problem?

But then how do I use the Run dialog (don't I have to a main to use
it?)

On Aug 15, 10:33 am, "Chad Bourque" <chad...@gmail.com> wrote:
> Juan,
>
> In Eclipse, did you add it to the Classpath as an External JAR in the Run
> dialog? I believe that's all you should need to do to get it to work in
> hosted mode. If you want it to work when you run the shell script (generated
> by the appCreator script), you have to add it to the classpath in that
> script (I believe).
>
> I generally don't worry about changing the shell script as I never run it
> that way. I always either just Run from Eclipse or deploy to my external
> Tomcat via a WAR file. So, the only things I do is add it to the Eclipse
> dialog as stated above for development and debugging, and I follow my steps
> listed on the web (link provided earlier in this thread) for deployed
> testing.
>
> HTH,
> Chad
>

> --
> There are two types of people in the world:

Chad Bourque

unread,
Aug 15, 2007, 11:48:56 AM8/15/07
to Google-We...@googlegroups.com
Juan,
 
When you click the Run button the first time, I believe the dialog appears. If you expand Java Application, you will see your main class. Select it. The project should get populated with your project name and the main class should get populated with com.google.gwt.dev.GWTShell. Click over to the Classpath tab, Select your Project (default classpath) from the tree view (under User Entries) and the click the Add External JARs... button from the right. Browse to and select your data driver. Then, click Run. It should work for you. After that, you should only have to click the Run button and not bother with any settings.
 
The way you are doing it now should work if you manually edit the classpath in that script to include your driver.
 
HTH,
Chad
 

Brian Munroe

unread,
Aug 15, 2007, 1:53:56 PM8/15/07
to Google Web Toolkit
On Aug 15, 8:25 am, Juan Sequeda <juanfeder...@gmail.com> wrote:

>
> the tomcat directory in the gwt already has the WEB-INF (C:\gwt\tomcat
> \webapps\ROOT\WEB-INF\lib) and inside the lib directory I have the
> sqljdbc.jar, but it still doesn't work?
>
> Do I have to change something in the web.xml file? If so, what is it?
>

I was just pointing out that when I have worked with Oracle drivers
with a classic tomcat configuration (not GWT), I got the same class
not found errors that you mentioned. I had placed my JDBC jar files
in the local WEB-INF/lib and then later in shared/lib, but I kept
getting the same error. After some poking around and reading the
Tomcat classloader [1] documentation like 10 times, I realized that
Tomcat needed access to the driver jars, but by placing them in WEB-
INF/lib or shared/lib, it was unable to 'see' them. The only way it
could access them was through common/lib. Since GWT hosted mode seems
to use an embedded Tomcat instance, I figure it might be the same
issue.

If the problem lies where I think it is, you shouldn't have to modify
anything in web.xml, etc.

The last thing I want to do is confuse the thread even more, I may be
really off base, but I saw the commonality between your problem and
mine, and though I'd offer my two cents.

[1] - http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html

Reply all
Reply to author
Forward
0 new messages