I read jsp tutorial by peter_budo on database connectivity using MVC model in jsp then I tried to create a registration page but I have errors on my page . Please help me correct them.
I here have
1. registration.jsp - as interface for taking input
2. RegisterServlet.java - as controller
3. RegisterBean - as model
4. UserBean
Database I have use is MS access with DSN name lib.

Error which I get with the cade below is "RegisterServlet.java 53:Unexpected exception java.sql.SQLException;must be caught or declared to be thrown...if(registerBean.userExist(userBean.getUserName(),userBean.getPassword()))" . Hope solving this would make the registration page to run properly.

1.registration.jsp

<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>Library Management System : Registration</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META content="MSHTML 6.00.2900.2180" name=GENERATOR></HEAD>
<BODY>
 <%@ include file = "header.html" %>
 <table border=2 width="17%" cellpadding="5" cellspacing="0" align=LEFT>
  <tr valign="top" >
   <td width="16%" bgcolor="#b0c4de">
   <p align="center"><b><a href="index.jsp"><font color="#056796">Login</font></a></b>
   </td>
  </tr>
 </table>

 
 <table border=2 width="83%" cellpadding="0" cellspacing="0">
  <tr valign="top">
   <td>
   <center>
	<h4>
    	<font color="#FF0000"><i>
    	<% String e = (String) session.getAttribute("error" );
			if(e != null)
			{
				out.print(e); }%>
		</i></font>
      </h4>
    </center>
   </td>
  </tr> 
  <tr valign="top" >
   <td width = "84%" >
    <P align=center><FONT color=#056796><STRONG><U>Registration Page</U></STRONG></FONT></P>
    <form action = register.jsp method = POST>
    <TABLE borderColor=#056796 cellSpacing=5 cellPadding=5 align=center border=0>
     <TBODY>
      <TR>
       <TD borderColor=#056796>
       <P align=right><FONT color=#056796>Username</FONT></P></TD>
       <TD borderColor=#056796><INPUT id=text1  name=userName SIZE="16" >
       </TD>
      </TR>
      <TR>
       <TD borderColor=#056796>
        <P align=right><FONT color=#056796>First Name</FONT></P></TD>
       <TD borderColor=#056796><INPUT id=text1 name=fName SIZE="16" >
       </TD>
      </TR>
      <TR>
       <TD borderColor=#056796>
        <P align=right><FONT color=#056796>Last Name</FONT></P></TD>
       <TD borderColor=#056796><INPUT id=text1 name=lName SIZE="16" >
       </TD>
      </TR>
      <TR>
       <TD borderColor=#056796>
        <P align=right><FONT color=#056796>Address</FONT></P></TD>
       <TD borderColor=#056796><TEXTAREA NAME="address" COLS="16" ROWS="3 "></TEXTAREA></td></tr>
      <TR>
       <TD borderColor=#056796>
        <P align=right><FONT color=#056796>Branch</FONT></P>
       </TD>
       <TD borderColor=#056796><INPUT id=text2 name=branch SIZE="16" ></TD></TR>
      <TR>
       <TD borderColor=#056796>
        <P align=right><FONT color=#056796>Year</FONT></P></TD>
       <TD borderColor=#056796><INPUT id=text7 name=year SIZE="16"></TD></TR>
      <TR>
       <TD borderColor=#056796>
        <P align=right><FONT color=#056796>Phone</FONT></P></TD>
       <TD borderColor=#056796><INPUT id=text4 name=phone SIZE="16"></TD></TR>
      <TR>
       <TD borderColor=#056796>
        <P align=right><FONT color=#056796>Email</FONT></P></TD>
       <TD borderColor=#056796><INPUT id=text5 name=email SIZE="16" ></TD></TR>
       <TD borderColor=#056796>
        <P align=right><FONT color=#056796>Password</FONT></P></TD>
       <TD borderColor=#056796><INPUT id=text7 name=password SIZE="16"></TD></TR>
      <TR>
       <TD borderColor=#056796>
        <P align=right><FONT color=#056796>Retype Password</FONT></P></TD>
       <TD borderColor=#056796><INPUT id=text8 name=retypePassword SIZE="16"></TD></TR>
      <TR>
       <TD borderColor=#056796>
        <P align=right><FONT color=#056796>You are</FONT></P></TD>
       <TD borderColor=#056796>
        <P><FONT color=#000000><INPUT id=radiobutton1 type=radio value=radiobutton1 name=youAre  checked></FONT><FONT color=#056796>Student</FONT></P>
        <P><FONT color=#056796><INPUT id=radiobutton2 type=radio value=radiobutton2 name=youAre >Faculty</FONT></P></TD></TR>
        <TR>
       <TD borderColor=#056796>
         <P align=center><INPUT id=button1 type=submit value=Register name=button1 ></P>
        </TD></TR>
     </TBODY>
   </TABLE>
   </form>
</P></U></STRONG></FONT>

</td>
</tr>
</table>

</BODY></HTML>

2.RegisterServlet.java

import java.io.Serializable;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.SQLException;

public class RegisterServlet extends HttpServlet {
	private String dbResult = null;
	private String myException = null;	
	private String strError = null;
	private boolean dbOK = false;
	private boolean userChk = false;
	
	// Connection string data
	private String db_URL;
	private String db_username;
	private String db_password;
	
	//Instance of beans
	private UserBean userBean;
	
	private RegisterBean registerBean;
	
	public void init(){
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,
	IOException{
		userBean = new UserBean();
		userBean.setUserName(request.getParameter("username"));
		userBean.setFName(request.getParameter("fName"));
		userBean.setLName(request.getParameter("lName"));
		userBean.setAddress(request.getParameter("address"));
		userBean.setBranch( request.getParameter("branch"));
		userBean.setYear(request.getParameter("year"));
		userBean.setPhone(request.getParameter("phone"));
		userBean.setEmail(request.getParameter("email"));
		userBean.setPassword( request.getParameter("password"));
		userBean.setRetypePassword(request.getParameter("retypePassword"));
		userBean.setYouAre(request.getParameter("youAre"));	
		
		if(validate(userBean)){
			if(registerBean.userExist(userBean.getUserName(),userBean.getPassword())){
			registerBean.saveData(userBean);
			dbOK = true;
		        }
		        else{
                        strError = "User Name already exists.";
			dbOK = false;				
		        }
		}
		else
		{
			strError = "Invalid details";
			dbOK = false;
		}
				
		HttpSession session = request.getSession(true);
		
		if(dbOK)
		{
			//redirect to member/admin pages
			if((userBean.getYouAre()).equals("student")||(userBean.getYouAre()).equals("faculty"))
			{
				session.setAttribute( "userBean", userBean);
				RequestDispatcher dispatcher = request.getRequestDispatcher("/memberMain.jsp");
				dispatcher.forward( request, response);				
			}
			
			else if((userBean.getYouAre()).equals("administrator"))
			{
				session.setAttribute( "userBean", userBean);
				RequestDispatcher dispatcher = request.getRequestDispatcher("/adminMain.jsp");
				dispatcher.forward( request, response);	
			}
			else{
				strError = "error";
			}
		}
		else
		{
			//Error after DB login checkout, redirect back to index.jsp
			session.setAttribute( "error", strError);
			RequestDispatcher dispatcher = request.getRequestDispatcher("/registration.jsp");
			dispatcher.forward( request, response);	
		}		
		
	}
	public boolean validate(UserBean uB )
	{
		UserBean uBV =new UserBean();
		uBV=uB;
		if(uBV.userName.length() != 6 || uB.pwd.length() < 6 || uB.pwd.length() > 20 || !(uB.pwd).equals(uB.rePwd))
		{
			return false;
		}
		return true;
	}	
	public void destroy(){
	}
}

3.RegisterBean.java

import java.sql.*;
import javax.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.*;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.*;
import java.io.*; //this allows javabean to be shared between diff components of web 
                             //applicatn ,even between server restarts

public class RegisterBean implements Serializable {
	private boolean valid=false;	
	public RegisterBean(){
	}
	private String dbURL = "jdbc:odbc:lib";  
    /*
     * Open database connection
     */
    public Connection getConnection() 
    {
	    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	    Connection conn = null;
	    try {	    
	      conn = DriverManager.getConnection(dbURL);
	      }
	    catch (SQLException e) {
	      System.out.println("Could not connect to DB: " + e.getMessage());
	      }
	    return conn;
    }
    
    /*
     * Close open database connection
     */
    public void putConnection(Connection conn) 
    {
	    if (conn != null) 
	    {
	    	try 
	    	{ 
	    		conn.close(); 
	    	}
	      catch (SQLException e) { }
	    }
    }
    
    /*
     * Check if single user exists
     */
    public boolean userExist(String uN, String pwd)throws SQLException,ServletException,java.io.IOException
	{	
		//String uN = userName1;
		//String pwd = password1;
		int records=0;
		//UserBean uBExist = new UserBean();
		Connection conn = getConnection();	
	    if (conn != null) 
	    {
	    	ResultSet rs = null;
	    	Statement stmt = null;
		
			try{
				String strQuery ="select userName,password from member where userName='"+uN+"' and password='"+pwd+"'";
				stmt = conn.createStatement();
				rs = stmt.executeQuery( strQuery);
				if(rs.next()){
                                   return false;
                                }
				else{
					return true;
				}
			}
			catch(Exception e)
			{
				System.out.println("Error on DB return");
				//userChk = true;				
			}
			//end of try
			finally 
			{
	        	if (stmt != null) 
	        	{
	        		try { stmt.close(); }
	          		catch (SQLException e) { }
	          	}
	          	putConnection(conn);	        
	        }//end of finally
	    }//end of if
	    
	}	
		
	/*
	 * save
	 */
	
	public void saveData(UserBean uB){
		//UserBean uBSave = new UserBean();
		//uBSave = uB;
		Connection conn = getConnection();	
	    if (conn != null) 
	    {
		    PreparedStatement ps=null;
			try
			{
				ps =conn.prepareStatement("insert into member(userName,firstName,lastName,address,branch,year,email,youAre,password) values(?,?,?,?,?,?,?,?,?)");
				ps.setString(1,uB.userName);
                                ps.setString(2,uB.fName);
                                ps.setString(3,uB.lName);
                                ps.setString(4,uB.addr);
                                ps.setString(5,uB.branch);
                                ps.setString(6,uB.yr);
                                ps.setString(7,uB.ph);
                                ps.setString(8,uB.email);
                                ps.setString(9,uB.youAre);
                                ps.setString(10,uB.pwd);
				ps.executeUpdate();
			}
			
			finally 
			{
	        	if (ps != null) 
	        	{
	        		try { ps.close(); }
	          		catch (SQLException e) { }
	          	}
	          	putConnection(conn);	        
	        }
	    }
	}
}

4.UserBean.java

public class UserBean
{
     String userName;       //Username        
     String fName;       //First Name
     String lName;       //Last Name
     String addr;        //Address
     String branch;      //branch
     String yr;        //year
     String ph;          //phone no
     String email;          //email Id
     String pwd;         //password
     String rePwd;       //Phone number
     String youAre;      //student/faculty/admin??

    public UserBean() {}
        public String getUserName(){
		return userName;
	}
	public void setUserName(String value){
		userName=value;
	}
	public String getFName(){
		return fName;
	}
	public void setFName(String value){
		fName=value;
	}
	public String getLName(){
		return lName;
	}
	public void setLName(String value){
		lName=value;
	}
	public String getAddress(){
		return addr;
	}
	public void setAddress(String value){
		addr=value;
	}
	public String getBranch(){
		return branch;
	}
	public void setBranch(String value){
		branch=value;
	}
	public String getYear(){
		return yr;
	}
	public void setYear(String value){
		yr=value;
	}
	public String getPhone(){
		return ph;
	}
	public void setPhone(String value){
		ph=value;
	}
	public String getEmail(){
		return email;
	}
	public void setEmail(String value){
		email=value;
	}
	public String getPassword(){
		return pwd;
	}
	public void setPassword(String value){
		pwd=value;
	}	
	public String getRetypePassword(){
		return rePwd;
	}
	public void setRetypePassword(String value){
		rePwd=value;
	}
	public String getYouAre(){
		return youAre;
	}
	public void setYouAre(String value){
		youAre=value;
	}
}

I am totally stuck here . Hope I would get the answers quickly to get started again...:)

Are you sure it is whole error list? Looking at your code

if(registerBean.userExist(userBean.getUserName(),userBean.getPassword()))

the RegisterBean userExist(String, String) method throws SQLException, however line bellow

registerBean.saveData(userBean);

is another RegisterBean method saveData(UserBean) that does not throw or catch SQLException. (The catch statement inside finally doesn't count as it is related to PreparedStatement) In your RegisterBean while executing this saveData(UserBean) method there is only try and finally statement

With the code I have posted , I am getting only error with

if(registerBean.userExist(userBean.getUserName(),userBean.getPassword()))

but you are right with saveData method , when I delete userExist method then I am experiencing same error with

registerBean.saveData(userBean);

What should I do?

You not catching SQLException in that method. As I said you have only try & finally but no catch. Add the catch section between them

I added catch section in savaData method but no change.
I think we are still missing the problem , because userExist method already had catch statement but is still giving SQLException error .
Errors which now I have

RegisterServlet.java 61:Unexpected exception java.sql.SQLException;must be caught or declared to be thrown...if(registerBean.userExist(userBean.getUserName(),userBean.getPassword()))

RegisterServlet.java 62:Unexpected exception java.sql.SQLException;must be caught or declared to be thrown...registerBean.saveData(userBean)

Please post the code with changes you made

Here's the code .
I made some change to get them working with other modules of my project . the registration.jsp page is similar as before . I removed password and retype password property from UserBean.java .

=>RegisterServlet.java

import java.io.Serializable;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class RegisterServlet extends HttpServlet {	
	private String strError = null;
	private boolean dbOK = false;
	private boolean userChk = false;
	private String uN;
	private String pwd;
	private String rePwd;
	
	// Connection string data
	private String db_URL;
	
	//Instance of beans
	private UserBean userBean;
	
	 RegisterBean registerBean;
	
	public void init(){
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}
		catch (Exception ex)
		{
			System.out.println("Exception is : " + ex.toString() );
		}
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,
	IOException{
		userBean = new UserBean();
		userBean.setUserName(request.getParameter("username"));
		userBean.setFName(request.getParameter("fName"));
		userBean.setLName(request.getParameter("lName"));
		userBean.setAddress(request.getParameter("address"));
		userBean.setBranch( request.getParameter("branch"));
		userBean.setYear(request.getParameter("year"));
		userBean.setPhone(request.getParameter("phone"));
		userBean.setEmail(request.getParameter("email"));
		userBean.setYouAre(request.getParameter("youAre"));
		pwd=request.getParameter("password");
		rePwd=request.getParameter("retypePassword");
		uN=userBean.getUserName();	
		registerBean =new RegisterBean();
		if(pwd.length()!=6&&(pwd.equals(rePwd))&&validate(userBean)){
			if(!registerBean.userExist(uN,pwd)){
			registerBean.saveData(userBean,pwd);
			dbOK = true;
		        }
		        else{
                        strError = "User Name already exists.";
			dbOK = false;				
		        }
		}
		else
		{
			strError = "Invalid details";
			dbOK = false;
		}
				
		HttpSession session = request.getSession(true);
		
		if(dbOK)
		{
			//redirect to member/admin pages
			if((userBean.getYouAre()).equals("student")||(userBean.getYouAre()).equals("faculty"))
			{
				session.setAttribute( "userBean", userBean);
				RequestDispatcher dispatcher = request.getRequestDispatcher("/memberMain.jsp");
				dispatcher.forward( request, response);				
			}
			
			else if((userBean.getYouAre()).equals("administrator"))
			{
				session.setAttribute( "userBean", userBean);
				RequestDispatcher dispatcher = request.getRequestDispatcher("/adminMain.jsp");
				dispatcher.forward( request, response);	
			}
			else{
				strError = "error";
			}
		}
		else
		{
			//Error after DB login checkout, redirect back to index.jsp
			session.setAttribute( "error", strError);
			RequestDispatcher dispatcher = request.getRequestDispatcher("/registration.jsp");
			dispatcher.forward( request, response);	
		}		
		
	}
	private boolean validate(UserBean uB )
	{
		
		if(uB.getUserName().length() != 6 )
		{
			return false;
		}
		return true;
	}	
	public void destroy(){
	}
}

=>RegisterBean.java

import java.sql.*;
//import javax.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.*;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.*;
import java.io.*; //this allows javabean to be shared between diff components of web 
                             //applicatn ,even between server restarts

public class RegisterBean implements Serializable {
	private boolean valid=false;	
	public RegisterBean(){
	}
	private String dbURL = "jdbc:odbc:lib";  
    /*
     * Open database connection
     */
    public Connection getConnection() 
    {
	    
	    Connection conn = null;
	    try {	    
	      conn = DriverManager.getConnection(dbURL);
	      }
	    catch (SQLException e) {
	      System.out.println("Could not connect to DB: " + e.getMessage());
	      }
	    return conn;
    }
    
    /*
     * Close open database connection
     */
    public void putConnection(Connection conn) 
    {
	    if (conn != null) 
	    {
	    	try 
	    	{ 
	    		conn.close(); 
	    	}
	      catch (SQLException e) { }
	    }
    }
    
    /*
     * Check if single user exists
     */
    public boolean userExist(String uN, String pwd)throws SQLException,ServletException,IOException
	{	
		String userName1 = uN;
		String password1= pwd ;
		
		//UserBean uBExist = new UserBean();
		Connection conn = getConnection();	
	    if (conn != null) 
	    {
	    	ResultSet rs = null;
	    	Statement stmt = null;
		
			try{
				String strQuery ="select * from member where userName='"+userName1+"' and password='"+password1+"'";
				stmt = conn.createStatement();
				rs = stmt.executeQuery( strQuery);
				if(rs.next()){
                                   return true;
                                }
				else{
					return false;
				}
			}
			catch(Exception e)
			{
				System.out.println("Error on DB return");
				//userChk = true;				
			}
			//end of try
			finally 
			{
	        	if (stmt != null) 
	        	{
	        		try { stmt.close(); }
	          		catch (SQLException e) { }
	          	}
	          	putConnection(conn);	        
	        }//end of finally
	    }//end of if
	    
	}	
	
   
	/*
	 * save
	 */
	
	public void saveData(UserBean uB,String pwd)throws SQLException,ServletException,IOException{
		//UserBean uBSave = new UserBean();
		//uBSave = uB;
		Connection conn = getConnection();	
	    if (conn != null) 
	    {
		    PreparedStatement ps=null;
			try
			{
				ps =conn.prepareStatement("insert into member(userName,firstName,lastName,address,branch,year,phone,email,youAre,password) values(?,?,?,?,?,?,?,?,?,?)");
				ps.setString(1,uB.getUserName());
                                ps.setString(2,uB.getFName());
                                ps.setString(3,uB.getLName());
                                ps.setString(4,uB.getAddress());
                                ps.setString(5,uB.getBranch());
                                ps.setString(6,uB.getYear());
                                ps.setString(7,uB.getPhone());
                                ps.setString(8,uB.getEmail());
                                ps.setString(9,uB.getYouAre());
                                ps.setString(10,pwd);
				ps.executeUpdate();
			}
			catch(Exception e)
			{
				System.out.println("Error on DB return");
				//userChk = true;				
			}
			finally 
			{
	        	if (ps != null) 
	        	{
	        		try { ps.close(); }
	          		catch (SQLException e) { }
	          	}
	          	putConnection(conn);	        
	        }
	    }
	}
}

=>UserBean.java

public class UserBean
{
     String userName;       //Username        
     String fName;       //First Name
     String lName;       //Last Name
     String addr;        //Address
     String branch;      //branch
     String yr;        //year
     String ph;          //phone no
     String email;          //email Id
     //String pwd;         //password
    // String rePwd;       //Phone number
     String youAre;      //student/faculty/admin??

    public UserBean() {}
        public String getUserName(){
		return userName;
	}
	public void setUserName(String value){
		userName=value;
	}
	public String getFName(){
		return fName;
	}
	public void setFName(String value){
		fName=value;
	}
	public String getLName(){
		return lName;
	}
	public void setLName(String value){
		lName=value;
	}
	public String getAddress(){
		return addr;
	}
	public void setAddress(String value){
		addr=value;
	}
	public String getBranch(){
		return branch;
	}
	public void setBranch(String value){
		branch=value;
	}
	public String getYear(){
		return yr;
	}
	public void setYear(String value){
		yr=value;
	}
	public String getPhone(){
		return ph;
	}
	public void setPhone(String value){
		ph=value;
	}
	public String getEmail(){
		return email;
	}
	public void setEmail(String value){
		email=value;
	}
	/*public String getPassword(){
		return pwd;
	}
	public void setPassword(String value){
		pwd=value;
	}	
	public String getRetypePassword(){
		return rePwd;
	}
	public void setRetypePassword(String value){
		rePwd=value;
	}*/
	public String getYouAre(){
		return youAre;
	}
	public void setYouAre(String value){
		youAre=value;
	}
}

Please, I need quick help because my project deadline is near!

Change to RegisterServlet

if(pwd.length()!=6&&(pwd.equals(rePwd))&&validate(userBean)){
try
{
	if(!registerBean.userExist(uN,pwd)){
	registerBean.saveData(userBean,pwd);
	dbOK = true;
        }
        else{
                      strError = "User Name already exists.";
	dbOK = false;				
        }
}
catch(SQLException sqle)
{
	sqle.printStackTrace();
}
}

change to RegisterBean

public boolean userExist(String uN, String pwd)throws SQLException,ServletException,IOException
{	
	String userName1 = uN;
	String password1= pwd ;
	boolean forReturn = false; //Add
	
	//UserBean uBExist = new UserBean();
	Connection conn = getConnection();	
    if (conn != null) 
    {
    	ResultSet rs = null;
    	Statement stmt = null;
		
		try{
			String strQuery ="select * from member where userName='"+userName1+"' and password='"+password1+"'";
			stmt = conn.createStatement();
			rs = stmt.executeQuery( strQuery);
			if(rs.next()){
                                  forReturn = true; //change to variable assignment
                               }
			else{
					forReturn = false;	//change to variable assignment
			}
		}
		catch(Exception e)
		{
			System.out.println("Error on DB return");
			//userChk = true;				
		}
		//end of try
		finally 
		{
        	if (stmt != null) 
        	{
        		try { stmt.close(); }
          		catch (SQLException e) { }
          	}
          	putConnection(conn);	        
        }//end of finally
    }//end of if
    return forReturn;	// return the boolean
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.