Return-Path: Delivered-To: apmail-tomcat-users-archive@www.apache.org Received: (qmail 285 invoked from network); 28 Jan 2007 21:49:51 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 28 Jan 2007 21:49:51 -0000 Received: (qmail 69075 invoked by uid 500); 28 Jan 2007 21:49:42 -0000 Delivered-To: apmail-tomcat-users-archive@tomcat.apache.org Received: (qmail 68833 invoked by uid 500); 28 Jan 2007 21:49:42 -0000 Mailing-List: contact users-help@tomcat.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Tomcat Users List" Delivered-To: mailing list users@tomcat.apache.org Received: (qmail 68817 invoked by uid 99); 28 Jan 2007 21:49:42 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 28 Jan 2007 13:49:42 -0800 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: neutral (herse.apache.org: local policy) Received: from [68.142.198.200] (HELO smtp101.sbc.mail.mud.yahoo.com) (68.142.198.200) by apache.org (qpsmtpd/0.29) with SMTP; Sun, 28 Jan 2007 13:49:33 -0800 Received: (qmail 49923 invoked from network); 28 Jan 2007 21:49:11 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=swbell.net; h=Received:X-YMail-OSG:Message-ID:Date:From:User-Agent:X-Accept-Language:MIME-Version:To:Subject:Content-Type:Content-Transfer-Encoding; b=RQhRB67qxFHe8oNQ48EATBnbkoLT/G7JRFb8OY0WE1xCAJ3PvDY7QroJ7D6FaNgPR8jJ+Bv0KlQ9APWzmIdznIqty2YFMj0jyeDGfWlZ/7iBeY7evLElmVLHFlXiOWKr/ohKynw9EXiN7k3YVQZuvJv0B2Y7I5xyKbXU7QiAPOY= ; Received: from unknown (HELO ?127.0.0.1?) (burrus1@swbell.net@209.30.199.175 with plain) by smtp101.sbc.mail.mud.yahoo.com with SMTP; 28 Jan 2007 21:49:10 -0000 X-YMail-OSG: mYgOgFcVM1nAymUON1W_qI3yPNzkSpTtHfS8u6iqrZHytV.BkQkO7BjK9oMWiDfFIvmHFvFCl.fFCXsRHClJKgjJCJ07eGoCzOJg3LP0.rMjn8S2jJb1Z99Gml1jtVH.Gv8MXhkIQAoG2GN4o.QR5PJAscQ0Ho2U Message-ID: <45BD1A42.5030903@swbell.net> Date: Sun, 28 Jan 2007 15:48:50 -0600 From: "Mr. Steve Burrus" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax) X-Accept-Language: en-us, en MIME-Version: 1.0 To: users@tomcat.apache.org Subject: Need Help w. Database Servlet. Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org hi all. I find myself in need of help/assistance with a longtime problem of mine and that is how do you get a servlet working with a backend database server?? Here is the basic SurveyServlet.java servlet and please notice that the database server in question is IBM's Cloudscape . after the servlet I have the html file which the servlet is supposed to work with. thanx in advance for anyone's help. import java.io.*; import java.text.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class SurveyServlet extends HttpServlet { private Connection connection; private PreparedStatement updateVotes, totalVotes, results; // set up database connection and prepare SQL statements public void init( ServletConfig config ) throws ServletException { // attempt database connection and create PreparedStatements try { Class.forName( "COM.cloudscape.core.RmiJdbcDriver" ); connection = DriverManager.getConnection( "jdbc:rmi:jdbc:cloudscape:animalsurvey" ); // PreparedStatement to add one to vote total for a // specific animal updateVotes = connection.prepareStatement( "UPDATE surveyresults SET votes = votes + 1 " + "WHERE id = ?" ); // PreparedStatement to sum the votes totalVotes = connection.prepareStatement( "SELECT sum( votes ) FROM surveyresults" ); // PreparedStatement to obtain surveyoption table's data results = connection.prepareStatement( "SELECT surveyoption, votes, id " + "FROM surveyresults ORDER BY id" ); } // for any exception throw an UnavailableException to // indicate that the servlet is not currently available catch ( Exception exception ) { exception.printStackTrace(); throw new UnavailableException( exception.getMessage() ); } } // end of init method // process survey response protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { // set up response to client response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); DecimalFormat twoDigits = new DecimalFormat( "0.00" ); // start XHTML document out.println( "" ); out.println( "" ); out.println( "" ); // head section of document out.println( "" ); // read current survey response int value = Integer.parseInt( request.getParameter( "animal" ) ); // attempt to process a vote and display current results try { // update total for current survey response updateVotes.setInt( 1, value ); updateVotes.executeUpdate(); // get total of all survey responses ResultSet totalRS = totalVotes.executeQuery(); totalRS.next(); int total = totalRS.getInt( 1 ); // get results ResultSet resultsRS = results.executeQuery(); out.println( "Thank you!" ); out.println( "" ); out.println( "" ); out.println( "

Thank you for participating." ); out.println( "
Results:

" );
         
         // process results
         int votes;
         
         while ( resultsRS.next() ) {
            out.print( resultsRS.getString( 1 ) );
            out.print( ": " );
            votes = resultsRS.getInt( 2 );
            out.print( twoDigits.format(
               ( double ) votes / total * 100 ) );
            out.print( "%  responses: " );
            out.println( votes );
         }

         resultsRS.close();
         
         out.print( "Total responses: " );
         out.print( total );
         
         // end XHTML document
         out.println( "
" ); out.close(); } // if database exception occurs, return error page catch ( SQLException sqlException ) { sqlException.printStackTrace(); out.println( "Error" ); out.println( "" ); out.println( "

Database error occurred. " ); out.println( "Try again later.

" ); out.close(); } } // end of doPost method // close SQL statements and database when servlet terminates public void destroy() { // attempt to close statements and database connection try { updateVotes.close(); totalVotes.close(); results.close(); connection.close(); } // handle database exceptions by returning error to client catch( SQLException sqlException ) { sqlException.printStackTrace(); } } // end of destroy method } the html file : Survey

What is your favorite pet?

Dog
Cat
Bird
Snake
None

--------------------------------------------------------------------- To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org For additional commands, e-mail: users-help@tomcat.apache.org