home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 March / CMCD0305.ISO / Software / Shareware / Grafica / 3dpie / TemplateScripts / Servlet / PieData.java < prev   
Text File  |  2004-07-02  |  3KB  |  88 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 PieData 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.              out.println("  <!-- Chart Data -->  \n");
  44.  
  45.             // Performing SQL query for Product X
  46.             query = "Select Value from SalesBar where Year=2004 and Product='X' ORDER BY Month";
  47.             ResultSet srs1 = stmt.executeQuery(query);
  48.  
  49.             // Write out the data for Series 1
  50.             datacount = 1;
  51.             while (srs1.next()) {
  52.                 out.println("data"+datacount+"series1: "+srs1.getString("Value")+"\n");
  53.                 datacount++;
  54.             }
  55.  
  56.             // Performing SQL query for Product Y
  57.             query = "Select Value from SalesBar where Year=2004 and Product='Y' ORDER BY Month";
  58.             ResultSet srs2 = stmt.executeQuery(query);
  59.  
  60.             // Write out the data for Series 2
  61.             datacount = 1;
  62.             while (srs2.next()) {
  63.                 out.println("data"+datacount+"series2: "+srs2.getString("Value")+"\n");
  64.                 datacount++;
  65.             }
  66.  
  67.         } // End try
  68.  
  69.  
  70.         // Error handling
  71.         catch(ClassNotFoundException e) {out.println("Could not load database driver: " + e.getMessage());}
  72.         catch(SQLException e) {out.println("SQLException caught: " + e.getMessage());}
  73.  
  74.         // All finished so close the database connection
  75.         finally {
  76.                  try {if (con != null) con.close();}
  77.                  catch (SQLException e) {}
  78.         }
  79.  
  80.  
  81.  
  82.  
  83.     } // End doGet
  84. //-----------------------------------------------------------------------------
  85.     public void doPost(HttpServletRequest request,HttpServletResponse response)
  86.                 throws ServletException, IOException {doGet(request, response);}
  87. //-----------------------------------------------------------------------------
  88. } // End class