home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 March / CMCD0305.ISO / Software / Shareware / Grafica / 3dpie / TemplateScripts / Servlet / PieAppletDB.java next >
Text File  |  2004-07-02  |  4KB  |  110 lines

  1. import java.sql.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6.  
  7. public class PieAppletDB extends HttpServlet {
  8. //
  9. // This simple servlet is designed to demonstrate how a servlet can be used
  10. // to retrieve data from a database and return the data in the correct format
  11. // to either the graphing applet or servlet.
  12. //
  13. // You may freely copy and modify this script to suite your own
  14. // requirements.
  15. //
  16. // For further information visit,
  17. // http://www.jpowered.com/pie_chart/
  18. //
  19. //-----------------------------------------------------------------------------
  20.  
  21.     // Initialise and set variables
  22.     String     url   = "jdbc:MySQL:///TESTDB";  // URL specifying the JDBC connection to a MySQL database TESTDB.
  23.     Connection con   = null;                    // Database connection object
  24.     Statement  stmt  = null;                    // Statement String
  25.     String     query;                           // Query String
  26.     int        datacount;
  27.  
  28. //-----------------------------------------------------------------------------
  29.     public void doGet(HttpServletRequest req, HttpServletResponse res)
  30.            throws ServletException, IOException {
  31.  
  32.         // Set the output characterics for the return data
  33.         res.setContentType("text/html");
  34.         ServletOutputStream out = res.getOutputStream();
  35.  
  36.         // Establish the database connection
  37.         try {
  38.             // Connect to TESTDB
  39.             Class.forName("org.gjt.mm.mysql.Driver");
  40.             con = DriverManager.getConnection (url,"[DB Username]","[DB Password]");
  41.                stmt = con.createStatement();
  42.  
  43.             // Construct HTML page containing Bar Chart Applet
  44.             out.println("<html>\n");
  45.             out.println("<head>\n");
  46.             out.println("<title>2D / 3D Horizontal Pie Chart</title>\n");
  47.             out.println("</head>\n");
  48.             out.println("<body>\n");
  49.             out.println("<applet code='PiechartApplet.class' archive='Piechart.jar' codebase='http://www.yourdomain.com/path/' width='500' height='420'>\n");
  50.             out.println("   <!-- Start Up Parameters -->\n");
  51.             out.println("  <PARAM name='LOADINGMESSAGE' value='Pie Chart Loading - Please Wait.'>     <!-- Message to be displayed on Startup -->\n");
  52.             out.println("  <PARAM name='STEXTCOLOR' value='0,0,100'>                                  <!-- Message Text Color-->\n");
  53.             out.println("  <PARAM name='STARTUPCOLOR' value='255,255,255'>                            <!-- Applet Background color -->\n");
  54.             out.println(" \n");
  55.             out.println("  <!-- Property file -->\n");
  56.             out.println("  <PARAM name='chartproperties' value='http://www.yourdomain.com/path/piepropsdb.txt'>\n");
  57.             out.println("  \n");
  58.             out.println("  <!-- Chart Data -->  \n");
  59.  
  60.             // Performing SQL query for Product X
  61.             query = "Select Value from SalesBar where Year=2004 and Product='X' ORDER BY Month";
  62.             ResultSet srs1 = stmt.executeQuery(query);
  63.  
  64.             // Write out the data for Series 1
  65.             datacount = 1;
  66.             while (srs1.next()) {
  67.                 out.println("<param name='data"+datacount+"series1' value='"+srs1.getString("Value")+"'>\n");
  68.                 datacount++;
  69.             }
  70.  
  71.             // Performing SQL query for Product Y
  72.             query = "Select Value from SalesBar where Year=2004 and Product='Y' ORDER BY Month";
  73.             ResultSet srs2 = stmt.executeQuery(query);
  74.  
  75.             // Write out the data for Series 2
  76.             datacount = 1;
  77.             while (srs2.next()) {
  78.                 out.println("<param name='data"+datacount+"series2' value='"+srs2.getString("Value")+"'>\n");
  79.                 datacount++;
  80.             }
  81.  
  82.             // write out the end of the HTML page
  83.             out.println(" \n");
  84.             out.println("</applet>\n");
  85.             out.println(" \n");
  86.             out.println("</body>\n");
  87.             out.println("</html>\n");
  88.  
  89.         } // End try
  90.  
  91.  
  92.         // Error handling
  93.         catch(ClassNotFoundException e) {out.println("Could not load database driver: " + e.getMessage());}
  94.         catch(SQLException e) {out.println("SQLException caught: " + e.getMessage());}
  95.  
  96.         // All finished so close the database connection
  97.         finally {
  98.                  try {if (con != null) con.close();}
  99.                  catch (SQLException e) {}
  100.         }
  101.  
  102.  
  103.  
  104.  
  105.     } // End doGet
  106. //-----------------------------------------------------------------------------
  107.     public void doPost(HttpServletRequest request,HttpServletResponse response)
  108.                 throws ServletException, IOException {doGet(request, response);}
  109. //-----------------------------------------------------------------------------
  110. } // End class