Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Build a Servlet-based Application That Executes SQL Statements Via the Web : Page 2

Learn how to use a Java servlet, a JavaServer Page, and a static Java class to create a SQL gateway application that enables users to interactively execute SQL statements against a database running on a server. This application allows you to work with a database as you're developing an application.


advertisement
The Code for the JSP
The JSP code starts with a scriptlet, which 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 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 target=_blank>
<form>

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

The table near the end of the JSP displays a message string containing 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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date