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.NumberFormatExce ption: 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("t ext/html") ;
out = response.getWriter();
//retrieve data sent by the user
int id = Integer.parseInt(request.g etParamete r("form_PR OGRAMID")) ;
int playdate = Integer.parseInt(request.g etParamete r("form_PL AYDATE"));
int runtime = Integer.parseInt(request.g etParamete r("form_RU NTIME"));
String programname = request.getParameter("form _PROGRAMNA ME");
String genre = request.getParameter("form _GENRE");
title = "Update record to database";
out.println(ServletUtiliti es.headWit hTitle(tit le) +
"<BODY BGCOLOR=\"#00FF00\">\n" +
"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n");
try
{
//connect to DB
DriverManager.registerDriv er( new oracle.jdbc.driver.OracleD river());
String dbURL = "jdbc:oracle:thin:@172.20. 138.6:1521 :ORA1";
Connection conn = DriverManager.getConnectio n(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(upda teSQL);
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
//************************ *********P ROBLEM WITH EXCEPTIONS****************
//not sure why this doesnt work..the code seems solid
catch(NumberFormatExceptio n 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
//************************ *********P ROBLEM WITH EXCEPTIONS****************
out.println("</BODY></HTML >");
out.close();//close resources
}//end doGet
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.NumberFormatExce
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("t
out = response.getWriter();
//retrieve data sent by the user
int id = Integer.parseInt(request.g
int playdate = Integer.parseInt(request.g
int runtime = Integer.parseInt(request.g
String programname = request.getParameter("form
String genre = request.getParameter("form
title = "Update record to database";
out.println(ServletUtiliti
"<BODY BGCOLOR=\"#00FF00\">\n" +
"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n");
try
{
//connect to DB
DriverManager.registerDriv
String dbURL = "jdbc:oracle:thin:@172.20.
Connection conn = DriverManager.getConnectio
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(upda
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
//************************
//not sure why this doesnt work..the code seems solid
catch(NumberFormatExceptio
{
//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
//************************
out.println("</BODY></HTML
out.close();//close resources
}//end doGet
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
"Not your problem" was refering only for the first line :-)
ASKER
ahh i see now! cheers! :) thanks for the suggestions!
ASKER