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
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 ;
}
}
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
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 ;
}
}