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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:16:54+00:00 2026-05-31T17:16:54+00:00

Instead of writing too many sql statements in each servlet I just want to

  • 0

Instead of writing too many sql statements in each servlet I just want to write one sql statement in a java class and want to call that class from each servlet but I have not found the correct way to do so.

The sql statement is: select distinct username from table where userid=?

while (rs.next){
    username=rs.getString(1);
}

I want to return username to each servlet
so I created a java class in netbeans:

public class Username {
     //How should I proceed here?        
}
  • 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-31T17:16:55+00:00Added an answer on May 31, 2026 at 5:16 pm

    Don’t embed that code in a servlet.

    Create a separate POJO that does the work for you. Write it, test it, and give a reference to any servlet that needs its services. Let the servlet call the one method that executes that logic.

    Start with an interface:

    package persistence;
    
    public inteface UserDao {
        String getUsername(Long userId);
    }
    

    Then give it an implementation:

    package persistence; 
    
    public class UserDaoimpl implements UserDao {
        public String getUsername(Long userId) {
            // All the SQL stuff here
        }
    }
    

    Your servlet will have a reference to it:

    private UserDao userDao = new UserDaoImpl();
    

    The servlet should acquire the Connection from a pool and give it to the DAO.

    If you have write operations, I’d recommend that you create a separate service layer. Services know about units of work. They’ll outlive your web UI and those servlets.

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

Sidebar

Related Questions

People keep telling me instead of writing shift 1 bit to the left, just
How can I write data to stdout instead of writing file? I'm using GPG
I'm just getting started on writing functions instead of writing everything inline. Is this
How to read n lines from a file instead of just one when iterating
I'm writing a Java library in Netbeans 7 which contains a Demo.java main class
I’m confused about this; instead of writing tweepy codes like- auth=tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret)
I have a string array of 3 different command line commands. Instead of writing
A friend of mine is programming a web page. Instead of writing any HTML
I am writing an instead of trigger for updates that does some auditing and
In a Silverlight application, instead of consuming and writing (wcf) wrappers around messages that

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.