?
Solved

trouble with exceptions

Posted on 2005-04-05
4
Medium Priority
?
247 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
[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
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

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