Home arrow Java arrow Page 6 - Build a Servlet-Based Application that Executes SQL Statements Against a Database
JAVA

Build a Servlet-Based Application that Executes SQL Statements Against a Database


Dynamically interact with an SQL database using JSP and Servlets. Joel's article takes you through the processing of building your own "SQL Gateway"

Author Info:
By: Joel Murach
Rating: 4 stars4 stars4 stars4 stars4 stars / 16
September 21, 2003
TABLE OF CONTENTS:
  1. · Build a Servlet-Based Application that Executes SQL Statements Against a Database
  2. · Prerequisites
  3. · The User Interface
  4. · The Code for the JSP
  5. · The Code for the Servlet
  6. · The Code for the Utility Class
  7. · Conclusion and Related Links/Resources

print this article
SEARCH DEVARTICLES

Build a Servlet-Based Application that Executes SQL Statements Against a Database - The Code for the Utility Class
(Page 6 of 7 )

The code for the utility class named SQLUtil is shown below. This class contains a static method named getHtmlRows that is called by the servlet shown earlier. Like the SQLGatewayServlet, this class is stored in the murach.sql package.

package murach.sql;

import java.sql.*;

public class SQLUtil{

The getHtmlRows method accepts a ResultSet object and returns a String object that contains the HTML code for all of the column headings and rows in the result set. To build the information for that String object, the getHtmlRows declares a StringBuffer object named htmlRows and appends data to it as the method is executed. At the end of the method, the toString method is used to convert the StringBuffer object to the String object that is returned to the servlet.

    public static synchronized String
    getHtmlRows(ResultSet results) throws SQLException{
        StringBuffer htmlRows = new StringBuffer();
        ResultSetMetaData metaData = results.getMetaData();
        int columnCount = metaData.getColumnCount();

        htmlRows.append("<tr>");
        for (int i = 1; i <= columnCount; i++)
            htmlRows.append("<td><b>"
                + metaData.getColumnName(i) + "</td>");
        htmlRows.append("</tr>");

        while (results.next()){
            htmlRows.append("<tr>");
            for (int i = 1; i <= columnCount; i++)
                htmlRows.append("<td>"
                    + results.getString(i) + "</td>");
        }
        htmlRows.append("</tr>");
        return htmlRows.toString();
    }
}

To get the column headings that are returned, the getHtmlRows method uses the getMetaData method of the ResultSet object to create a ResultSetMetaData object. This type of object contains information about the result set including the number of columns and the names of the columns. To get that information, the getHtmlRows method uses the getHtmlRows and getColumnName methods of the ResultSetMetaData object.

To get the data from the result set, the getHtmlRows method uses a for loop within a while loop to get the data for each column in each row. Within these loops, the code uses the getString method of the result set to get the data for each field. That converts the data to a string no matter what data type the field is.

Please note that this method is declared with the synchronized keyword. This prevents two or more threads of a servlet from executing this method at the same time.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials