Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Error trapping and the like...

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
jhazard
Asked:
jhazard
  • 3
1 Solution
 
cheekycjCommented:
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
 
jhazardAuthor Commented:
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
 
cheekycjCommented:
you can have one try catch.. that is fine.

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

CJ
0
 
cheekycjCommented:
Thanx for the "A"

CJ
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now