Using JDBC, MySQL, Tomcat, Eclipse with Google RPC Web Toolkit


Robert Cooper has provided an excellent introduction of the Gooble Web Toolkit (GWT) and the source code can be downloaded from here.

His tutorial was about implementing RPC with the GWT. However the example that was provided was using wired-data to populate a FlexTable. I've modified it so that the data are now extracted from a MySQL database.

The modified application can be downloaded from here and then imported into Eclipse.

This is not a step by step tutorial but rather a guideline to implement RPC and then test it with the development shell or with tomcat.

I've used mysql-connector-java-3.0.16-ga-bin to access MySQL database. I would recommend to put that jar file in the same directory then gwt-user.jar and gwt-dev-windows.jar.

I've also added the following line into MyAPP02.gwt.xml:
<servlet path="/DataService" class="com.uniksoft.server.DataServiceImpl" />

and modified the MyAPP02-shell.cmd like this:


	@java -cp "%~dp0\src;%~dp0\bin;C:/google/gwt-windows-1.0.21/gwt-user.jar;
	C:/google/gwt-windows-1.0.21/gwt-dev-windows.jar;
	C:/google/gwt-windows-1.0.21/mysql-connector-java-3.0.16-ga-bin.jar" 
	com.google.gwt.dev.GWTShell -out "%~dp0\www" %* com.uniksoft.MyAPP02/MyAPP02.html
	
to make sure the Tomcat test-bed will see the mysql-connector-java-3.0.16-ga-bin file.

SQL to create the employees table

	CREATE TABLE `employees` (
 		id int(10) unsigned NOT NULL auto_increment,
		First_Name varchar(45) NOT NULL default ' ',
		Last_Name varchar(45) NOT NULL default ' ',
		Days int(10) unsigned default NULL,
		Hire_Date date default NULL,
		Bonus float default NULL,
		PRIMARY KEY  (id)
	);
	
SQL to populate the employees table

	mysql> insert into employees (First_Name, Last_Name, Days, Hire_Date, Bonus) values('Pierre', 'Lasante', 31, '1955/05/12', 65.1);
	mysql> insert into employees (First_Name, Last_Name, Days, Hire_Date, Bonus) values('Carole', 'Spenard', 32, '1962/11/18', 34.7);
	mysql> insert into employees (First_Name, Last_Name, Days, Hire_Date, Bonus) values('Bill', 'Gates', 33, '1950/01/01', 23.9);
	mysql> insert into employees (First_Name, Last_Name, Days, Hire_Date, Bonus) values('Larry', 'Page', 34, '1957/03/21', 59.2);
	
Table.gwt.xml
		
	
DataService.java

	package com.uniksoft.client;

	import com.google.gwt.user.client.rpc.RemoteService;

	public interface DataService extends RemoteService { 
		public Person[] getData();
	}
	
DataServiceAsync.java

	package com.uniksoft.client;

	import com.google.gwt.user.client.rpc.AsyncCallback;

	public interface DataServiceAsync { 
		public void getData(AsyncCallback callback); 
	}
	
DataServiceImpl.java

package com.uniksoft.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.uniksoft.client.DataService;
import com.uniksoft.client.Person;

public class DataServiceImpl extends RemoteServiceServlet implements DataService {
	
	public Person[] getData(){
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		ArrayList values = new ArrayList();
    	
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","username","password");
			stmt = con.createStatement();
			rs = stmt.executeQuery("SELECT First_Name, Last_Name, Days, Hire_Date, Bonus " + "FROM employees");
			while (rs.next()) {
				values.add( new Person(rs.getString(1), rs.getString(2), rs.getDate(4).toString(), rs.getString(5)));
			}
			con.close();
			stmt.close();
			rs.close();
		} catch(Exception e) {
			System.out.println(e.toString());
		}
		return (Person[]) values.toArray( new Person[values.size()] );
	}
}
	
TableEntryPoint.java

	package com.uniksoft.client;

	import com.google.gwt.core.client.EntryPoint;
	import com.google.gwt.core.client.GWT;
	import com.google.gwt.user.client.History;
	import com.google.gwt.user.client.HistoryListener;
	import com.google.gwt.user.client.Window;
	import com.google.gwt.user.client.rpc.AsyncCallback;
	import com.google.gwt.user.client.rpc.ServiceDefTarget;
	import com.google.gwt.user.client.ui.RootPanel;
	import com.google.gwt.user.client.ui.SourcesTableEvents;
	import com.google.gwt.user.client.ui.TableListener;


	/**
	 * Entry point classes define onModuleLoad().
	 */
	public class TableEntryPoint implements EntryPoint, HistoryListener {

	private Table table;
	
	public void onModuleLoad() {
		table = new Table( null, "myTable");
		table.setStyleName("myTable");
		RootPanel.get().add(table);
		getData();
		
		// Add a new listener to record the row history
		table.addTableListener( new TableListener() {
			public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
				History.newItem( "" + row );
			}
		});
		History.addHistoryListener( this );
	}

	private void getData() {
		DataServiceAsync dataService = (DataServiceAsync) GWT.create( DataService.class );
		ServiceDefTarget endpoint = (ServiceDefTarget) dataService;
		// This endpoint is to be used in hosted mode
		endpoint.setServiceEntryPoint("/DataService");
		// This endpoint is to be used with an external Tomcat and servlet
		//endpoint.setServiceEntryPoint("http://localhost:8080/MyAPP02_RPC/data");
		dataService.getData( new AsyncCallback() {
			public void onSuccess(Object result) {
				table.setSource( new SimpleDataSource( (Person[]) result ));
				if (History.getToken().length() != 0) {
					table.onCellClicked( table, Integer.parseInt( History.getToken()), 0);
				}
			}
			public void onFailure(Throwable caught) {
				Window.alert("Unable to get data from server.");
			}	
		});
	}
	
	public void onHistoryChanged(String historyToken){
		table.onCellClicked( table, Integer.parseInt( historyToken ), 0);
	}
}
	
Hosted by www.Geocities.ws

1