How to run the SQL command directly in the browser

xiaoxiao2021-03-06  58

How to run directly in the SQL command within the browser Author: Joel Murach; cactus studio Translation dispatch time: 2003.08.13 14:39:58 This article demonstrates how to use a Java Servlet, a JSP page and a static Java class to construct a SQL Gateway application. With this app, you can execute the SQL command directly, and the browser will submit the SQL command to the database system on the remote server, then return the result. If you are using the database provided by ISP (Internet Service Provider), you may be familiar with the concept of SQL gateway app. Some ISPs will provide an HTML page for an operational database, just like the gateway provided herein. If the ISP does not provide this interface, you can load this article to the server, which is convenient to access the database on the ISP server. SQL gateway applications can not only be used for development processes, but also directly provide end users who are more familiar with the system. Of course, allowing end users to run SQL commands directly on the database to bring some security hazards, should be careful. This article requires readers to have a certain basic knowledge of Java, Servlet, JSP, and databases. If you want to run this article, there is a servlet / JSP server and database server. In the following description, we want to use Tomcat 4.0 and mysql, but it should also run in other JSP / Servlet containers; if you want to use other databases other than mysql, just provide an appropriate driver, Then modify the database connection string. First, the user interface Figure 1 is the user interface of the SQL gateway of this article. In this interface, the SQL gateway has executed a SQL command and returns the result.

Figure 1: User interface of the SQL gateway

As can be seen from Figure 1, a message at the bottom of the page shows the number of rows affected by the recently executed SQL command. If the SQL command is a SELECT statement, when the SELECT statement is executed, the page will be displayed with the HTML table with the HTML table, as shown in Figure 2.

Figure 2: HTML form Shows the query result set

Of course, if the SQL command is executed, the SQL gateway will return an exception information. Second, the design JSP page is in the JSP page, we first put into a scriptlet, its function is to extract two attributes from the session object:

<%

String SqlStateMent = (String) session.getattribute ("SqlStatement");

IF (SqlStatement == Null) SQLStatement = ""

String message = (string) session.getattribute ("message");

IF (message == null) message = "";

%>

The first attribute SQLStatement represents the SQL command string, and the second attribute message is a string containing the result information. If the value of these two attributes is null, they have not been set, and we set the SQLStatement and Message variables into empty strings. The JSP page also has an HTML form, the HTML form contains a textual (TextArea) and a "Execute" button.

sql command: