import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.URL;
import java.sql.*;
public class SQLservlet extends HttpServlet
{ public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{ res.setContentType("text/html");
PrintWriter out=res.getWriter();
String docType=
"\n";
out.println(docType+
"\n"+
"
SQL servlet\n"+
"\n"+
""+
""+
"
"+
""+
""+
""+
" "+
" "+
" | "+
""+
"Database"+
"Administration | "+
"
"+
"
"+
""+
""+
" | "+
" | "+
" | "+
"
"+
""+
" | "+
"Query Statement | "+
" | "+
"
"+
""+
" | "+
""+
""+
" | "+
" | "+
"
"+
"
"+
"");
if ( req.getParameter("sqlstatement") != null )
out.print(execute(req.getParameter("sqlstatement")));
}
public static String execute (String SQLstr)
{
String rs_htm = "";
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=
DriverManager.getConnection("jdbc:oracle:thin:@arabica.labunix.uqam.ca:1521:o8db",
"your code", "your password");
// Get the DatabaseMetaData object and display
// some information about the connection
DatabaseMetaData dma = con.getMetaData ();
// Create a Statement object so we can submit
// SQL statements to the driver
Statement stmt = con.createStatement ();
try
{ if (stmt.execute(SQLstr))
{
// Result set available
ResultSet rs = stmt.getResultSet();
rs_htm = dispResultSet (rs);
rs.close();
} // if
} catch (SQLException e)
{
rs_htm = e.getMessage ();
} // try-catch
// Close the statement
stmt.close();
// Close the connection
con.close();
}
catch (Exception ex)
{ rs_htm = ex.getMessage ();
}
return rs_htm;
}
//-------------------------------------------------------------------
// dispResultSet
// Displays all columns and rows in the given result set
//-------------------------------------------------------------------
private static String dispResultSet (ResultSet rs) throws SQLException
{
ResultSetMetaData rsmd = rs.getMetaData ();
int numCols = rsmd.getColumnCount ();
String rs_htm=" | ";
for (int i=1; i<=numCols; i++)
rs_htm += " "+rsmd.getColumnLabel(i)+" | ";
int i=1;
while (rs.next ())
{ rs_htm +=""+ i++ +" | "+ dispRow (rs, numCols)+"
";
} // while
rs_htm += "
";
return rs_htm;
}
/**
* Displays the current row of data.
*/
private static String dispRow (ResultSet rs, int numCols) throws SQLException
{
String r="";
for (int i=1; i<=numCols; i++) {
r += ""+rs.getString(i)+" | ";
}
return r;
} // method dispRow
}