NewBee
NewBee

Reputation: 839

Not processing desired output through while (rs.next())

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.

Upvotes: 1

Views: 2250

Answers (5)

rahul
rahul

Reputation: 393

Change your query to

String queryTest = "select username,password from login where username = ?";
pstmt = conn.prepareStatement(queryTest); 
pstmt.setString(1,user);

UPDATE: A better solution would be by not getting the password from your DB(for security issues) rather get only the data which is required.

Upvotes: 1

BalusC
BalusC

Reputation: 1108642

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:

Upvotes: 4

Meherzad
Meherzad

Reputation: 8553

The query which you are running is not for any specific user you are querying for each and every user. I think so you have forgot to add the username and password in query.

Though if this is your requirement to iterate it one time replace the while with if to iterate only once

if (rs.Next){

       //your code

}

Upvotes: 0

Jean Logeart
Jean Logeart

Reputation: 53809

Make your query so that you select only the results having the right username and password:

String queryTest = "select username,password from login where username=? and password=?";
PreparedStatement pstmt = conn.prepareStatement(queryTest);
pstmt.setString(1, user);    
pstmt.setString(2, pass);

Upvotes: -1

MiSu
MiSu

Reputation: 29

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");
     }
break;
     }

This is not a correct implementation but should solve your problem.

Upvotes: -1

Related Questions