Link to home
Start Free TrialLog in
Avatar of annie613
annie613

asked on

trouble with exceptions

i am having some trouble with exceptions. when i pass data to my servlet form and click on the update button my data is altered and the user can see the modifications...however, i wanted to prgram for error checking...

if the user leaves a field blank i want a message to display
also if a user enters  bad data (like a letter in a number field) i want another message to display

i thought i had the code correct with the NumberFormatException but the program seems to move over this code and when a user enters in wrong data like the letter 'd' i get this error
java.lang.NumberFormatException: For input string: "d"
not a regular screen that says
Input Type Not Valid!
from my code out.println("Input Type Not Valid!");

any suggestions??? thanks in advance annie :)

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out;  
String title;

response.setContentType("text/html");
out = response.getWriter();

//retrieve data sent by the user
int id = Integer.parseInt(request.getParameter("form_PROGRAMID"));
int playdate = Integer.parseInt(request.getParameter("form_PLAYDATE"));
int runtime = Integer.parseInt(request.getParameter("form_RUNTIME"));
String programname = request.getParameter("form_PROGRAMNAME");
String genre = request.getParameter("form_GENRE");

title = "Update record to database";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#00FF00\">\n" +
"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n");

try
{
//connect to DB
DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
String dbURL = "jdbc:oracle:thin:@172.20.138.6:1521:ORA1";
Connection conn = DriverManager.getConnection(dbURL, "myTestDB", "password");
Statement stmt = conn.createStatement();
//SQL statement to update the fields
String updateSQL = "UPDATE SHOWS SET PLAYDATE=?, RUNTIME=?, " +
"PROGRAMNAME=?, GENRE=? WHERE PROGRAMID = '" + id + "'";

PreparedStatement prepStmt = conn.prepareStatement(updateSQL);
prepStmt.setInt(1, playdate);
prepStmt.setInt(2, runtime);
prepStmt.setString(3, programname);
prepStmt.setString(4, genre);

prepStmt.executeUpdate();

//close connections
stmt.close();
conn.close();

//display to user the record was updated and show link to DB contents
out.println("Record " + id + " has been successfully updated.<br>");
out.println("<A HREF='week09.ShowTV' >Click here</A> to "
+ "display database.");        

}//end try

//*********************************PROBLEM WITH EXCEPTIONS****************
//not sure why this  doesnt work..the code seems solid
catch(NumberFormatException n)
{  
//display error to user if user does not insert an integer value
out.println("Input Type Not Valid!");
}//end catch

//not sure what type of exception i need for null fields???
/*
catch(NullSomething n)
{  
//display error to user if user does not insert an integer value
out.println("Fields Cannot be left Null!");
}//end catch
*/

//this one seems to work!
catch(Exception e)
{
out.println("There was a problem: <BR>/n");
out.println(e);
}//end catch
//*********************************PROBLEM WITH EXCEPTIONS****************


out.println("</BODY></HTML>");
out.close();//close resources
}//end doGet
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of annie613
annie613

ASKER

actually that was the problem. i need to move that into the try catch :) cheers
"Not your problem" was refering only for the first line :-)
ahh i see now! cheers! :) thanks for the suggestions!