<%@ page import="java.io.StringWriter" %>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>Welcome to the Derby Tutorial Fortune Server!</title>
</head>
<body bgcolor=white link="#0066FF" vlink="#0066FF" alink="#0066FF">

<table border="0">
<tr>
<td align=center>
</td>
<td>
<h1 align=center><font color="#0066FF">Welcome to the Derby Tutorial Fortune Server!</font></h1>
<h4 align=center>
Powered by
<a href="http://incubator.apache.org/derby">Derby</a>,
<a href="http://jakarta.apache.org/regexp">Jakarta Regexp</a>,
<a href="http://jakarta.apache.org/commons/math">Jakarta Mathematics Library</a>,
and
<a href="http://jakarta.apache.org/tomcat">Jakarta Tomcat</a>.
</h4>

<p>
The Derby Tutorial Fortune Server stores fortunes in a Derby database.
FortuneQuery.jsp lets you execute <em>ad hoc</em> SQL statements.
</p>

</td>
</tr>
</table>

<%
String varJdbcUrl = request.getParameter("jdbcurl");
if (varJdbcUrl == null) {
    varJdbcUrl = "";
}
varJdbcUrl = java.net.URLDecoder.decode(varJdbcUrl).trim();
String varSql     = request.getParameter("sql");
if (varSql == null) {
    varSql = "";
}
varSql = java.net.URLDecoder.decode(varSql).trim();

%>

<form name="sqlaccess" action="FortuneQuery.jsp">
<table>
<tr>
<td colspan=2> &nbsp;</td>
</tr>
<tr>
<td> JDBC URL:</td>
<td> <textarea cols="64" rows="1" name="jdbcurl">
<%=varJdbcUrl%>
</textarea> </td>
</tr>

<tr>
<td valign=top> SQL:</td>
<td> <textarea cols="64" rows="3" name="sql">
<%=varSql%>
</textarea> </td> </tr>
<tr> <td align=center> <input value="Execute SQL" type="submit"> </td> </tr>
</table>
</form>
<p>


<%


if (varJdbcUrl.length() != 0) {
    try {
        Connection varJdbcConnection = DriverManager.getConnection(varJdbcUrl);
        Statement varJdbcStatement = null;
        try {
            varJdbcStatement = varJdbcConnection.createStatement();
            if (varSql.length() != 0) {
                if (varJdbcStatement.execute(varSql)) {
                    java.sql.ResultSet varJdbcResultSet = varJdbcStatement.getResultSet();
                    try {
                        java.sql.ResultSetMetaData varJdbcMetaData = varJdbcResultSet.getMetaData();
                        int varColumnCount = varJdbcMetaData.getColumnCount();
                        %>
                            <table>
                            <tr>
                        <%
                        for (int i = 1; i <= varColumnCount; i++) {
                            %>
                            <th bgcolor="#0066FF" class="tab"><font color="#FFFFFF"><%= varJdbcMetaData.getColumnLabel(i) %> </font></th>
                           <%
                        }
                        %>
                            </tr>
                        <%
                        int varReturnedRows=0;
                        while (varJdbcResultSet.next()) {
                            varReturnedRows++;
                            %>
                                <tr>
                            <%
                            for (int i = 1; i <= varColumnCount; i++) {
                                %>
                                    <td valign=top><pre><%= varJdbcResultSet.getString(i) %> </pre></td>
                                <%
                            }
                            %>
                                </tr>
                            <%
                        }
                        %>
                            </table>
                            <font size="-1">
                        <%
                        if (varReturnedRows == 1) {
                            %>
                                1 row
                            <%
                        } else {
                            %>
                                <%=varReturnedRows %> rows
                            <%
                        }
                        %>
                            returned.</font>
                        <%
                    } finally {
                        varJdbcResultSet.close();
                    }
                } else {
                    %>
                        <font size="-1">
                    <%
                    int varProcessedRows = varJdbcStatement.getUpdateCount();
                    if (varProcessedRows == 1) {
                        %>
                            1 row
                        <%
                    } else {
                        %>
                            <%=varProcessedRows %> rows
                        <%
                    }
                    %>
                        processed.</font>
                    <%

                }
            }
        } catch (Exception varException) {
            StringWriter varStackBuffer = new StringWriter();
            PrintWriter varPrint = new PrintWriter(varStackBuffer);
            varException.printStackTrace(varPrint);
            %>
            <p><b>An exception was caught executing a SQL statement.</b>
            <p><pre> <%=varStackBuffer %> </pre>
            <%
        } finally {
            if (varJdbcStatement != null) {
                try {
                    varJdbcStatement.close();
                } catch (Throwable e) {}
            }
            try {
                varJdbcConnection.close();
            } catch (Throwable e) {}
        }
    } catch (Exception varException) {
        StringWriter varStackBuffer = new StringWriter();
        PrintWriter varPrint = new PrintWriter(varStackBuffer);
        varException.printStackTrace(varPrint);
        %>
        <p><b>An Exception was caught while connecting to the database.</b>
        <p><pre> <%=varStackBuffer %> </pre>
        <%
    }
}
%>

<SCRIPT LANGUAGE="JavaScript">
document.sqlaccess.sql.focus()
</SCRIPT>


</body>
</html>
