Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Unreported exception java.sql.SQLException

Thread view: 
Kermit Piper - 04 Jun 2006 00:14 GMT
Hello, I've been knocking my head over many errors and got down to one
last error that is something that should be simple so I can move on,
but I keep getting this error:
build:
   [javac] Compiling 1 source file to C:\projects\TEST\build\src
   [javac] C:\projects\TEST\src\Login.java:20: unreported exception
java.sql.SQL
Exception; must be caught or declared to be thrown
   [javac]       String uName = validateUser(userId, password);
   [javac]                                      ^
   [javac] 1 error

BUILD FAILED
C:\projects\TEST\build.xml:43: Compile failed; see the compiler error
output for
details.

I've checked and double-checked and can not see what I'm doing wrong?
I'm declaring the exception, and set up my try/catch blocks, made sure
my braces are closed, etc. The doPost method is compiling fine, it's
when I add the validateUser method. If someone could point out what I'm
missing I'd sure appreciate it.

/*Login Class*/

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Types;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Login extends HttpServlet
{
    public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException
    {
     String userId = req.getParameter("userId");
     String password = req.getParameter("password");
     String uName = validateUser(userId, password);

        if (uName == null)
        {
            PrintWriter ot = res.getWriter();
            ot.println(" Please verify the Userid and password");
            ot.close();
        }
        else
        {

         HttpSession userSession = req.getSession(true);

         //userSession.putValue("userName", uName); (deprecated)
         userSession.setAttribute("userName", uName);

         RequestDispatcher rd =
getServletContext().getRequestDispatcher("/welcome.jsp");

            if (rd != null)
            {
                rd.forward(req,res);
            }
        }
    }

public String validateUser(String inputUserid, String inputPwd) throws
SQLException
{
    String returnString = null;
    String dbUserid = "guest"; // Your Database user id
    String dbPassword = "guest" ; // Your Database password

    try
    {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }
    catch (ClassNotFoundException cnfe)
    {
        System.out.println(cnfe);
    }

    //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    try
    {
        Connection con = DriverManager.getConnection("jdbc:odbc:myDriver",
dbUserid ,dbPassword);
        Statement stmt = con.createStatement();
        String sql = "select USERID from USERTABLE where USERID = '" +
inputUserid + "' and PASSWORD = '" + inputPwd +"' ;" ;
        ResultSet rs = stmt.executeQuery(sql);
        if (rs.next())
        {
         returnString = rs.getString("USERID");
        }
        stmt.close();
        con.close();
    }
    catch (SQLException sqle)
    {
        System.out.println(sqle);
    }

    return returnString ;
}

}
Philipp Leitner - 04 Jun 2006 01:03 GMT
The compiler message says it all. The function "validateUser()" can
throw exceptions of type "SQLException" (see the 'throws' statement in
the function declaration), so you have to deal with this exception when
you call the method, i.e. in your case you should probably catch the
exception (surround the call with a try/catch block).

/philipp

> Hello, I've been knocking my head over many errors and got down to one
> last error that is something that should be simple so I can move on,
[quoted text clipped - 108 lines]
>
> }
Juha Laiho - 04 Jun 2006 17:15 GMT
"Kermit Piper" <dice_response@hotmail.com> said:
>Hello, I've been knocking my head over many errors and got down to one
>last error that is something that should be simple so I can move on,
[quoted text clipped - 14 lines]
>
>I've checked and double-checked and can not see what I'm doing wrong?

The line number refers to the line in your doPost method where you call
validateUser.

doPost is only allowed to throw ServletException or IOException.
However, you've declared your validateUser to throw SQLException,
and you're not catching that exception around the call to
validateUser in doPost.

However, looking at your validateUser, you're already catching the
SQLException there, and validateUser will not actually be throwing
SQLExceptions, so you can just remove the "throws SQLException"
from the validateUser declaration.

Now, the remaining issue is, what should your code do when the code
in validateUser catches SQLException. How should the situation
be shown to the user, and how should the problem be logged and
handled on the server side.
Signature

Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
        PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)

Kermit Piper - 05 Jun 2006 01:31 GMT
Thanks everyone. I did this for the doPost() and it finally could
build/deploy correctly:

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException
{
String userId = req.getParameter("userId");
String password = req.getParameter("password");
String uName = "";
try
{
uName = validateUser(userId, password);
}
catch (SQLException sqle)
{
System.out.println(sqle);
}
if (uName == null)
{
PrintWriter ot = res.getWriter();
ot.println(" Please verify the Userid and password");
ot.close();
}
else
{
HttpSession userSession = req.getSession(true);
userSession.setAttribute("userName", uName);
RequestDispatcher rd =
getServletContext().getRequestDispatcher("/welcome.jsp");
if (rd != null)
{
rd.forward(req,res);
}
}
}
Thanks again,
KP
> "Kermit Piper" <dice_response@hotmail.com> said:
> >Hello, I've been knocking my head over many errors and got down to one
[quoted text clipped - 38 lines]
>          PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
> "...cancel my subscription to the resurrection!" (Jim Morrison)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.