Solved

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

Posted on 2002-04-02
2
327 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL  on Tomcat 8 68
login form jsp example 2 37
SequenceInputStream example 3 19
Setup GlassFish 4 21
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

856 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