### User initiated connect to database.

No. 48

An application writing records to a database server

Our aim is enhancing our first GUI prototype being described in Figure 946, “A simple GUI to insert data into a database server. ”. The application shall start being disconnected from the database server. Prior to entering data the user shall be guided to open a connection. The following video illustrates the desired user interface:

Figure 948. A GUI frontend for adding personal data to a server.

Mind the following topics:

1. There are potential database related problems:

1. JDBC driver registration may fail e.g due to a missing library.

2. Establishing a connection may fail e.g. due to wrong connection parameters.

3. Inserting data may fail e.g. due to a missing table definition.

Separate end-user and expert related hints:

• Expert messages shall be handled by log4j.

• End user error messages shall just convey a basic idea containing a reference to the log file i.e. Could not open database, see log file for details.

2. Separate your GUI and database components: Basically the GUI part shall have no reference to SQL / JDBC and your database part shall have no Vaadin GUI related references.

### Tip

• The database layer may send both error and informative messages to your GUI to be presented in a end user friendly manner.

• Using your class PersistenceHandler from Database layer handling requires two steps:

1. Executing mvn install in your project Database layer handling . This will create libraries to be installed below ~/.m2.

2. Importing these libraries as Maven dependencies into your GUI project's pom.xml:

...
<dependencies>

<dependency>
<groupId>de.hdm_stuttgart.mi.persistence</groupId>
<artifactId>persistencehandler</artifactId>
<version>3</version>
</dependency>
...

A:

### Caution

Caveat: Calling mvn compile in the dependent project P/Sda1/PersistenceHandler/Statement is a prerequisite which installs required libraries below ~/.m2.

Our implementation uses class de.hdm_stuttgart.mi.PersistenceHandler for handling database communication handling. Our GUI needs to visualize the two different disconnected and connected states. In disconnected state the whole input pane for entering datasets and clicking the Insert button is locked. So the user is forced to open a database connection.

Notice the detach listener:

public class LoginUI extends UI implements DetachListener {
...

@Override
public void detach(DetachEvent event) {
if (persistenceHandler.isConnected()) {
persistenceHandler.toggleConnectionState();
}
}

This allows for closing JDBC connections whenever the application server chooses to evict our application instance i.e. due to timeouts.