Connect SQL Server via GWT

Environment:

  • Eclipse 3.5
  • Google Web Toolkit Eclipse plug-in 2.0.4
  • SEQL Server 2005

Google plug-in uses Jetty [1] as web (Servlet) container. The key is Jetty configuration.

Step 1: create jetty configuration xml file

Create file named “jetty-web.xml” under /war/WEB-INF/

The content is:

<?xml version="1.0" encoding="UTF-8"?>
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
    <New id="DSTest" class="org.mortbay.jetty.plus.naming.Resource">
        <Arg>jdbc/DSTest</Arg>
        <Arg>
            <New class="net.sourceforge.jtds.jdbcx.JtdsDataSource">
                <Set name="User">sa</Set>
                <Set name="Password">123456</Set>
                <Set name="DatabaseName">Repository</Set>
                <Set name="ServerName">localhost</Set>
                <Set name="PortNumber">1433</Set>
            </New>
        </Arg>
    </New>
</Configure>

Step 2: set up JDBC driver

Download jtds JDBC driver [2] and copy the jar file into /war/WEB-INF/lib/ and your project class path. I firstly use the official Microsoft driver but did no work.

To connect other database, please refer to [3].

Step 3: code snippet

The code to access database should be server side. To get a JDBC connection, the code is:

    public static Connection getSQLServerConnection() {
        try {
            if (con == null || con.isClosed()) {
                Context context = new InitialContext();
                DataSource dataSource = (DataSource) context
                .lookup("jdbc/DSTest");
                con = dataSource.getConnection();
            }

        } catch (NamingException e) {
            e.printStackTrace();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

The rest code is same as normal JDBC programming.

Step 4: set up jetty jar

Down jetty-naming and jetty-plus jar file from [4] and copy to /war/WEB-INF/lib/ and your project class path.

Reference:

[1] Jetty: http://en.wikipedia.org/wiki/Jetty_%28web_server%29

[2] Jtds: http://jtds.sourceforge.net/index.html

[3] Jetty data source: http://docs.codehaus.org/display/JETTY/DataSource+Examples

[4] Jetty jar: http://dist.codehaus.org/jetty/jetty-6.1.16/

Advertisements

3 thoughts on “Connect SQL Server via GWT

  1. I’m using: Eclipse Helios, Windows 7 GWT-plugin.
    In my configuration javax-naming is not supported.
    Which files I have to import?

  2. My environement : Windows 7, Eclipse Helios, GWT plugin. In my environement:
    import javax.naming.InitialContext;
    import javax.naming.*;
    is not supported by GWT. What I can do?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s