<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<%@ page language="java" session="true" %>
<html>
<body>
<%
		//Getting the text values from create login page
		String lid   = request.getParameter("loginid");
		String lpin  = request.getParameter("loginpin");
   
        String loginid  = lid.trim();
	    String loginpin = lpin.trim();
         
        out.println(loginid);
		out.println(loginpin);         // Connect to Database to retrive all the login id created and check if the Id already exist if it exist then redirect to login page again
		java.sql.Connection con;
        java.sql.Statement s;
        java.sql.ResultSet rs , rs1;
        
		
	    con=null;
        s=null;
        rs=null;
		rs1=null;

        String id  =  null;
		String pin =  null;
		
		int i=0;
   
        //Connecting Connetion String - passing the string to connect to db
		String connectionURL = "jdbc:sqlserver://localhost:1433;databaseName=Admissions;integratedSecurity=true;selectMethod=cursor";
		try{

               Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
               con = java.sql.DriverManager.getConnection(connectionURL);
              // out.println("1");
           }
		catch(ClassNotFoundException cnfex)
		{
               cnfex.printStackTrace();
		}

		String sql = "select * from Login where userID='"+loginid+"'";
		try{
				s = con.createStatement();
				rs = s.executeQuery(sql);
		while( rs.next() )
		{
		     pin = rs.getString("userPin");
			 id  = rs.getString("userID");
			 //i++;
		}

		
		out.println(id);
                                out.println(pin);
		if(loginid.equals(id) && loginpin.equals(pin))
		{
		
		  out.println("login successful");
		
        }
		else
		{
           out.println("incorrect username/password combination");
		}
		}
    	catch(Exception e){e.printStackTrace();}
		finally{
		if(rs!=null) rs.close();
		if(s!=null) s.close();
		if(con!=null) con.close();
		}

%>
</body>
</html>

When I execute this code its not allowing valid login and password.
i tried printing the values to see if its comparing wrong values but the same values are being displayed. i think the if statement is not working fine. does any one know what could be the problem with the if statement or any other possible problem,....

Hello thank you so much for your suggestion ...I really appreciate it .. I went through the MVC example ,but i do not know java beans and servlets . More over i am very curious to know whats wrong with the code i wrote here

Hello,

Just try to use trim() for id and pin i.e when retrieving from database and then compare with loginid and loginpin.

I hope it solves the problem.

This question has already been answered. Start a new discussion instead.