?
Solved

Error trapping and the like...

Posted on 2003-04-01
4
Medium Priority
?
177 Views
Last Modified: 2010-04-01
Hi
I have written a website (learning as I go) and I now need to add in error trapping.

I have read about try catch finally and put in exceptions where I think they should go but am not sure if I have done it in the most effective/efficient way and would be grateful if someone could correct me if need be.

Also, is there anything I need to add if the site has a large amount of traffic?  i.e. would the code I have be able to handle that (with the database connection etc).  I have pasted below my jsp file and my createconnection class.  If anyone has any suggestions on how I could best acheive the above I should be very grateful.

buy.jsp:

<html>
<head>
<%@ include file="headerandbuttons.jsp" %>
<%@ include file="javascript.jsp" %>
<%@ page language="java" import="java.util.*, java.sql.*" %>
<jsp:useBean id="db1" scope="page" class="com.desres.db.CreateConnection" />
<jsp:useBean id="db2" scope="page" class="com.desres.db.CreateConnection" />
</head>
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" background="images/tile_top.gif" onLoad="MM_preloadImages('images/_hi.gif','images/sell_hi.gif','images/links_hi.gif','images/aboutus_hi.gif','images/contactus_hi.gif')">

<%
String CountryID="";
CountryID=request.getParameter("CountryID");
%>

<form name="form1" method="post" action="results.jsp">
<table width="662" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="170" height="49" valign="top"><img src="images/logo_desres.gif" width="170" height="65" usemap="#Map" border="0"></td>
    <td colspan="2" valign="top" height="49"><img src="images/pixel.gif" width="590" height="27"><br>
    <img src="images/pixel.gif" width="20" height="17"><img src="images/h_buy.gif" width="274" height="17"><br>
    <img src="images/pixel.gif" width="590" height="21"></td>

  </tr>
  <tr>
    <%@ include file="buttons.jsp" %>
    <td width="496" bgcolor="#FFFFFF" valign="top">
      <table width="446" border="0" cellspacing="0" cellpadding="20">
        <tr>
          <br>
          <td valign="top" class="txt"><span class="txt_bold_black">Search for a property:</span><span class="txt_bold"><br>
            <br><br>
            </span>
               <table width="406" border="0" cellspacing="0" cellpadding="0">
                  <tr valign="bottom">
                    <td class="txt" width="130">Country:</td>
                    <td width="270">
                       <img src="images/pixel.gif" width="5" height="20">
            <select name="cmbCountry" style="width: 172; height: 23" onchange="changecountrybuy(this.options[this.selectedIndex].value)">
                  <%
                  try{
                   Connection newConn = db1.CreateConnection();
               java.sql.Statement myStmt = newConn.createStatement();
               ResultSet myRs = myStmt.executeQuery("SELECT CountryID, Country FROM tblCountry");
             
               String vListCountryID= "";
               String vListCountry= "";
                        
               while(myRs.next())
                {
                 vListCountryID = myRs.getString("CountryID");
                 vListCountry = myRs.getString("Country");
                 
                 if(vListCountryID.equals(CountryID))
                 {
                   vListCountryID=vListCountryID + " SELECTED";
                 }
                 %>
                        <option value=<%=vListCountryID%>><%=vListCountry%></option>
                 <%
                }
                 myStmt.close();
                 //close try statement                
                   }  
                  catch (SQLException s)
                  {
                        out.println("SQL Error<br>");
                     }
                %>      
                 </select>
              </td>
                  </tr>
                  <tr valign="bottom">
                    <td class="txt" width="130">County:</td>
                    <td width="270">
                    <img src="images/pixel.gif" width="5" height="20">
                  <select name="cmbCounty" style="width: 171; height: 23">
              <%
                  try{
                   Connection newConn2 = db2.CreateConnection();
               java.sql.Statement myStmt2 = newConn2.createStatement();
               ResultSet myRs2 = myStmt2.executeQuery("SELECT CountyID, County FROM tblCounty WHERE CountryID ='"+CountryID+"'");
             
               String vListCountyID= "";
               String vListCounty= "";
                        
               while(myRs2.next())
                {
                 vListCountyID = myRs2.getString("CountyID");
                 vListCounty = myRs2.getString("County");
                 
                 
                 %>
                        <option value=<%=vListCountyID%>><%=vListCounty%></option>
                 <%
                 }
                 myStmt2.close();
                 //close try statement
                 }  
                catch (SQLException s)
                {
                      out.println("SQL Error<br>");
                     }
                %>      
                 </select>
   
                    </td>
                  </tr>
                  <tr valign="bottom">
                    <td class="txt" width="130" valign="middle">Town <b>or</b> postcode:<br>
                      <br>
                      </td>
                    <td width="270" valign="middle"> <img src="images/pixel.gif" width="5" height="20">
                      <input type="text" name="txtSearchArea" size="23">&nbsp;&nbsp;&nbsp;<br>
                      &nbsp;(<font size="1">SW% will locate all properties in
                      SW)</font>
                    </td>
                  </tr>
                  <tr valign="bottom">
                    <td width="130" class="txt">Property type:</td>
                    <td width="270"> <img src="images/pixel.gif" width="5" height="20">
                      <select name="cmbType" size="1">
                  <option value="Any">Any Property
                      <option value="House">House
                      <option value="Flat">Flat
                      </select>
                    </td>
                  </tr>
                  <tr valign="bottom">
                    <td width="130" class="txt">Minimum price:</td>
                    <td width="270"> <img src="images/pixel.gif" width="5" height="20">
                      <select name="cmbFrom" size="1">
                        <option value="50000">£ 50,000
                        <option value="100000">£100,000
                        <option value="150000">£150,000
                        <option value="200000">£200,000
                        <option value="250000">£250,000
                        <option value="300000">£300,000
                        <option value="350000">£350,000
                        <option value="400000">£400,000
                        <option value="450000">£450,000
                    <option value="500000">£500,000
                        <option value="750000">£750,000
                        <option value="1000000">£1,000,000
                      </select>
                    </td>
                  </tr>
                  <tr valign="bottom">
                    <td width="130" class="txt">Maximum price:</td>
                    <td width="270"> <img src="images/pixel.gif" width="5" height="20">
                      <select name="cmbTo" size="1">
                        <option value="50000">£ 50,000
                        <option value="100000">£100,000
                        <option selected value="150000">£150,000
                        <option value="200000">£200,000
                        <option value="250000">£250,000
                        <option value="300000">£300,000
                        <option value="350000">£350,000
                        <option value="400000">£400,000
                        <option value="450000">£450,000
                    <option value="500000">£500,000
                        <option value="750000">£750,000
                        <option value="1000000">£1,000,000
                        <option value="3000000000">Above
                      </select>
                    </td>
                  </tr>
                  <tr valign="bottom">
                    <td width="130"><img src="images/pixel.gif" width="107" height="20"></td>
                    <td width="270"> <img src="images/pixel.gif" width="5" height="20">
                    <input type="submit" name="Submit" onclick="return validateBuyForm(this);" value="Search">
                    </td>
                  </tr>
                </table>



            <span class="txt_bold"> </span></td>
        </tr>
        <tr>
          <td><img src="images/pixel.gif" width="406" height="20"></td>
        </tr>
      </table>
    </td>
    <td width="135" bgcolor="#FFFFFF" valign="top"><br>
      <img src="images/image_window.gif" width="144" height="333"></td>
  </tr>
</table>
<map name="Map">
  <area shape="rect" coords="7,2,170,60" href="index.html">
</map>
</body>
</html>


createconnection.java


package com.desres.db;

import java.lang.*;
import java.io.*;
import java.sql.*;


public class CreateConnection
{


/** Creates the connection
  */
  public Connection CreateConnection () throws Exception
  {
      Connection myConn = null;

      String driverName = "org.gjt.mm.mysql.Driver";
      String dbUrl = "jdbc:mysql://www.desres.com/hazard?user=hazard&password=aaaaaa";
      Class.forName(driverName);
      myConn = DriverManager.getConnection (dbUrl);

    return myConn;
  }

}
0
Comment
Question by:jhazard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 8245938
what exactly do you need help with?

You seem to be catching sql exceptions which is good.  I prefer to redirect to an error page with a message attached rather than outputting one line.

I don't see a finally anywhere.

since you are dealing with DB connections you should add a finally like this:

finally {
  myRs.close();
  myStmt.close();
  newConn.close();
}

after each try catch so you don't have open connections when an exception occurs.

CJ
0
 

Author Comment

by:jhazard
ID: 8246046
can I have one try catch statement that has a more than one sql statement within it or is it better to seperate them?
0
 
LVL 19

Accepted Solution

by:
cheekycj earned 200 total points
ID: 8246190
you can have one try catch.. that is fine.

Unless you want the errors handled differently for each sql statement.

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 8253178
Thanx for the "A"

CJ
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
I originally wrote this article to compare SARDU and YUMI, but have now added Easy2Boot, since that is the one I currently use and find the easiest to create and alter.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question