Create Database-driven Resource Bundles with Java and MySQL | 2 | WebReference

Create Database-driven Resource Bundles with Java and MySQL | 2


Create Database-driven Resource Bundles with Java and MySQL [con't]

Creating a DBResourceBundle

As I explained in my Isolate Translatable Text in XML Files Using the Java 6 ResourceBundle.Control Class article, instantiation of a resource bundle is generally handled by the newBundle() method of the ResourceBundle.Control class. The advantage of overriding the newBundle() method is that it allows you to customize the default implementation of bundle instantiation to suit your specific needs. In this section, you will establish a connection to the database as you did previously so that you can populate ResourceBundles from your resource_bundle table. To fetch data from the table, you need to use the Connection object's createStatement(int resultSetType, int resultSetConcurrency) method. The resultSetType parameter can be ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE. You only need to scroll forward. The resultSetConcurrency can either be ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE. You can make yours read only, as you aren't modifying any data.

String Utilities

Working with databases requires you to pay special attention to strings. A common mistake is to insert a variable into a table like so:

Without performing some kind of filtering on the variables, there is nothing to escape single quotes from the names. Notice the extra quote in the following SQL statement:

You will use three specialized string utilities to optimize the generation of SQL: StrngBuilder, StringUtils, and StringEscapeUtils.

The StringBuilder is used to construct the query string, as it is designed for use as a replacement for StringBuffer in places where the string buffer is used by a single thread (as is generally the case). The Java docs recommended using this class instead of StringBuffer because it is faster under most implementations.

The StringUtils object is included with the MySQL Connector/J driver. Its static isNullOrEmpty() method replaces the cumbersome (somevalue == null || somevalue = "") test.

The StringEscapeUtils is what escapes special characters from query variables. It is a third-party utility included in the org.apache.commons.lang3 package, which is available from their site. Like MySQL Connector/J, the commons-lang-2.5.jar file must be copied to the your $JAVA_HOME/jre/lib/ext directory or added to your CLASSPATH environment variable.

Fetching the Properties

An if statement adds criteria to the query string based on the locale info provided. The completed SQL statement is then executed and stored in a Resultset object. You can then iterate through the results using the method and add the name/value pairs to the Properties. Finally, the connection is closed and a new DBResourceBundle is instantiated with the Properties object. Here is the complete code for the newBundle() method:

That's enough coding for one session. I believe that I hear a latte calling. In this information-packed article, you learned how to communicate between Java and a MySQL database using the MySQL Connector/J driver. You also created a test database and table, reviewed some specialized string utilities and overrode the newBundle() method to fetch locale-aware strings from a database. My Next article will walk through creating the DBResourceBundle class and creating a test program that will output text for different locales to illustrate what is happening behind the scenes. Last but not least, I'll discuss some ways to fine tune the loading of your ResourceBundles using the getCandidateLocales() method.



Original: July 8, 2010