Home arrow Java arrow Page 4 - 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 JSP
(Page 4 of 7 )

The code for the JSP starts with a scriptlet that contains Java code that retrieves two attributes from the session object:

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<%
String sqlStatement =
(String) session.getAttribute("sqlStatement");
if (sqlStatement == null)
   sqlStatement = "";
String message =
(String) session.getAttribute("message");
if (message == null)
   message = "";
%>

The first attribute is the string that contains the SQL statement, and the second attribute is the string that contains the result message. If these attributes contain null values, they haven't been set yet so this code sets the sqlStatement and message variables to empty strings.

This JSP also contains an HTML form that contains a text area and a submit button:

<form action="../servlet/murach.sql.SQLGatewayServlet" method="post">
   <b>SQL statement:</b><br>
   <textarea name="sqlStatement" cols=60 rows=8>
   <%= sqlStatement %>
   <textarea><br>
   <br>
   <input type="submit" value="Execute">
<form>

Here, the text area allows the user to enter the SQL statement. This code creates a text area that's approximately 60 characters wide and 8 lines tall. Within this area, the sqlStatement variable is displayed, which is empty the first time this JSP is run. Then, when the user clicks the submit button, this JSP calls the SQLGatewayServlet that's described later in this article.

The table near the end of the JSP displays the message string that contains the result of the SQL statement:

<b>SQL result:</b><br>
<table cellpadding="5" border="1">
   <%= message %>
<table>

Since this message contains the rows and columns for an HTML table, it's coded within the Table tags.


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