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

  • Home
  • SEARCH
  • 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 8176137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:02:53+00:00 2026-06-06T23:02:53+00:00

I have this weird problem in java when trying to fetch records from MYSql

  • 0

I have this weird problem in java when trying to fetch records from MYSql database by using the limit function in the query. Not sure what went wrong or did wrong, this query is giving me a hard time.

Issue – When I run this query through my java program it returns all the records and not limiting the records to 10 as given in the limit.

The same query when ran in MYSql command line, it execute very well and fetches me only 10 recrods.

Below is the java code and query. Any help or support is appreciated.!

Java code –

public UserVO getApplUserDetailsList(UserVO userVO) throws CAPDAOException {

     List<UserVO> returnList = null;
     String methodName = "getApplUserDetails()";
     Session session = null;
     String queryString = null;
     Transaction transaction = null;
     PreparedStatement ps = null;
     ResultSet rs = null;

     if(userVO == null)
     {
         logger.writeToTivoliAlertLog(className, CAPConstants.ERROR, methodName, null, "userVO returned null. Busines validation error.!", null);
         throw new CAPDAOException("userVO returned null. Busines validation error.!",CAPException.BUSINESS_VALIDATION_ERROR_SECURITY);
     }

      try {

         returnList = new ArrayList<UserVO>();

         System.out.println("");
         String appusr = userVO.getAppUsrNm(); 
         session = getSession();
         transaction = session.beginTransaction();

          if(userVO.getAppUsrRoleCd()!=null && !userVO.getAppUsrRoleCd().trim().equalsIgnoreCase(CAPConstants.DEFAULT_DROPDOWN_VALUE)){

              queryString = "SELECT " +
                                "APPL_USR_ID,APPL_USR_NM,APPL_USR_FRST_NM, " +
                                "APPL_USR_LST_NM,ACCESS_ROLE_CD " +
                                "FROM APPL_USR " +
                                "WHERE " +
                                "APPL_USR_NM LIKE ?"+
                                " AND APPL_USR_FRST_NM LIKE ?"+
                                " AND APPL_USR_LST_NM LIKE ?"+
                                " AND ACCESS_ROLE_CD = ?"+
                                " AND APPL_USR_ID != ?";

                ps = session.connection().prepareStatement(queryString);

                ps.setString(1,userVO.getAppUsrNm()+CAPConstants.PERCENTILE_SYMBOL);
                ps.setString(2,userVO.getAppUsrFirstNm()+CAPConstants.PERCENTILE_SYMBOL);
                ps.setString(3,userVO.getAppUsrLastNm()+CAPConstants.PERCENTILE_SYMBOL);
                ps.setString(4,userVO.getAppUsrRoleCd());
                ps.setInt(5, 1);

          }
          else
          {
              queryString = "SELECT " +
                        "APPL_USR_ID,APPL_USR_NM,APPL_USR_FRST_NM, " +
                        "APPL_USR_LST_NM,ACCESS_ROLE_CD " +
                        "FROM APPL_USR " +
                        "WHERE " +
                        "APPL_USR_NM LIKE ?"+
                        " AND APPL_USR_FRST_NM LIKE ?"+
                        " AND APPL_USR_LST_NM LIKE ?"+
                        " AND APPL_USR_ID != ?";

        ps = session.connection().prepareStatement(queryString);

        ps.setString(1,userVO.getAppUsrNm()+CAPConstants.PERCENTILE_SYMBOL);
        ps.setString(2,userVO.getAppUsrFirstNm()+CAPConstants.PERCENTILE_SYMBOL);
        ps.setString(3,userVO.getAppUsrLastNm()+CAPConstants.PERCENTILE_SYMBOL);
        ps.setInt(4, 1);

          }

          if(userVO.getQueryAction()!=null && userVO.getQueryAction().equals(CAPConstants.GET_DATA))
          queryString += " ORDER BY APPL_USR_ID LIMIT " + userVO.getPAGE_MIN_LIMIT() + ", " + userVO.getPAGE_MAX_LIMIT();
          else
              queryString += " ORDER BY APPL_USR_ID";

         rs = ps.executeQuery();

         if(userVO.getQueryAction()!=null &&         userVO.getQueryAction().equals(CAPConstants.GET_DATA))
         {

            int tempCOunt = 0;

             while(rs!=null && rs.next())
             {
                 tempCOunt ++;

                 UserVO returnVO = new UserVO();

                 returnVO.setAppUsrId(rs.getInt("APPL_USR_ID"));
                 returnVO.setAppUsrNm(rs.getString("APPL_USR_NM"));
                 returnVO.setAppUsrFirstNm(rs.getString("APPL_USR_FRST_NM"));
                 returnVO.setAppUsrLastNm(rs.getString("APPL_USR_LST_NM"));

                 if (rs.getString("ACCESS_ROLE_CD")!=null && rs.getString("ACCESS_ROLE_CD").trim().equalsIgnoreCase(CAPConstants.ADMINISTRATOR_ROLE_CD))
                         returnVO.setApplicationLevelRole("Administrator");
                 else if (rs.getString("ACCESS_ROLE_CD")!=null && rs.getString("ACCESS_ROLE_CD").trim().equalsIgnoreCase(CAPConstants.MAINTAINER_ROLE_CD))
                     returnVO.setApplicationLevelRole("Maintainer");
                 else if (rs.getString("ACCESS_ROLE_CD")!=null && rs.getString("ACCESS_ROLE_CD").trim().equalsIgnoreCase(CAPConstants.VIEWER_ROLE_CD))
                     returnVO.setApplicationLevelRole("Viewer");
                 else
                     returnVO.setApplicationLevelRole("None");

                 returnList.add(returnVO);

             }

             System.out.println("Count >>>>>>>>>>>>>>>>>>> "+tempCOunt);

             userVO.setReturnListFromDB(returnList);
         }
         else
         {

            int rowcount = 0;
            if (rs.last()) {
              rowcount = rs.getRow();
              rs.beforeFirst(); // not rs.first() because the rs.next() below will move on, missing the first element
            }

            userVO.setTotalRecordCount(rowcount);

            System.out.println("Total count of the records to be used for pagination >> "+rowcount);

            rowcount = 0;

             while(rs!=null && rs.next())
             {
                 rowcount ++;
                 UserVO returnVO = new UserVO();

                 returnVO.setAppUsrId(rs.getInt("APPL_USR_ID"));
                 returnVO.setAppUsrNm(rs.getString("APPL_USR_NM"));
                 returnVO.setAppUsrFirstNm(rs.getString("APPL_USR_FRST_NM"));
                 returnVO.setAppUsrLastNm(rs.getString("APPL_USR_LST_NM"));

                 if (rs.getString("ACCESS_ROLE_CD")!=null && rs.getString("ACCESS_ROLE_CD").trim().equalsIgnoreCase(CAPConstants.ADMINISTRATOR_ROLE_CD))
                         returnVO.setApplicationLevelRole("Administrator");
                 else if (rs.getString("ACCESS_ROLE_CD")!=null && rs.getString("ACCESS_ROLE_CD").trim().equalsIgnoreCase(CAPConstants.MAINTAINER_ROLE_CD))
                     returnVO.setApplicationLevelRole("Maintainer");
                 else if (rs.getString("ACCESS_ROLE_CD")!=null && rs.getString("ACCESS_ROLE_CD").trim().equalsIgnoreCase(CAPConstants.VIEWER_ROLE_CD))
                     returnVO.setApplicationLevelRole("Viewer");
                 else
                     returnVO.setApplicationLevelRole("None");

                 returnList.add(returnVO);

                 System.out.println("Row count >>"+rowcount);

                 if(rowcount == CAPConstants.PAGINATION_MAX_VALUE)
                     break;

             }

             rowcount = 0;

             userVO.setReturnListFromDB(returnList);

         }



         System.out.println("returnList >>"+returnList);


         return userVO;

       } catch (Throwable e) {
           e.printStackTrace();
            logger.writeToTivoliAlertLog(className, CAPConstants.ERROR, methodName, userVO.getAppUsrNm(), "Error occured while trying to fetch application user details. Printing stack trace to the log for analysis..", e);
            throw new CAPDAOException("Error occured while trying to fetch application user details.",CAPException.SPEXECUTION_ERROR_CODE);

       }
      finally{

          closeTransactionAndSession(session,transaction);

      }

}

MYSQL Query –

SELECT APPL_USR_ID,APPL_USR_NM,APPL_USR_FRST_NM, APPL_USR_LST_NM,ACCESS_ROLE_CD 
FROM APPL_USR WHERE APPL_USR_NM LIKE '%' 
       AND APPL_USR_FRST_NM LIKE '%' 
       AND APPL_USR_LST_NM LIKE '%' 
       AND APPL_USR_ID != 1 
ORDER BY APPL_USR_ID 
LIMIT 10, 10
  • 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-06T23:02:56+00:00Added an answer on June 6, 2026 at 11:02 pm

    You are changing the querystring after you have prepared the statement with the string.

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

Sidebar

Related Questions

I have weird problem. I'm using Eclipse for writing J2EE (java, jsf, javascript) application
I have this weird problem with setting up cookies with PHP. Everything worked fine
This is a weird problem. I have a solution for it, but I don't
I have a weird problem. I create this window, but its blank until i
I have a weird problem, and I don't know if this is the default
Ok, this one is weird, i have this code: $('#nps').submit(function(e) { e.preventDefault(); var images
I have been developing a Java application using J2EE and a Derby database. My
I have a weird problem. While running my Android application, I receive Exception: java.lang.ClassCastException:
i have a weird problem, i've been following a youtube tutrial from Matt Blagden
I'm trying to embed groovy in java application and got a weird problem. Let's

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.