home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 November / PCONLINE_11_99.ISO / filesbbs / OS2 / APCHSSL2.ZIP / OS2HTTPD / servlets / MySQLServlet.java < prev    next >
Encoding:
Java Source  |  1999-05-18  |  8.2 KB  |  234 lines

  1.  
  2. import java.net.URL;
  3. import java.util.*;
  4. import java.sql.*;
  5. import java.io.*;
  6. import javax.servlet.*;
  7. import javax.servlet.http.*;
  8. public class MySQLServlet extends HttpServlet {
  9.     /**
  10.      * The database connection.
  11.      */
  12.     private Connection m_connection = null;
  13.  
  14.     /**
  15.      * Result set for a given query.
  16.      */
  17.     private ResultSet m_resultSet = null;
  18.  
  19.     /**
  20.      * The Statement used for SQL query or update.
  21.      */
  22.     private Statement m_statement = null;
  23.  
  24.     /**
  25.      * The output stream used for HTTP output to create dynamic HTML page.
  26.      */
  27.     private ServletOutputStream m_outstream = null;
  28.  
  29. private void connectToDatabase(String aURL, String aDriver, String uid,
  30.                                String pass, ServletResponse aResponse) {
  31.  
  32.     try {
  33.         Class.forName(aDriver);
  34.  
  35.         m_connection = DriverManager.getConnection(aURL, uid,pass);
  36.     }
  37.     catch(SQLException anSQLException) {
  38.         processSQLException(anSQLException, aResponse, "CON");
  39.     }
  40.     catch (Exception anException) {
  41.         // some other kind of exception occurred so dump the call stack
  42.         anException.printStackTrace();
  43.     }
  44. }
  45. /**
  46.  * Destroys the servlet upon server shutdown.
  47.  */
  48. public void destroy() {
  49.     try {
  50.         m_connection.commit();
  51.         m_resultSet.close();
  52.         m_resultSet = null;
  53.         m_statement.close();
  54.         m_statement = null;
  55.         m_connection.close();
  56.         m_connection = null;
  57.     }
  58.     catch(SQLException anSQLException) {
  59.  
  60.         System.err.println("\n*** SQLException caught ***\n");
  61.     }
  62. }
  63. private void executeTheQuery(String anSQLQuery, ServletResponse aResponse) {
  64.  
  65.     try {
  66.         m_statement = m_connection.createStatement();
  67.         m_resultSet = m_statement.executeQuery(anSQLQuery);
  68.     }
  69.     catch (SQLException anSQLException) {
  70.         processSQLException(anSQLException, aResponse, "QUERY");
  71.     }
  72. }
  73. public void init(ServletConfig servletConfig) {
  74.  
  75.  
  76.     try{
  77.         super.init(servletConfig);
  78.     }
  79.     catch (ServletException aServletException) {
  80.         System.err.println("ServletException: " + aServletException.getMessage());
  81.     }
  82. }
  83. private void outResultSet(ServletResponse aResponse, String anActionDescription)
  84.     throws SQLException {
  85.  
  86.     try {
  87.         m_outstream = aResponse.getOutputStream();
  88.         aResponse.setContentType("text/html");
  89.         m_outstream.println("<html>");
  90.         m_outstream.println("<head><title>A Data Access Servlet using Pure JDBC</title></head>");
  91.           try {
  92.                 ResultSetMetaData resultSetMetaData = m_resultSet.getMetaData();
  93.                 DatabaseMetaData databaseMetaData = m_connection.getMetaData();
  94.  
  95.                 int iNumCols = resultSetMetaData.getColumnCount();
  96.  
  97.                 // extra connection information provided debugging purposes
  98.                 m_outstream.println("JDBC DATABASE CONNECTION DETAILS:");
  99.                 m_outstream.println("<BR>");
  100.  
  101.                 m_outstream.println("<BR>Current date and time: " + new java.util.Date().toString());
  102.  
  103.                 m_outstream.println("<BR>Connection URL: " + databaseMetaData.getURL());
  104.  
  105.                 m_outstream.println("<BR>Driver: " +
  106.                     databaseMetaData.getDriverName());
  107.  
  108.                 m_outstream.println("<BR>Version: " +
  109.                     databaseMetaData.getDriverVersion());
  110.  
  111.                 m_outstream.println("<BR>User Name: " +
  112.                     databaseMetaData.getUserName());
  113.  
  114.                 m_outstream.println("<BR>Driver Major Version: " +
  115.                     databaseMetaData.getDriverMajorVersion());
  116.  
  117.                 m_outstream.println("<BR>Driver Minor Version: " +
  118.                     databaseMetaData.getDriverMinorVersion());
  119.  
  120.                 m_outstream.println("<BR>Database Product Name: " +
  121.                     databaseMetaData.getDatabaseProductName());
  122.  
  123.                 m_outstream.println("<BR>Database Product Version: " +
  124.                     databaseMetaData.getDatabaseProductVersion());
  125.  
  126.                 m_outstream.println("<BR>SQL query or update command processed by servlet: ");
  127.                 m_outstream.println("<BR><BR>");
  128.  
  129.                 m_outstream.println("<TABLE>");
  130.                 m_outstream.println("<TABLE BORDER=1>");
  131.  
  132.                 String strCaption = "TABLE: TEST ***** ACTION TAKEN: " + anActionDescription;
  133.  
  134.                 m_outstream.println("<CAPTION>" + strCaption + "</CAPTION>");
  135.  
  136.                 int i;
  137.                 // create column names
  138.  
  139.                 for (i = 1; i <= iNumCols; i++) {
  140.                     m_outstream.println("<TH>" + resultSetMetaData.getColumnLabel(i));
  141.                 }
  142.  
  143.                 boolean more = m_resultSet.next();
  144.  
  145.                 // fill cells in table with values extracted from result set
  146.                 byte[] tmp = new byte[10];
  147.                 String aStr;
  148.                 while (more) {
  149.                     m_outstream.println("<TR>");
  150.                     for (i = 1; i <= iNumCols; i++) {
  151.                         if (i==1) {
  152.                             tmp = m_resultSet.getBytes(i);
  153.                             aStr = new String(tmp,"ibm819");
  154.                                m_outstream.println("<TD>" + aStr);
  155.                         } else {
  156.                                m_outstream.println("<TD>" + m_resultSet.getString(i));
  157.                         }                        
  158.                     }
  159.                     more = m_resultSet.next();
  160.                 }
  161.                 m_outstream.println("</FONT>");
  162.                 m_outstream.println("</TABLE>");
  163.  
  164.             }
  165.             catch (SQLException anSQLException) {
  166.                 processSQLException(anSQLException, aResponse, "OUT");
  167.             }
  168.  
  169.             m_outstream.println("</body></html>");
  170.             m_outstream.flush();
  171.             m_outstream.close();
  172.     }
  173.     catch(IOException anIOException) {
  174.         System.err.println ("\n*** IOException caught ***\n");
  175.         System.err.println("IOException: " + anIOException.toString());
  176.     }
  177. }
  178. private void processSQLException(SQLException anSQLException,
  179.                                  ServletResponse aResponse, String str) {
  180.  
  181.     try {
  182.         m_outstream = aResponse.getOutputStream();
  183.         aResponse.setContentType("text/html");
  184.         m_outstream.println("<html>");
  185.         m_outstream.println("<head><title>Servlet threw an SQLException</title></head>");
  186.         m_outstream.println("<body BGCOLOR=#000000 TEXT=#C0C0C0>");
  187.  
  188.         m_outstream.println("<P>*** An SQLException was caught:" + str + " ***</P>");
  189.         //while (true) {;//(anSQLException != null) {
  190.         while(anSQLException != null) {
  191.             m_outstream.println("<P>" + "SQLState: " +
  192.                 anSQLException.getSQLState() + "</P>");
  193.             m_outstream.println("<P>Message:  " + anSQLException.getMessage() + "</P>");
  194.             m_outstream.println("<P>Vendor:   " + anSQLException.getErrorCode() + "</P>");
  195.             anSQLException = anSQLException.getNextException();
  196.             m_outstream.flush();
  197.             m_outstream.close();
  198.         }
  199.     }
  200.     catch(IOException anIOException) {
  201.         System.err.println ("\n*** IOException caught ***\n");
  202.         System.err.println("IOException: " + anIOException.toString());
  203.     }
  204. }
  205. public void service(ServletRequest aRequest, ServletResponse aResponse)
  206.     throws ServletException, IOException {
  207.  
  208.     String aDriver="org.gjt.mm.mysql.Driver";
  209.     String url = "jdbc:mysql://202.120.182.10:3306/test";
  210.     if(m_connection == null) {
  211.         connectToDatabase(url, aDriver,"dehua", "yangdehua", aResponse);
  212.         executeTheQuery("SELECT * FROM test", aResponse);
  213.         try {
  214.             outResultSet(aResponse, "User-defined Query");
  215.         }
  216.         catch (SQLException anSQLException) {
  217.             processSQLException(anSQLException, aResponse,"AAA");
  218.         }
  219.     }
  220.     // close connections and free resources
  221.     try {
  222.         m_connection.commit();
  223.         m_resultSet.close();
  224.         m_resultSet = null;
  225.         m_statement.close();
  226.         m_statement = null;
  227.         m_connection.close();
  228.         m_connection = null;
  229.     }
  230.     catch(SQLException anSQLException) {
  231.         processSQLException(anSQLException, aResponse,"SERV");
  232.     }
  233. }
  234. }