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
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
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
To download the source code please visit this link http://groups.google.com/group/gtug-ph-davao/browse_thread/thread/ff7576daa39f1654
Comments
you can search the net for more precise examples.
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...
Can you send me your email so i can send to you the complete file.
mark
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
Thanks
Thanks
Can you send me coode too..
sridhar.vennela@gmail.com
Thanks for the wonderful explanation.
Can you pls send me the GWT+MYSQL code on my email: niranjan@umpire.com
Thanks You,
niranjan
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.
i would love the complete source? can u send it please???
jpamies@gmail.com
thanks a lot
Can you send me the complete code to priu_84@yahoo.com
would you please send me code to ryungz@gmail.com? Thanks in advance.
sauternes_chocg@hotmail.com
samantha
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
Regards.
Enrique
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
Can you pls send me the complet GWT+MYSQL code on my email: kidane_henoke@yahoo.com
Thanks You,
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
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.
as i am beginner in gwt so i need it to refer. could you pls send me the complete GWT+MYSQL code on my email:prajaktapisal233@gmail.com
Thanks You,