GWT+MySQL Best Practice

Migrating from GWT-EXT to native GWT has never been an easy task as I expect it would. I took me some moths to really absorb the fundamentals of its Java code, structure and pattern.

The price for taking this challenge was undoubtedly more gratifying especially on performance and size. I had lesser reboot due to memory leak and I had better handling on syntax especially on database due to its strict use of its object.

Here are some improvements on my code:

String url = "jdbc:mysql://localhost:3306/";
String db = "mydb";
String driver = "com.mysql.jdbc.Driver";
String user = "myuser";
String password = "mypassword";

public String connectDatabase() {
String resultString;


conn = DriverManager.getConnection(url+db, user, password);
//conn = DriverManager.getConnection(url, user, password);

catch(Exception e){
System.err.println("Unable to connect");
return resultString;

public ArrayList querryReturnListofProducts(){
ArrayList info = new ArrayList();

if (connectDatabase().equals("Connected")){
try {
select=conn.prepareStatement("SELECT * FROM productsmain");


while ({
DatabaseDTO dbaseDTO=new DatabaseDTO();
dbaseDTO.setSQLRemarks("Access Granted");




}catch (SQLException e) {


return info;

In simpler term you must assign a value to your Arraylist function. In my case its a "DatabaseDTO"

Which contains the following code.

public class DatabaseDTO implements IsSerializable{
private String productName;
private String productImagelocation;
private String productLinklocation;
private String sqlremarks;

public void setSQLRemarks(String sqlremarks) {
this.sqlremarks = sqlremarks;


public String getSQLRemarks() {
return this.sqlremarks;

public void setproductName(String productName) {
this.productName = productName;


public String getproductName() {
return this.productName;

public void setproductImagelocation(String productImagelocation) {
this.productImagelocation = productImagelocation;


public String getproductImagelocation() {
return this.productImagelocation;

public void setproductLinklocation(String productLinklocation) {
this.productLinklocation = productLinklocation;


public String getproductLinklocation() {
return this.productLinklocation;

To retrieve your data from the database use this code:

final DatabaseServiceAsync databaseSVC=DatabaseService.Util.getInstance();
databaseSVC.querryReturnListofProducts(new AsyncCallback

public void onFailure(Throwable caught) {
//Window.alert("Failed","Query Statement Error: Failed to get response from server");


public void onSuccess(Object result) {

ArrayList data= (ArrayList) result;

for(Iterator it = data.iterator(); it.hasNext();){
DatabaseDTO dbaseDTO = (DatabaseDTO);




One last important reminder to make this code run is to make sure that your service must be assign with a value instead of a default object that is being assign.

public interface DatabaseService extends RemoteService {

public static final String SERVICE_URI = "DatabaseService";

public static class Util {

public static DatabaseServiceAsync getInstance() {

DatabaseServiceAsync instance = (DatabaseServiceAsync) GWT
ServiceDefTarget target = (ServiceDefTarget) instance;
target.setServiceEntryPoint(GWT.getModuleBaseURL() + SERVICE_URI);
return instance;

public String connectDatabase();
public ArrayList querryReturnListofProducts();

I know it's pretty kinda complicated but here is the result...

In summary, in order to attain better performance never ignore warnings by assigning proper object on them using DTO(Data Transfer Object).


manyo said…
hi Maks. good tutorial. but how do you get the connectivity? how do you set up the database driver from scratch?
maks said… the mysql jar file. add it to your eclipse project. configure the build path to assign the classess. then assign the code.

you can search the net for more precise examples.
manyo said…
got it. thanks a lot.
sabir said…
i was trying to connect MYSQL with GWT using similar trick as you do. Can you post the complete codes for connection part?

I was just trying to use your connectDatabase() part but didn't work.

for instance i used:

public interface DatabaseService extends RemoteService {

public String connectDatabase();


final DatabaseServiceAsync databaseSVC= GWT
databaseSVC.connectDatabase(new AsyncCallback(){
public void onFailure(Throwable caught) {
public void onSuccess(String result) {
maks said…
hi sabir,

Can you send me your email so i can send to you the complete file.

sabir said…
This comment has been removed by the author.
Anonymous said…
Is there anyway you can post a blogger link to your GWT+MySQL Best Practic tutorial source code?
balaji said…
Hey Maks,

I was searching to get such a tutorial for long time, Could you please send me the complete source code for me.

I just want to know from the scrap for the project how you did it?
my email:
Anonymous said…
Hi, please send me the complete source code at
Hi, please send me the complete source code at
maks said…
I just sent you the files that you need.
Sridhar said…

Can you send me coode too..
Anonymous said…
Hi Maks,
Thanks for the wonderful explanation.
Can you pls send me the GWT+MYSQL code on my email:
Thanks You,
qsdfqs said…

Can you send me code too please.

I try to setup a connection between GWT client / gwt server via Mysql but i'm stuck.
popadom said…
great tut,

i would love the complete source? can u send it please???

thanks a lot
Pri said…
hi maks,

Can you send me the complete code to
SaMueL said…
hi, can u send mi a complete code at
youngju said…
Hi Maks,
would you please send me code to Thanks in advance.
maks said…
code sent...
maks said…
To all those who need the code I have uploaded the code to GTUG Davao Philippines please visit this link
Anonymous said…
Cool blog! Nice to see all those filipino desserts my mom makes haha. I followed your download link but it doesn't work anymore.. I'd really appreciate a fresh download link. Thanks!
shire chui said…
hi, can u sent me ur sql and gwt codes to me ? i follow the link at the blog but the page is not longer exist..caould u pls help me out? thnx a lot..

Stacy said…
Hi Maks,

I would really love this example as I ma struggling with GWT talking to MYSQL

Could you please send it to me at
Enrique said…
hi Maks, great tutorial can you upload the code again please?


Anmbr said…
You could upload which driver is using for one MySQL?
Majid said…

I tried the link you gave for downloading the source code :

But there is nothing there, I will appreciate if you can upload it again or send it to my email :

Anonymous said…
This is wonderful explanation.
Can you pls send me the complet GWT+MYSQL code on my email:
Thanks You,
Anonymous said…
please send me the complet code
rihab said…
hi MArk,

please can you send me the tuto+ your code source to, I have some problems with my connection MySql :s

Bemyself said…
please can you send me the tuto+ your code source to Thanks
Am also trying to use mysql along with GWT...

Whether GWT+mysql is easier or GWT + PHP is easier to retrieve & show records from Mysql...

Could u pls guide me.. Instead of sending codes. i need the steps to connect to MySQL.
hazel said…
ei maks! can you send me the full version of your code:) and if you dont mind can i get ur skype account so we can talk about my problem in my app.. thanks in advance...
osmium said…
Can u send me the source? thank you very much! My mail is
Anonymous said…
can you sand me too
Nha Luong said…
I realy need the source for research, Can you send me the source? thank you very much! My mail is

Popular posts from this blog

John Gokongwei Speech before Ateneo 2004 Graduates

SM Mall of Asia