slu

Søren's Blog

Random Ramblings

Previous Entry Share Next Entry
HSQLDB Part 5 - Using a Bean
computer
slu
I my previous entry I created a JSP Document, which is the way I prefer to write JSP's. However there were still a little too much coding going on in the JSP, so I've take the main part of the code and put it in a class. The class is then used from the JSP Document with a jsp:useBean-tag. I'm still using the core tags of JSTL to render the page, but have moved all database-related code to the Bean.

Here's first the new and improved JSP Document:

<?xml version="1.0" encoding="iso-8859-1"?>

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"
          xmlns:c="http://java.sun.com/jstl/core">

  <jsp:directive.page contentType="text/html; charset=iso-8859-1" />

  <c:set var="title" value="Database Test" />

  <jsp:useBean id="bookmarks" scope="session" class="soren.Bookmarks" />

  <c:set target="${bookmarks}" property="database">
    <jsp:scriptlet>
      out.print(application.getRealPath("/database/demobase"));
    </jsp:scriptlet>
  </c:set>
	  
  <html>
    <head>
      <title><c:out value="${title}" /></title>
    </head>

    <body>

      <h1><c:out value="${title}" /></h1>
  
      <h2>Bookmarks</h2>
    
      <ul>
        <c:forEach var="row" items="${bookmarks.links}">
          <li>
            <c:out value="&lt;a href='${row.value}'&gt;${row.key}&lt;/a&gt;" 
               escapeXml="false" />
          </li>
        </c:forEach>
      </ul>

    </body> 
  </html>

</jsp:root>


This is actually what I would consider production quality code, except for the scriptlet code that sets the database property in the bookmarks Bean. This should be placed in a properties file or defined with JNDI in the servlet container.

Next create the Bookmarks.java file and compile it. Place the resulting Bookmarks.class file in a directory called soren in your WEB-INF/classes directory. This is still not production quality code, but it's getting closer.
package soren;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

public class Bookmarks {    
    String sql = "SELECT title, url FROM Bookmarks ORDER BY title";
    String database = "";
    HashMap links = null;

    public Bookmarks() {
	links = new HashMap();
    }
    
    public String getDatabase() {
	return database;
    }

    public void setDatabase(String database) {
	this.database = database;
    }       
    
    public synchronized Map getLinks() {
	Connection connection = null;
	Statement statement = null;
	ResultSet resultSet = null;
	    
	try {
	    Class.forName("org.hsqldb.jdbcDriver");
	    connection = DriverManager.getConnection("jdbc:hsqldb:" + 
						     database, "sa", "");
	    
	    statement = connection.createStatement();
	    resultSet = statement.executeQuery(sql);
	    
	    while (resultSet.next()) {
		links.put(resultSet.getString("title"), 
			  resultSet.getString("url"));
	    }       	    
	}
	catch (Exception e) {
	    e.printStackTrace();
	}
	finally {
	    try {
		resultSet.close();
		statement.close();
		connection.close();
	    }
	    catch (Exception e) {
		e.printStackTrace();
	    }
	}
	
	return links;
    }    
}

  • 1

Ready for IBatis

(Anonymous)
Hi Søren

Great series of articles. I am ready for the IBatis part d:-)

What are your experiences with IBatis. I can see on their site that they claim to have great Stored Procedure support. Have you used that?

/Hans
Copenhagen d:-)

  • 1
?

Log in

No account? Create an account