Unreported exception java.sql.SQLException

K

Kermit Piper

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:eek:dbc: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 ;
}

}
 
P

Philipp Leitner

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


Kermit 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,
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:eek:dbc: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 ;
}

}
 
J

Juha Laiho

Kermit Piper 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,
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?

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.
 
K

Kermit Piper

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
Juha said:
Kermit Piper 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,
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?

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.
--
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top