home *** CD-ROM | disk | FTP | other *** search
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.SQLWarning;
- import java.sql.Statement;
- import java.util.Date;
- import java.util.Hashtable;
- import java.util.Properties;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServlet;
-
- public class DataAccessServlet extends HttpServlet {
- private String m_strUID = "";
- private String m_strPWD = "";
- private String m_strURL = "";
- private String m_strDriver = "";
- private String m_strSQLString = "";
- private String m_strActionType = "";
- private String m_strDatabase = "";
- private String m_strTableName = "";
- private Properties m_properties;
- private Connection m_connection;
- private ResultSet m_resultSet;
- private Statement m_statement;
- private ServletOutputStream m_outstream;
-
- private static boolean checkForWarning(SQLWarning var0) throws SQLException {
- boolean var1 = false;
- if (var0 != null) {
- System.err.println("\n *** SQL Warning ***\n");
-
- for(var1 = true; var0 != null; var0 = var0.getNextWarning()) {
- System.err.println("SQLState: " + ((SQLException)var0).getSQLState());
- System.err.println("Message: " + ((Throwable)var0).getMessage());
- System.err.println("Vendor: " + ((SQLException)var0).getErrorCode());
- System.err.println("");
- }
- }
-
- return var1;
- }
-
- private void connectToDatabase(String var1, String var2, Properties var3, ServletResponse var4) {
- try {
- this.trace("Trying the JDBC driver.");
- Class.forName(var2);
- this.trace("Getting a database connection.");
- this.m_connection = DriverManager.getConnection(var1, ((Hashtable)var3).get("UID").toString(), ((Hashtable)var3).get("PWD").toString());
- this.trace("Checking for connection warnings.");
- checkForWarning(this.m_connection.getWarnings());
- this.trace("Connected to database.");
- } catch (SQLException var6) {
- this.trace("An SQL Exception was thrown:\n" + var6);
- this.processSQLException(var6, var4);
- } catch (Exception var7) {
- ((Throwable)var7).printStackTrace();
- }
- }
-
- public void destroy() {
- this.trace("Destroying the servlet.");
-
- try {
- this.m_connection.commit();
- this.m_resultSet.close();
- this.m_resultSet = null;
- this.m_statement.close();
- this.m_statement = null;
- this.m_connection.close();
- this.m_connection = null;
- } catch (SQLException var2) {
- SQLException var1 = var2;
- System.err.println("\n*** SQLException caught ***\n");
-
- while(var1 != null) {
- System.err.println("SQLState: " + var1.getSQLState());
- System.err.println("Message: " + ((Throwable)var1).getMessage());
- System.err.println("Vendor: " + var1.getErrorCode());
- var1 = var1.getNextException();
- System.err.println("");
- }
-
- }
- }
-
- private void executeTheQuery(String var1, ServletResponse var2) {
- try {
- this.trace("Executing a query.");
- this.m_statement = this.m_connection.createStatement();
- this.m_resultSet = this.m_statement.executeQuery(var1);
- } catch (SQLException var4) {
- this.trace("An SQL Exception was thrown:\n" + var4);
- this.processSQLException(var4, var2);
- }
- }
-
- private void executeTheUpdate(String var1, ServletResponse var2) {
- try {
- this.trace("Executing an update.");
- this.m_statement = this.m_connection.createStatement();
- this.m_statement.executeUpdate(var1);
- } catch (SQLException var4) {
- this.trace("An SQL Exception was thrown:\n" + var4);
- this.processSQLException(var4, var2);
- }
- }
-
- public String getServletInfo() {
- return "a data access servlet by Sheldon Bradley Wosnick which demonstrates server-side JDBC programming with servlets";
- }
-
- public void init(ServletConfig var1) {
- this.trace("Calling overridden init() method now.");
-
- try {
- super.init(var1);
- } catch (ServletException var3) {
- System.err.println("ServletException: " + ((Throwable)var3).getMessage());
- }
- }
-
- private void outResultSet(ServletResponse var1, String var2) throws SQLException {
- try {
- this.trace("Getting an output stream for the HTML response page.");
- this.m_outstream = var1.getOutputStream();
- var1.setContentType("text/html");
- this.m_outstream.println("<html>");
- this.m_outstream.println("<head><title>A Data Access Servlet using Pure JDBC</title></head>");
-
- try {
- this.trace("Preparing some connection information.");
- ResultSetMetaData var3 = this.m_resultSet.getMetaData();
- DatabaseMetaData var4 = this.m_connection.getMetaData();
- int var5 = var3.getColumnCount();
- this.m_outstream.println("JDBC DATABASE CONNECTION DETAILS:");
- this.m_outstream.println("<BR>");
- this.m_outstream.println("<BR>Current date and time: " + (new Date()).toString());
- this.m_outstream.println("<BR>Connection URL: " + var4.getURL());
- this.m_outstream.println("<BR>Driver: " + var4.getDriverName());
- this.m_outstream.println("<BR>Version: " + var4.getDriverVersion());
- this.m_outstream.println("<BR>User Name: " + var4.getUserName());
- this.m_outstream.println("<BR>Driver Major Version: " + var4.getDriverMajorVersion());
- this.m_outstream.println("<BR>Driver Minor Version: " + var4.getDriverMinorVersion());
- this.m_outstream.println("<BR>Database Product Name: " + var4.getDatabaseProductName());
- this.m_outstream.println("<BR>Database Product Version: " + var4.getDatabaseProductVersion());
- this.m_outstream.println("<BR>SQL query or update command processed by servlet: " + this.m_strSQLString);
- this.m_outstream.println("<BR><BR>");
- this.trace("Preparing an HTML table.");
- this.m_outstream.println("<TABLE>");
- this.m_outstream.println("<TABLE BORDER=1>");
- String var6 = "TABLE: " + this.m_strTableName + " ***** ACTION TAKEN: " + var2;
- this.m_outstream.println("<CAPTION>" + var6 + "</CAPTION>");
- this.trace("Creating the column names");
-
- for(int var7 = 1; var7 <= var5; ++var7) {
- this.m_outstream.println("<TH>" + var3.getColumnLabel(var7));
- }
-
- boolean var8 = this.m_resultSet.next();
- this.trace("Filling in the table elements.");
-
- while(var8) {
- this.m_outstream.println("<TR>");
-
- for(int var11 = 1; var11 <= var5; ++var11) {
- if (!this.m_strTableName.toUpperCase().equals("TESTTT") || var11 != 1 && var11 != 3) {
- this.m_outstream.println("<TD>" + this.m_resultSet.getString(var11));
- } else {
- this.m_outstream.println("<TD>" + new String(this.m_resultSet.getBytes(var11), "ibm819"));
- }
- }
-
- var8 = this.m_resultSet.next();
- }
-
- this.m_outstream.println("</FONT>");
- this.m_outstream.println("</TABLE>");
- this.trace("Finished preparing the HTML table.");
- } catch (SQLException var9) {
- this.trace("An SQL Exception was thrown:\n" + var9);
- this.processSQLException(var9, var1);
- }
-
- this.m_outstream.println("</body></html>");
- this.trace("Finished with the output stream.");
- this.m_outstream.flush();
- this.m_outstream.close();
- this.trace("Flushed and closed the output stream.");
- } catch (IOException var10) {
- System.err.println("\n*** IOException caught ***\n");
- System.err.println("IOException: " + ((Throwable)var10).toString());
- }
- }
-
- private void processSQLException(SQLException var1, ServletResponse var2) {
- try {
- this.trace("Processing an SQL Exception");
- this.m_outstream = var2.getOutputStream();
- var2.setContentType("text/html");
- this.m_outstream.println("<html>");
- this.m_outstream.println("<head><title>Servlet threw an SQLException</title></head>");
- this.m_outstream.println("<body BGCOLOR=#000000 TEXT=#C0C0C0>");
- this.m_outstream.println("<P>*** An SQLException was caught ***</P>");
-
- while(var1 != null) {
- this.m_outstream.println("<P>SQLState: " + var1.getSQLState() + "</P>");
- this.m_outstream.println("<P>Message: " + ((Throwable)var1).getMessage() + "</P>");
- this.m_outstream.println("<P>Vendor: " + var1.getErrorCode() + "</P>");
- var1 = var1.getNextException();
- this.m_outstream.flush();
- this.m_outstream.close();
- }
-
- } catch (IOException var4) {
- System.err.println("\n*** IOException caught ***\n");
- System.err.println("IOException: " + ((Throwable)var4).toString());
- }
- }
-
- public void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException {
- this.trace("Calling the service() method now.");
- this.m_properties = new Properties();
- this.trace("Getting request parameters.");
- this.m_strUID = var1.getParameter("UID");
- this.m_strPWD = var1.getParameter("password");
- this.m_strDriver = var1.getParameter("driver");
- this.m_strTableName = var1.getParameter("table");
- this.m_strSQLString = var1.getParameter("SQL");
- this.m_strDatabase = var1.getParameter("database");
- if (!this.m_strDriver.substring(0, 16).equals("org.gjt.mm.mysql") && !this.m_strDriver.substring(0, 22).equals("com.imaginary.sql.msql") && !this.m_strDriver.substring(0, 20).equals("COM.ibm.db2.jdbc.net")) {
- this.m_strURL = "jdbc:" + var1.getParameter("URL") + ":" + this.m_strDatabase;
- } else {
- this.m_strURL = "jdbc:" + var1.getParameter("URL") + "/" + this.m_strDatabase;
- }
-
- this.m_strActionType = var1.getParameter("action");
- this.m_properties.put("UID", this.m_strUID);
- this.m_properties.put("PWD", this.m_strPWD);
- this.trace("User ID: " + this.m_strUID);
- this.trace("Driver: " + this.m_strDriver);
- this.trace("URL: " + this.m_strURL);
- this.trace("Table Name = " + this.m_strTableName);
- this.trace("SQL String = " + this.m_strSQLString);
- this.trace("Action Type = " + this.m_strActionType);
- if (this.m_connection == null) {
- this.connectToDatabase(this.m_strURL, this.m_strDriver, this.m_properties, var2);
- }
-
- if (this.m_strActionType.equals("query")) {
- this.executeTheQuery(this.m_strSQLString, var2);
-
- try {
- this.outResultSet(var2, "User-defined Query");
- } catch (SQLException var7) {
- this.trace("An SQL Exception was thrown.");
- this.processSQLException(var7, var2);
- }
- } else if (this.m_strActionType.equals("update")) {
- try {
- this.trace("Querying the database for table " + this.m_strTableName + " before any updates.");
- this.m_statement = this.m_connection.createStatement();
- this.m_resultSet = this.m_statement.executeQuery("select * from " + this.m_strTableName);
- this.outResultSet(var2, "Query Before the Update");
- } catch (SQLException var6) {
- this.trace("An SQL Exception was thrown:\n" + var6);
- this.processSQLException(var6, var2);
- }
-
- this.executeTheUpdate(this.m_strSQLString, var2);
-
- try {
- this.trace("Requerying the database for table " + this.m_strTableName + " after any updates.");
- this.m_statement = this.m_connection.createStatement();
- this.m_resultSet = this.m_statement.executeQuery("select * from " + this.m_strTableName);
- this.outResultSet(var2, "Query after the Update");
- } catch (SQLException var5) {
- this.trace("An SQL Exception was thrown:\n" + var5);
- this.processSQLException(var5, var2);
- }
- } else {
- System.err.println("ERROR >> For processing purposes, you must explicitly pass 'update' or 'query'");
- }
-
- try {
- this.m_connection.commit();
- this.m_resultSet.close();
- this.m_resultSet = null;
- this.m_statement.close();
- this.m_statement = null;
- this.m_connection.close();
- this.m_connection = null;
- } catch (SQLException var4) {
- this.trace("An SQL Exception was thrown:\n" + var4);
- this.processSQLException(var4, var2);
- }
- }
-
- public void trace(String var1) {
- System.out.println("DEBUG TRACE: " + var1);
- }
- }
-