Connect to a SqlServer

22 views
Skip to first unread message

Rahul

unread,
Jul 6, 2009, 12:02:20 PM7/6/09
to Google Web Toolkit
Hi
I am new to google web toolkit.
I need to connect to a sqlserver 2005 database and show all its tables
and columns in an tree fashion on to my website.
I believe this is done getting connected to an sqlserver and getting
its metadata information (this should be implemented server side) and
presenting in the UI in the tree fashion(done at the client side).

There are different ways to connect to a server, like RPC and JSON et
al.
Could anyone tell me which method should i use ? Also are there any
tutorials to connect to the database ?

Sincerely,
Rahul

Piotr Kirklewski

unread,
Jul 6, 2009, 2:17:36 PM7/6/09
to Google-We...@googlegroups.com
Hi mate

When you build your GWT application with Eclipse 3.4 its using RPC
I'm connecting to my postgres database so you need to research on the
driver youneed for SQL server
.
So:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;

c =
DriverManager.getConnection("jdbc:postgresql://192.168.55.101/databasename",
"username", "pass");
Statement select =
c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.TYPE_SCROLL_SENSITIVE);
ResultSet res = select.executeQuery("select * from fred;");

Next stick the results int an array and grab them on the client side
from "reusult" :

markers[x][0] = new String(result[x][0]);
markers[x][1] = new String(result[x][1]);
markers[x][2] = new Float(result[x][2]);
markers[x][3] = new Float(result[x][3]);


I can;t help much with the tree view though.


Regards

Peter

Rahul

unread,
Jul 6, 2009, 12:56:21 PM7/6/09
to Google Web Toolkit
Hi,
I am able to find tutorials connecting to an sqlserver using RPC. Are
there any tutorials from JSON. What advantages would JSON give over
RPC to connect to the database? Also is there any other way to connect
to the database using google web toolkit?

Rahul

unread,
Jul 6, 2009, 2:38:15 PM7/6/09
to Google Web Toolkit
Hi Peter
Thanks for the reply
I presently have the driver I need to connect for sqlserver.
Reading the tutorials one needs to connect with sqlserver via RPC, for
database connectivity I need to follow all the basic steps in there?
Sorry to ask this because I am getting confused by this

1. Create a service on the server.
2. Invoke the service from the client.
3. Serialize the data objects.
4. Handle exceptions: checked and unexpected.

I need to follow all these steps to connect, right?
Thanks a lot for reply

On Jul 6, 2:17 pm, Piotr Kirklewski <pkirklew...@gabaedevelopment.com>
wrote:

Piotr Kirklewski

unread,
Jul 6, 2009, 4:40:20 PM7/6/09
to Google-We...@googlegroups.com
Hi
I'm not sure if  you use JASON to connect to the database, I would rather use it  for sending data over to the client side.
It's hard to say if JASON is in any way better than RPC.
GWT RPC doesn't want to send Object arrays, it complains that they are to complex.
You need to convert all your data types in the array to one type (string for example) and then convert them back on the client side.
I'm using this method rather than JSON.

Regards
PEter

Piotr Kirklewski

unread,
Jul 6, 2009, 5:00:39 PM7/6/09
to Google-We...@googlegroups.com
Hi mate

Just create your app with eclipse 3.4.

RPC will be already there with all the elements required..

Then you just have to change the app and adjust it to your needs.

Two interfaces are already there: Greetingservice.java and GreetingServiceAsync.java.

GreetingServiceImpl.java is the server side code.

Yourappname.java is on the client side.

You need to add the database driver to your classpath >> BuildPath.
Also you need to copy the driver to your war/WEB-INF/lib
You need to turn off the GoogleAppEngine (right click your project >> properties >> AppEngine  ) -otherwise the application is complaining that no suitable driver was found.

After you make your connection to the database and get the results -  you need to put it into an array converting everything to String, Change the code in the interfaces so they work with an aray ...

example:

package com.gabae.client;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import com.google.gwt.maps.client.geom.LatLng;

/**
 * The client side stub for the RPC service.
 */
@RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
    String[][] greetServer(String[] input); //you send one dimensional array of strings from the client to the server and recieve multidin=mensionale Strin[][] array from the server.
}

The GreetingServiceImpl.java must then return String[][] not just String...


Return the array in the GreetingServiceImpl.java and Change YourAppname.java so its also working with the array Strin[][] rather that with a String.

Follow this http://www-lehre.inf.uos.de/~btenberg/misc/DB-Access-in-GWT-The-Missing-Tutorial.pdf

But remember that the guy did several mistakes so yu need to think while doing that.

Reagrds

Peter
Reply all
Reply to author
Forward
0 new messages