Solved

Servlets and Database (if(rs.isBeforeFirst()))

Posted on 2002-04-02
2
293 Views
Last Modified: 2013-11-24
The if(rs.isBeforeFirst()) methods cannot be found, so the login page always the false statement even if the customer exist. I tried to use if(rs.next()) as suggested but the problem still remains.

Assistance required with examples

Thank You
0
Comment
Question by:wbmorapedi
2 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6913461
what's the error message when you try rs.next() ?
0
 
LVL 3

Accepted Solution

by:
msterjev earned 70 total points
ID: 6913464
You alredy own me two unawarded questions. This is my last try:

I check this code and it works fine on tomcat:

HTML form:

<html>
<body>
<form METHOD=POST Action="servlet/LoginServlet">
USERNAME:<INPUT TYPE=TEXT Name="username"><BR>
PASSWORD<INPUT TYPE=PASSWORD NAME="password"><BR>
<INPUT TYPE=SUBMIT VALUE="LOGON">
</form>
</body>
</html>


LoginServlet.java

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


public class LoginServlet extends HttpServlet
{
   
   
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
    {
         Connection con=null;
         Statement stmt=null;
         ResultSet result=null;
         String username,password,stmtSQL;
         boolean logged=false;
         PrintWriter out=res.getWriter();
         res.setContentType("text/html");
         username=req.getParameter("username");
         password=req.getParameter("password");
         try
         {
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              con=DriverManager.getConnection("jdbc:odbc:Imenik");
              stmt=con.createStatement();
              stmtSQL="Select * From Users Where username='"+username+"' and password='"+password+"'";
              result=stmt.executeQuery(stmtSQL);
              if(result.next())
                   logged=true;
              else
                   logged=false;
              stmt.close();
              con.close();
              con=null;
         }
         catch(Exception e)
         {
              e.printStackTrace();
         }
         finally
         {
              if(con!=null)
              {
                   try
                   {
                        con.close();
                   }
                   catch(Exception ignored){}
              }
         }
         if(logged)
              res.sendRedirect("http://localhost:8080/examples");
         else
              res.sendRedirect("http://localhost:8080/Log/index.html");
         
    }
}
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now