Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7530143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:49:48+00:00 2026-05-30T04:49:48+00:00

Is there any better way to handle Exceptions using Play framework apart from what

  • 0

Is there any better way to handle Exceptions using Play framework apart from what am using?

Query q = JPA.em().createQuery("SELECT u FROM " + 
    User.class.getName() +
    " u WHERE userCode = :userCd AND password = :password"
)
.setParameter("userCd", userName)
.setParameter("password", password);

User user=null;
try {
    user = (User) q.getSingleResult();
} catch (NoResultException n) {
    flash.put("username",userName);
    flash.error("Invalid Credentials");
    index();
}

What I am trying to achieve is so simple, User validation. As of now the above code works properly but wanted to know from anyone to assist me if there is any better way to handle Exceptions in Play apart from this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:49:50+00:00Added an answer on May 30, 2026 at 4:49 am

    Part 8 of the play framework getting started guide covers off how to set up a a user login within the play framework. There is a lot of framework already built to handle this so you just need to figure out how to make use of it.

    Have a look at the simplified query stuff in the play documentation for more info on how to do queries using the play framework rather than resorting to straight JPA.

    Answering your specific question, you have a few options…

    Option 1 (pass the username and password into the query):

    User<List> users = User.find("byUsernameAndPassword", userName, password).fetch();
    if (users.size() == 0)
    {
         //do flash stuff here
    }
    else
    {
         //we found a valid user
    }
    

    Option 2 (pass only username and then check password):

    User<List> users = User.find("byUsername", userName).fetch();
    if (users.size() == 0)
    {
         //do flash stuff here
    }
    else
    {
         //we found a valid user
         // check password and if matches then continue otherwise do flash
    }
    

    Option 2 has some benefits around how you store the password etc. So you might have it salted and hashed. Or perhaps you want to lock the account after 5 attempts on it, in this scenario you need to get the user object whether or not the password matches.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Save me from raptor death - is there any better way to handle this
Is there any better way to make this query work? I'm looking for a
Is there any better way to get take a string such as (123) 455-2344
Having the hours and minutes, is there any easier or better way to set
is there any way to write this foreach in linq or another better way,
Is there any better alternative for doing string formatting in VC6, with syntax checking
Are there any better ways to use constants in a JSP than: <%@ page
Or is there any better suited 3rd party control for this purpose?
Do I use varchar(36) or are there any better ways to do it?
I have lots and lots of data in various structures. Are there any better

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.