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:

DatabaseServiceImpl.java

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;

try{

Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+db, user, password);
//conn = DriverManager.getConnection(url, user, password);

resultString="Connected";
}
catch(Exception e){
System.err.println("Unable to connect");
e.printStackTrace();
//System.exit(1);
resultString="failed";
}
return resultString;
}


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

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

result=select.executeQuery();

while (result.next()){
DatabaseDTO dbaseDTO=new DatabaseDTO();
dbaseDTO.setproductName(result.getString("name"));
dbaseDTO.setproductImagelocation(result.getString("imagelocation"));
dbaseDTO.setproductLinklocation(result.getString("linklocation"));
dbaseDTO.setSQLRemarks("Access Granted");

info.add(dbaseDTO);

}


select.close();
conn.close();


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

}


}
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.

DatabaseDTO.java

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");

}

@SuppressWarnings("unchecked")
public void onSuccess(Object result) {

ArrayList data= (ArrayList) result;

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

}

}

});


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.

DatabaseService.java

public interface DatabaseService extends RemoteService {

public static final String SERVICE_URI = "DatabaseService";

public static class Util {

public static DatabaseServiceAsync getInstance() {

DatabaseServiceAsync instance = (DatabaseServiceAsync) GWT
.create(DatabaseService.class);
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).

Comments

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…
simple...download 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:

DatabaseService.java

public interface DatabaseService extends RemoteService {

public String connectDatabase();

}


IN THE MAIN CLASS:
final DatabaseServiceAsync databaseSVC= GWT
.create(DatabaseService.class);
databaseSVC.connectDatabase(new AsyncCallback(){
public void onFailure(Throwable caught) {
Window.alert("Failed");
}
public void onSuccess(String result) {
System.out.println(result);
}});
thanks...
maks said…
hi sabir,

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

mark
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: venkivoice@gmail.com
Cheers,
Venkat
Anonymous said…
Hi, please send me the complete source code at jodemakara@yahoo.com
Thanks
Hi, please send me the complete source code at josemakara@yahoo.com
Thanks
maks said…
I just sent you the files that you need.
Sridhar said…
Hi,

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

Can you send me code too please.
lamarque.matthieu@gmail.com

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???
jpamies@gmail.com

thanks a lot
Pri said…
hi maks,

Can you send me the complete code to priu_84@yahoo.com
SaMueL said…
hi, can u send mi a complete code at samuel_foon_is@yahoo.com
youngju said…
Hi Maks,
would you please send me code to ryungz@gmail.com? 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 http://groups.google.com/group/gtug-ph-davao/browse_thread/thread/ff7576daa39f1654
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..
sauternes_chocg@hotmail.com

samantha
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

swaugh@hotmail.com
Enrique said…
hi Maks, great tutorial can you upload the code again please?

Regards.

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

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

http://groups.google.com/group/gtug-ph-davao/browse_thread/thread/ff7576daa39f1654

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

thanks
Anonymous said…
This is wonderful explanation.
Can you pls send me the complet GWT+MYSQL code on my email: kidane_henoke@yahoo.com
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 rihab.saidi@gmail.com, I have some problems with my connection MySql :s

thanks
Bemyself said…
please can you send me the tuto+ your code source to bemyself1983@gmail.com. Thanks
KISHORE said…
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 hungrycannibal@myopera.com.
Anonymous said…
can you sand me too duhbanniy@gmail.com
Nha Luong said…
I realy need the source for research, Can you send me the source? thank you very much! My mail is lgthnha@yahoo.com.vn

Popular posts from this blog

John Gokongwei Speech before Ateneo 2004 Graduates

SM Mall of Asia