Tags

, , , , , , ,

Now that everythings working flawlessly on the network between the MySQL server and the prototype front-end, I wanted to start developing a proper commercial-grade application for it using Java.

Database Connections
Things work a little differently with Java than with Python, but we still require a library to enable a program to comunicate with a database server. Here the library/class is a MySQL ‘connector’ (mysql-connector-java-5.1.33-bin.jar).
With this installed, a new database connection can be created, and it’s listed under the Services window on the left.

netbeans-db-connection

UI Form and Elements
From here, the database properties are available. So far, so good. I had a database connection to work with. The next stage is to create a Java UI for this connection, but before getting to that, NetBeans’ GUI designer won’t load properly on some Debian-based systems, because it has problems finding a component for this. Use the following to define its location when starting NetBeans in the command line:
$netbeans -cp:a /usr/share/java/xercesImpl.jar

Now the quickest way of getting a basic GUI is by creating a Java Desktop Application project, and selecting the Database Application shell in the dialogue.

netbeans-create-application-1

Here I checked the ‘Use Dedicated Folder for Storing Libraries‘ option, just in case it was required for portability. After this, I specified the database connection and the table.

NetBeans is supposed to display a ready-made GUI form to work with, but initially it didn’t. What I encountered was an error message stating that a load of components were missing. Since I’m still fairly new to Java and had been hacking the NetBeans installation over the last month, there was a good chance I broke something fundamental. I removed the IDE and all its configuration files before re-installing it.

The same problem occurred, so it was definitely a dependecy problem. I went through the package manager, downloading all the components that NetBeans might have been trying to load. When that didn’t work, I finally resorted to actually reading the error messages.

Now, to cut a longer story short, what should have been a simple tutorial program required ‘javax.persistence.Exception‘, which apparently was missing from the available libraries, and it was something native to Java EE (the ‘enterprise’ edition of Java).
What I needed was included with an open source alternative to Java EE called ‘GlassFish’. Install this in the package manager, then select the following in NetBeans plugin installer:
* Java Persistence
* GlassFish
* EJB and EAR

These weren’t listed in the plugin catalogue before, for some reason. Connecting to the plugin server was also problematic, and various solutions have been suggested on the web. In my case, the plugin server itself was actually down for several hours.

When the plugins were finally installed and the IDE restarted, the form loaded, compiled and ran perfectly. I made a few changes here.

netbeans-prototype-ui

netbeans-prototype-about

Portability Settings
Just a few other things before wrapping up. The compile libraries under the Project Properties should include the mysql-connector-java and MySQL JDBC Driver libraries, and probably the TopLink library also.

netbeans-compile-libraries

The source tree might look a bit complicated, but we can forget about the files under Libraries, META-INF and META-INF.services from this point, since I’ve already demonstrated a form that interacts with the server. Everything that needs to be modified to rebuild the UI exists in just two of the .java source files.

Network Test
Now for the real test of whether the application can do the same on another machine. The persistence.xml appears to be where the MySQL server address is defined for the whole project, so I replaced ‘localhost‘ here with the server’s known address before compiling the source – this could also be the static address of an Internet-facing MySQL server.

netbeans-persistence-file

Cleaning and building the project (in other words compiling it) produces the /dist direcory, which contains the application and its dependencies. This is what I copied over to the other host, and it ran perfectly.
The screenshot below shows the client querying the MySQL server over the network.

netbeans-mysql-remote

Advertisements