Topic
  • 1 reply
  • Latest Post - ‏2010-01-15T02:05:44Z by bhupp
SystemAdmin
SystemAdmin
210 Posts

Pinned topic error in the code

‏2009-10-25T08:03:56Z |
the following progrm is giving the error:
column index out of range.
what is the error.??

the program is:

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;

public class Login extends HttpServlet
{
static int count=0;

public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
{
PrintWriter pw=res.getWriter();
pw.println("<html>");
pw.println("<title> Online Test Conducting System </title>");
pw.println("<body>

");
pw.println("

<center><marquee>Online Test Conducting System</marquee></center>

");
pw.println("

");
pw.println("<hr color=red/>");
pw.println("

<center>");
pw.println("<form method=post>");
pw.println("Username: <input type=text name=username>");
pw.println("

");
pw.println("Password: <input type=password name=passwd>");
pw.println("

");
pw.println("New User? ");
pw.println("<input type=submit value=Submit>");
pw.println("</form></center></body></html>");
}

public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
{
String user=req.getParameter("username");
String pass=req.getParameter("passwd");
PrintWriter pw=res.getWriter();

while(count!=4)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/onlinetst","root","root");
Statement s=con.createStatement();
//String sql="select * from login";
String sql="Select username FROM login "+"WHERE username='"user"'"+ "AND passwrd='"pass"'";
ResultSet rs=s.executeQuery(sql);

while(rs.next())
{
String s1=rs.getString(1);
String s2=rs.getString(2);

if((s1.equals(user))&&(s2.equals(pass)))
{
RequestDispatcher rd=req.getRequestDispatcher("Welcome");
rd.forward(req,res);
}

else
{
pw.println("<BODY>");
pw.println("<FORM method=get>");
pw.println("Invalid username or password");
pw.println("</form></body>");
}

rs.close();
s.close();
con.close();
}

rs.close();
s.close();
con.close();
}

catch(ClassNotFoundException e)
{
System.out.println(e.toString());
}

catch(SQLException e)
{
System.out.println(e.toString());
}

catch(Exception e)
{
System.out.println(e.toString());
}
}
count++;
};
}
Updated on 2010-01-15T02:05:44Z at 2010-01-15T02:05:44Z by bhupp
  • bhupp
    bhupp
    1 Post

    Re: error in the code

    ‏2010-01-15T02:05:44Z  
    your sql statement: select username from login - you only get 1 column back in the resultset. so, String s2=rs.getString(2) is out of range.