<%@page language="Java" isErrorPage="false" import=" java.sql.*, java.util.*, java.io.PrintWriter, java.io.StringWriter, javax.naming.Context, javax.naming.InitialContext, javax.naming.NamingException, javax.sql.DataSource " session="false" pageEncoding="utf-8" %> <%@ include file="htmlescape.jsp" %> <%! /** * Gets a JDBC connection. This implementation uses JNDI to obtain a * connection. Feel free to substitute your own. */ Connection getConnection() throws SQLException, javax.naming.NamingException { Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:/comp/env/jdbc/myDataSource"); if(null == ds) throw new NamingException("Cannot obtain DataSource"); return ds.getConnection(); } %> <% String query = request.getParameter("q"); String title = request.getParameter("title"); boolean showQuery = (!"false".equals(request.getParameter("showQuery"))); boolean preformattedResults = ("true".equalsIgnoreCase(request.getParameter("preformattedResults"))); boolean escapeHtml = (!"false".equalsIgnoreCase(request.getParameter("escapeHtml"))); boolean tallFormat = ("true".equalsIgnoreCase(request.getParameter("tallFormat"))); boolean showAllOptions = ("true".equalsIgnoreCase(request.getParameter("showAllOptions"))); boolean hasTitle = (null != title && 0 < title.trim().length()); boolean hasQuery = (null != query && 0 < query.trim().length()); if(!hasQuery) query = ""; else query = query.trim(); if(hasTitle) title = htmlescape(title); %> <% if (hasTitle) { %> <%= title %> <% } else { %> Ad Hoc Query<%= hasQuery ? " - Results" : "" %> <% } %>

<%= hasTitle ? title : "Ad Hoc Query Tool" %>

<% if(hasQuery) { query = query.trim(); // We've got a query Connection conn = null; Statement s = null; ResultSet rs = null; long queryTime = -1; long renderTime = -1; try { conn = getConnection(); s = conn.createStatement(); queryTime = System.currentTimeMillis(); // Note that executeQuery cannot modify data rs = s.executeQuery(query); queryTime = System.currentTimeMillis() - queryTime; ResultSetMetaData rsmd = rs.getMetaData(); renderTime = System.currentTimeMillis(); %> <% if(!tallFormat) { %> <% for(int i=0; i <% } } %> <% int rowCount = 0; boolean even = true; while(rs.next()) { if(tallFormat) { %> "> <% } else { %> "> <% } for(int i=0; i "> <% } %> <% if(tallFormat) { %> <% } } if(!tallFormat) { %> <% } even = !even; ++rowCount; } renderTime = System.currentTimeMillis() - renderTime; %>
<%= (hasTitle ? "" : "Query results") %>
<%= rsmd.getColumnLabel(i + 1) %>
Row <%= rowCount %>
<%= rsmd.getColumnLabel(1 + i) %> <% if(preformattedResults) { %>
<% } %><%= (escapeHtml ? htmlescape(value) : value) %><% if(preformattedResults) { %>
<% } %>

Query produced <%= rowCount %> results in <%= queryTime %>ms. Results were rendered in <%= renderTime %>ms.

<% if(hasQuery) query = htmlescape(query); if(showQuery) { %>

Query:

<%= query %>
<% } } catch (SQLException sqle) { %>

There was an error executing your query: (See page source for details)

<%= sqle.getMessage() %>

<% } finally { if(null != rs) try { rs.close(); } catch (SQLException sqle) { getServletContext().log("Cannot close ResultSet", sqle); } if(null != s) try { s.close(); } catch (SQLException sqle) { getServletContext().log("Cannot close Statement", sqle); } if(null != conn) try { conn.close(); } catch (SQLException sqle) { getServletContext().log("Cannot close Connection", sqle); } } %> <% } %> <% if(showQuery) { %>

Enter a SQL query:
(May not be a data manipulation query; SELECTs only)

<% if(showAllOptions) { %> <%-- always checked --%>
checked="checked"<% } %> />
value="<%= htmlescape(title) %>"<% } %> />
checked="checked"<% } %> />
checked="checked"<% } %> />
checked="checked"<% } %> />
<% } else { if(tallFormat) { %> <% } } %>

<% } %>

Creative Commons License Valid XHTML 1.0! Valid CSS!

This work is licensed under a Creative Commons License.