Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Jump into Java microframeworks, Part 3: Spark

Matthew Tyson | Jan. 8, 2016
An extra lightweight, flexible, and scalable architecture for single-page web apps.

Listing 17 is the JdbcDAO implementation of loadPeople(). Remember that we'll also need to add the loadPeople() to the DAO interface.

Listing 17. JdbcDAO.loadPeople()

	public List<Person> loadPeople() {

    	QueryRunner run = new QueryRunner( dataSource );

		try	{

			ResultSetHandler<List<Person>> h = new BeanListHandler<Person>(Person.class)); // 1

			List<Person> persons = run.query("SELECT * FROM Person", h);

			return persons;

		} catch(SQLException sqle) {

		    throw new RuntimeException("Problem updating", sqle);



JdbcDAO.loadPeople leverages DBUtils again, this time to issue the query, and also to convert the SQL resultset into a List of Persons. The SQL conversion is handled by passing in the ResultSetHandler to the query() method. You can see the definition for the ResultSetHandler in the line commented with the number 1. Also note the use of generics and the argument to specify the type of results we want back. DBUtils provides several useful handlers like this one.

At this point, we can test out the loadPerson endpoint in Postman, by sending a GET to /people. What you'll find is that it almost works: we get back our rows, but they only have the IDs, no first and last name fields. Figure 3 shows Postman returning a JSON array with only the ID fields.

javamicroframeworksp3 fig3 
Figure 3. Postman get people, with only IDs. Credit: Matthew Tyson

For the Person names to be properly listed, we have to convert the SQL-style database fields (first_name) into the JavaBeans style (firstName). This is exactly analogous to when we configured Boon to convert from underscore to camel-case on the front-end. Fortunately, DBUtils makes the conversion easy; just swap line 1 in Listing 17 for what's shown in Listing 18. The DBUtils GenerousBeanProcessor accepts underscore-separated names.

Listing 18. JdbcDAO.loadPeople()

	ResultSetHandler<List<Person>> h = new BeanListHandler<Person>(Person.class,new BasicRowProcessor(new GenerousBeanProcessor())); // 1

The passed-in RowProcessor customizes our conversion, and GenerousBeanProcessor transforms our first_name to firstName. With these changes, a Postman test on /people should return the name fields we're looking for.

Figure 4 shows off the new response in Postman, which now shows a request after a couple of country music legends have been added to the database (note that this is old country). You'll see the people listed in the response as a JSON array, including the first and last name fields.

javamicroframeworksp3 fig4 
Figure 4. The new GET succeeds with all fields. Credit: Matthew Tyson

Completing the UI

We'll finish up the basic Person app by adding a few more UI elements to template.html, as shown in Listing 19.

Listing 19. JdbcDAO.loadPeople()

<!DOCTYPE html>

<html lang="en">


<meta charset="utf-8"></meta>


<script language="javascript" type="text/javascript" src="jquery-2.1.4.min.js"></script>

<script language="javascript" type="text/javascript" src="serializeObject.js"></script>

<script language="javascript" type="text/javascript" src="app.js"></script>



	<form id="personForm">

		First name: <input type="text" name="first_name"></input><br></br> Last

		name: <input type="text" name="last_name"></input><br></br>

		<button id="addPersonButton">Add</button>


	<div id="people"></div>




Previous Page  1  2  3  4  5  6  7  8  Next Page 

Sign up for CIO Asia eNewsletters.