Once you have username
as primary it will not allow to insert duplicates . so try like this
try
{
//query to insert your username which you get from user
}
If it is duplicate it will throw an exception as ora-
so handle the exception using page redirect using catch block.
catch(Exception e)
{
system.out.print(e);
response.sendRedirect("file.jsp");
}
Update
Declare an integer
variable to check whether you have inserted the value . Also change the return type of the method addAccount(Accounts bean)
to int
public int addAccount(Accounts bean) {
int count=0;
try {
bConnector = new DBConnector();
Connection c = bConnector.getConnection();
String query = "insert into account (username, name, email, password) values (?,?,?,?)";
PreparedStatement preparedstatement = c.prepareStatement(query);
preparedstatement.setString(1, bean.getUsername());
preparedstatement.setString(2, bean.getName());
preparedstatement.setString(3, bean.getEmail());
preparedstatement.setString(4, bean.getPassword());
count=preparedstatement.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(AccountsDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return count;
}
In your servlet,
int count=0;
try {
Accounts bean = new Accounts();
bean.setUsername(request.getParameter("uname"));
bean.setName(request.getParameter("fname"));
bean.setEmail(request.getParameter("email"));
bean.setPassword(request.getParameter("password"));
AccountsDAO aO = new AccountsDAO();
count=aO.addAccount(bean);
if(count>0){
response.sendRedirect("Stream.jsp"); \\Success condition here
}
else{
response.sendRedirect("file.jsp"); \\ failure condition here
}
}
catch(Exception e)
{
system.out.print(e);
}
Hope this helps!