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

x
?
Solved

trouble with exceptions

Posted on 2005-04-05
4
Medium Priority
?
254 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:annie613
  • 2
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
aozarov earned 700 total points
ID: 13708552
Not your problem but might be a problem later on -> Move your conn.close(); to a finally statement.

Move:
nt 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");

inside the try~catch
0
 

Author Comment

by:annie613
ID: 13708615
actually that was the problem. i need to move that into the try catch :) cheers
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13708706
"Not your problem" was refering only for the first line :-)
0
 

Author Comment

by:annie613
ID: 13708733
ahh i see now! cheers! :) thanks for the suggestions!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

581 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