Table of contents
1.
Introduction
2.
Installation
3.
Load Database
4.
Dependencies
5.
GWT Database implementation Example
6.
Frequently Asked Questions
6.1.
What is the purpose of GWT?
6.2.
What is a GWT servlet?
6.3.
What is a Smart GWT?
6.4.
How do the GWT applications run in the end user's Web browser?
6.5.
How does the GWT compiler work?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

GWT Database

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

For creating a  complete and working full stack application, it is necessary to connect it to a database. GWT can easily connect with a database and make the development easier and faster. 

GWT intro image

The following article will dive deeper into GWT Database and explore its installation,  functionality, and example. So let's get started right away.

Installation

Using GWT, we will set up database connectivity in this lesson. Database setup can be done using Eclipse.

The Installable HSQLDB plugin is already present in Eclipse. Installing plugins is simple to do. The database manager GUI is called, and the plugin offers numerous capabilities, including starting and stopping databases. The configuration steps here are listed below-

  • Go to Help → Eclipse MarketPlace, search the keyword HSQLDB and click the install button.
  • Now, select Window → Show View → Others and in the Show View dialog, expand the HSQL Database server and select HSQLDB Server to open the HSQLDB view.
  • Click on the View menu and select Preferences from the drop-down menu. Enter the values as shown in the Figure.
  • Start the database and open the database manager by choosing Show database Client from the drop-down menu.

Load Database

To load data, we utilize RStore, which offers the following features-

  • It improves Java persistence classes.
  • The database schema is created.
  • The database schema is removed.
  • It stores and gets things from the database.

Dependencies

HSQLDB, Data Nucleus JDO, MyBatis, and Hibernate libraries are needed for Rstore. The dependencies are listed in the table below.

GWT Database implementation Example

Adding Mysql configuration to GWT

LoginScreen.gwt.xml

The following XML code is for the login screen performing functions like specifying the app entry point and inheriting the core web toolkit functionality.

<module>  
 <!-- Inherit the core Web Toolkit stuff. -->  
 <inherits name='com.google.gwt.user.User'/>  
 <!-- Specify the app entry point class. -->  
 <entry-point class='com.yourdomain.projectname.client.LoginScreen'/>  
 <!-- servlet context - path is arbritray, but must match up with the rpc init  
inside java class -->  
 <!-- Tomcat will listen for this from the server and waits for rpc request in  
this context -->  
 <servlet class="com.yourdomain.projectname.server.MySQLConnection"path="/MySQLConnection" />  
 <inherits name="com.google.gwt.user.theme.standard.Standard"/>  
 <inherits name="com.google.gwt.user.theme.chrome.Chrome"/>  
 <inherits name="com.google.gwt.user.theme.dark.Dark"/>  
</module>  


Database Connection

DBConnection.java

Code for database connection

public interface DBConnection extends RemoteService  
 {  
 public User authenticateUser(String user, String pass);  
 }  
You can also try this code with Online Java Compiler
Run Code


DBConnectionAsync.java

Code for asynchronous database connection

public interface DBConnectionAsync {  
 public void authenticateUser(String user, String pass, AsyncCallback<User>  
callback);  
}  
You can also try this code with Online Java Compiler
Run Code

 

User.java (Frontend designing)

The following code is responsible for the frontend design and its connections to the MYSQL.

import com.google.gwt.user.client.rpc.IsSerializable;  
public class User implements IsSerializable {  
 private String username;  
 private String password;  
 @SuppressWarnings("unused")  
 private User() {  
 //just here because GWT wants it.  
 }  
 public User(String username, String password) {  
 this.username = username;  
 this.password = password;  
 }  
}  
MySQLConnection.java (Creating Servlet)
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.PreparedStatement;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.util.Vector;  
import com.google.gwt.user.server.rpc.RemoteServiceServlet;  
import com.yourdomain.projectname.client.User;  
public class MySQLConnection extends RemoteServiceServlet implements DBConnection {  
 private Connection conn = null;  
 private String status;  
 private String url = "jdbc:mysql://yourDBserver/yourDBname";  
 private String user = "DBuser";  
 private String pass = "DBpass";  
 public MySQLConnection() {  
 try {  
 Class.forName("com.mysql.jdbc.Driver").newInstance();  
 conn = DriverManager.getConnection(url, user, pass);  
 } catch (Exception e) {  
 //NEVER catch exceptions like this  
 }  
  
 }  
 public int authenticateUser(String user, String pass) {  
 User user;  
 try {  
 PreparedStatement ps = conn.prepareStatement(  
 "select readonly * from users where  
username = \"" + user + "\" AND " +  
 "password = \"" + pass + "\""  
 );  
 ResultSet result = ps.executeQuery();  
 while (result.next()) {  
 user = new User(result.getString(1),  
result.getString(2));  
 }  
 result.close();  
 ps.close();  
 } catch (SQLException sqle) {  
 //do stuff on fail  
 }  
 return user;  
 }  
}  
You can also try this code with Online Java Compiler
Run Code


Output

Frequently Asked Questions

What is the purpose of GWT?

A GWT development toolkit is used to create and improve sophisticated browser-based apps. Its objective is to make it possible to construct high-performance web apps productively without the developer having to be an expert in JavaScript, XMLHttpRequest, or browser quirks.

What is a GWT servlet?

GWT offers an RPC protocol based on Java Servlets to allow access to server-side resources. This approach generates effective client-side and server-side code to serialize objects across the network using deferred binding.

What is a Smart GWT?

A Java Server Framework is used to create high-quality online applications using the Smart GWT framework based on GWT and combines its rich UI components. Smart GWT has the broadest selection of UI components.

How do the GWT applications run in the end user's Web browser?

JavaScript code is used by all GWT applications to execute on the user's web browser. However, it's common to desire to make more than just a standalone client-side program. Your application will require sending queries to and getting updates from a web server.

How does the GWT compiler work?

The core of GWT is a Java source to JavaScript compiler that turns your running Java application into a comparable JavaScript application. The GWT compiler supports the bulk of the Java language. The GWT runtime library simulates a pertinent portion of the Java runtime library.

Conclusion

In this article, we have extensively discussed the GWT Database. We began with a brief introduction to the GWT Database, followed by its installation,  functionality, and example for a better understanding of the concept.

After reading about the GWT Database, refer to GWT DocumentationIntroduction to GWTGWT Tutorial15 Best Java FrameworksJavaFX, and Coding Ninjas for a deeper understanding of GWT development and other related topics.

Thank  You image
Live masterclass