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 8388123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:24:01+00:00 2026-06-09T18:24:01+00:00

I’m using spring mvc and I created the CRUD functionality. But I want to

  • 0

I’m using spring mvc and I created the CRUD functionality. But I want to create a search function that will allow me to find a user by any parameter (variable) as ‘userid’ or ‘username’ or ‘lastname’ or ‘social security number’ or whatever.

My userid is an integer type.

How can I do that? What is the SQL query for that?

How can I check if the input is integer or string and then go through the database by the given parameter and search for the user?

  • 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-06-09T18:24:02+00:00Added an answer on June 9, 2026 at 6:24 pm

    If you are using Hibernate for data access you can easily create universal finder using criteria API:

    Abstract DAO class:

    public abstract class AbstractHibernateDAO<T> {
    
        private static final String PARAM_VALUE_PARAMETER = "paramValue";
    
        private final Class<T> clazz;
    
        @Autowired
        private SessionFactory sessionFactory;
    
        public AbstractHibernateDAO(Class<T> clazz) {
            this.clazz = clazz;
        }
    
        public T findOne(String paramName, Object paramValue) {
            Session session = sessionFactory.getCurrentSession();
    
            @SuppressWarnings("unchecked")
            T fetchedObject = (T) session.createCriteria(clazz).add(Restrictions.eq(paramName, paramValue)).uniqueResult();
    
            return fetchedObject;
        }
    
        // Other CRUD methods.
    
    }
    

    Concrete DAO class for entity:

    @Repository
    @Transactional
    public class ProductHibernateDAO extends AbstractHibernateDAO<Product> {
    
        public ProductHibernateDAO() {
            super(Product.class);
        }
    
    }
    

    Or if you prefer to use HQL instead of Criteria API you can rewrite search method as:

    public T findOne(String paramName, Object paramValue) {
        Session session = sessionFactory.getCurrentSession();
    
        StringBuilder queryText = new StringBuilder();
    
        queryText.append("from ");
        queryText.append(clazz.getSimpleName());
        queryText.append(" where ");
        queryText.append(paramName);
        queryText.append("=:");
        queryText.append(PARAM_VALUE_PARAMETER);
    
        @SuppressWarnings("unchecked")
        T fetchedObject = (T) session.createQuery(queryText.toString()).setParameter(PARAM_VALUE_PARAMETER, paramValue).uniqueResult();
    
        return fetchedObject;
    }
    

    In this article you can find very good description how to create generic DAO with hibernate (Or if you prefer JPA there are also described how to do this with JPA).

    Or if you prefer to use JDBC for data access I recommend you to look at Spring’s JdbcTemplate. It simplifies development a lot. Here how you can implement universal finder using JdbcTemplate:

    @Repository
    @Transactional
    public class ProductJDBCDAO implements DAO<Product> {
    
        private static final String TABLE_NAME = "product";
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        public Product findOne(String paramName, Object paramValue) {
            RowMapper<Product> rowMapper = new RowMapper<Product>(){
    
                public Product mapRow(ResultSet rs, int rowNum) throws SQLException {
    
                    long productId = rs.getLong("product_id");
                    // Other properties
    
                    Product product = new Product(...);
    
                    return product;
                }
    
            };
    
            StringBuilder queryText = new StringBuilder();
            queryText.append("select * from ");
            queryText.append(TABLE_NAME);
            queryText.append(" where ");
            queryText.append(paramName);
            queryText.append("=?");
    
            Product fetchedObject = jdbcTemplate.queryForObject(queryText.toString(), rowMapper, paramValue);
    
            return fetchedObject;
        }
    
    
        // Other CRUD methods
    }
    

    Ass you can see in all examples you don’t need explicitly specify parameter type, you just add it as Object parameter.

    If you will work with direct JDBC in such case I recommend you to use PreparedStatement and it’s setObject(..) method. Query text will be similar to shown in the example with JdbcTemplate.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I want to count how many characters a certain string has in PHP, but
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.