Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JSP and JavaBeans.. Arghhh

Status
Not open for further replies.

aarushi2001

Technical User
Nov 16, 2005
109
0
0
US
Hi,

I have a login page with a username and password field. I have two java files:
1. check if username or password or both field are empty or have values.
2. files open the database and checks if the entered value matches with values in the table.

When I use these java files separately, they are throwing respective errors. But when I try and combine them, its giving me an error.

My task is:
If username and password are not empty, check if the value exists in the database.

I need to know how to call my database java file into my validation java file.

My current code is:
if (!user.equals("") && !password.equals("")) {
Beano qb = new Beano();
boolean test = qb.getVal(user,password);
}

on compiling the file, the error I get is:
C:\Program Files\apache-tomcat-5.5.12\webapps\ROOT\WEB-INF\classes\MRI>javac *
ava
MyForm.java:30: unreported exception java.lang.ClassNotFoundException; must be
aught or declared to be thrown
boolean test = qb.doQuery(user,password);

What should I do :(
 
In your class "Beano", I imagine you are doing :

Class.forName(driverClass);

This throws a ClassNotFoundException - so you must do :

Code:
try {
  Class.forName(driverClass);

} catch (ClassNotFoundException cnfe) {
  cnfe.printStackTrace(System.err);
  // add other error handling here  
}

Your form checking code is also prone to NullPointerExceptions. You should do soemthing like this :


Code:
if (user == null || password == null || user.equals("") || password.equals("")) {
   // handle error 
} else {
    Beano qb = new Beano();
    boolean test = qb.getVal(user,password);
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hi Sedj,

This is the code for my Beano class:

public boolean doQuery(String user, String password) throws ClassNotFoundException, SQLException {
Class.forName(dbDriver);
dbCon = DriverManager.getConnection(dbURL, "root", "aarushi");
...
...
...

So the revised code will be:

public boolean doQuery(String user, String password) throws ClassNotFoundException, SQLException {
try {
Class.forName(dbDriver);
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace(System.err);
System.out.println("error");
}
dbCon = DriverManager.getConnection(dbURL, "root", "aarushi");
...
...

right?

How should I handle this error in MyForm file?
 
Perhaps :

Code:
public boolean doQuery(String user, String password) throws SQLException {
try {
      Class.forName(dbDriver);
} catch (ClassNotFoundException cnfe) {
  cnfe.printStackTrace(System.err);
  throw new SQLException("Cannot load driver : " +dbDriver);
} 
      dbCon = DriverManager.getConnection(dbURL, "root", "aarushi");
  ...
}

Then in your form, when you do your bean.doQuery() method, you need to put it in a try/catch block also ...

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Ok, as reqd I have changed the Beano file.. now when I call doQuery in my form validation file.. I am using the following the set of code:

CODE:
if (!user.equals("") && !password.equals("")) {
try {
Beano qb = new Beano();
boolean test = qb.doQuery(user,password);
} catch (SQLException e) {
throw new ServletException("error", e);
}
}

is it right?
 
Thats not too bad, but I would use :

Code:
Beano qb = null;
boolean bValidated = false;
if (user == null || password == null || user.equals("") || password.equals("")) {
   // handle error 
} else {
  try {
    qb = new Beano();
    bValidated = qb.getVal(user,password);
  } catch (SQLException sqle) {
     throw new ServletException("There was an error validating the user '" +user +"'");
  }
}

if (!bValidated) {
  // handle incorrect password possibilty
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hey,

Thanks for all ur help.. but now I got a new error:

MyForm.java:33: cannot find symbol
symbol : class ServletException
location: class MRI.MyForm
throw new ServletException("error" +user);
^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
 
either change ServletException to IOException (or something else), or import javax.servlet.ServletException

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
hmm.. if I use IOException or something I get:
MyForm.java:34: unreported exception java.io.IOException; must be caught or declared to be thrown
throw new IOException("error");
^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

if I import javax.servlet...
I get:
MyForm.java:33: cannot find symbol
symbol : class ServletException
location: class MRI.MyForm
throw new ServletException("error" +user);
^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

could I be doing something else wrong?
 
Look - if you throw an exception from a method - then you must either catch that exception within the method, or declare that method as being throw, and then catch it whenever you use that method.

You cannot find ServletException symbol because you do not have the servlet api jar on your classpath.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
hi,

About ServletException error.. I have set my classpath (cos am using servlets)..

thanks for ur help... :)
 
no, but I knew one more way.. so just used my back-up strategy :)
thanks for all ur help!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top