Java
  Home arrow Java arrow Page 4 - Build a Servlet-Based Application that Exe...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Build a Servlet-Based Application that Executes SQL Statements Against a Database
By: Joel Murach
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 16
    2003-09-21

    Table of Contents:
  • Build a Servlet-Based Application that Executes SQL Statements Against a Database
  • Prerequisites
  • The User Interface
  • The Code for the JSP
  • The Code for the Servlet
  • The Code for the Utility Class
  • Conclusion and Related Links/Resources

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Java Articles
    More By Joel Murach


     

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT