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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:23:42+00:00 2026-06-18T12:23:42+00:00

I am processing following code for resolving login credentials from servlet. import java.io.IOException; import

  • 0

I am processing following code for resolving login credentials from servlet.

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

@WebServlet("/login")
public class oneServlet extends HttpServlet {

public static Connection getConnection() throws Exception {

    String driver = "org.postgresql.Driver";
    String url = "jdbc:postgresql://10.1.11.112:5432/pack";
    String username = "pack";
    String password = "pack";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String user=request.getParameter("t1");
    String pass=request.getParameter("t2");

    System.out.println("done 1");

    Connection conn = null;
    PreparedStatement pstmt = null;

    System.out.println("done 2");

    try {
         conn = getConnection();
         String queryTest = "select username,password from login ";
         pstmt = conn.prepareStatement(queryTest);

         System.out.println("done 3");

         ResultSet rs = pstmt.executeQuery();

         System.out.println("done4");

         while (rs.next()) {

         System.out.println("done5");    

         String username=rs.getString(1);
         String password=rs.getString(2);    

         if(user.equals(username) && pass.equals(password))
         {
             response.sendRedirect("LoginSuccess.jsp");
             return;

         }  
         else
         {
            JOptionPane.showMessageDialog(null, "retry");
         }
         }
         }

     catch (Exception e) {
        e.printStackTrace();
    }

    finally {
        try {
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
}

the problem i am facing is that “while (rs.next())” is iterating and showing output for every username available in table , but i need it to be displayed only once , either redirect or retry. Any suggestion are appreciated.

  • 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-18T12:23:43+00:00Added an answer on June 18, 2026 at 12:23 pm

    You’re unnecessarily copying the entire DB table into Java’s memory instead of writing the SQL query in such way that the DB returns exactly the row you’re looking for, so that you can just get away with if (resultSet.next()).

    Make use of the SQL WHERE clause.

    String query = "select username from login where username=? and password=?";
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    
    try {
        connection = getConnection();
        statement = connection.prepareStatement(query);
        statement.setString(1, user);
        statement.setString(2, pass);
        resultSet = statement.executeQuery();
    
        if (resultSet.next()) {
            response.sendRedirect("LoginSuccess.jsp");
        } else {
            request.setAttribute("message", "retry");
        }
    } catch (SQLException e) {
        throw new ServletException("DB interaction failed", e);
    } finally {
        if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {}
        if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
        if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
    }
    

    Note that I also fixed the insane way of displaying a message and fixed the exception handling and the closing of DB resources. The JOptionPane would only display the message to webserver’s screen, not to webbrowser’s screen, which would of course fail in real production environment when they do not run at physically the same machine. I strongly recommend to stop reading roseindia.net tutorials. That site is cluttered of bad practices like as exposed in your code.

    See also:

    • Our Servlets wiki page – contains a Hello World example with validation
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code is used to fetch logs from app engine for further processing.
I was following some Java code to do image processing on bitmaps and found
I'm processing xml in Java and I have the following code: dbf.setValidating(false); dbf.setIgnoringComments(false); dbf.setIgnoringElementContentWhitespace(true);
The following code adds tasks that perform some processing on files from the blobstore,
I have the following code, that inserts the processing instructions before root element: Document
I do not see any improvements in processing speed using the following code: IEnumerable<Quote>
I have the following code: for (i=1;i<=pages;i++) { $('#procout').append(Processing Page#+i+<br />); $.ajax({ url: 'processor.php',
I have the following async queue processing routing. var commandQueue = new BlockingCollection<MyCommand>(); commandQueue
I have the following log entry that I am processing in PowerShell I'm trying
I know the following function can be used to do some pre-save processing. But,

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.