Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

920 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

16 Experts available now in Live!

Get 1:1 Help Now