Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / February 2005

Tip: Looking for answers? Try searching our database.

jdbc query works only with parameters given in command line

Thread view: 
magda muskala - 15 Feb 2005 04:02 GMT
hi,
my problem is, that while trying to call the authorize method [from
class Authorizer] in doPost method [class login_page] , it returns
always false.

when i print the query i execute it is correct and works well in mysql
console.

tried also to call the same method from command line[i.e.  in both
main methods]
and it the query is executed correctly. the authorize method not
always returns false]. any clue?
tia
magda muskala

and this is the code:
public class MysqlWeghausConnection
{
   public Connection getConnection()
   {
         try
       {
                // The newInstance() call is a work around for some
                // broken Java implementations
   
                   
Class.forName("com.mysql.jdbc.Driver").newInstance();
            }
       catch (Exception e) {
            //     handle the error
       e.printStackTrace();
    }
       try
       {
           con = DriverManager.getConnection("jdbc:mysql://localhost/ect");
       }
       catch(SQLException e)
       {
 System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
   e.printStackTrace();
       }
       return con;
       
           
   }
   //Ein java.sql.PreparedStatement, zur Interaktion mit der
Datenbank.
   protected transient Statement s;
   //Ein java.sql.ResultSet für die Ergebnisse der doQuery() Methode.
   protected transient ResultSet rs;
   public Connection con;
   //Gibt die Datenbankverbindung frei.


   public void doQuery()
   {
       try
       {
           rs= s.executeQuery(q);
       }
       catch(Exception e)
       {
           e.printStackTrace();
       }
           
   }
   public ResultSet getResult()
   {    
       return rs;
   }
   //Gibt das SQL Statement als String zurück, welches durch Aufrufen
von doQuery() ausgeführt werden kann.

   public String getQuery()
   {
       return q;
   }
   private String q = new String("");
   public void setQuery(String q)
   {
       this.q=q;
       try
       {
           s = getConnection().createStatement();
       }
       catch(Exception e)
               {
                       e.printStackTrace();
               }
   }
}



public class login_page extends HttpServlet
{



    /** Processes requests for HTTP <code>POST</code>.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request,
HttpServletResponse response)
    throws ServletException, IOException
   {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String strResult = "";

        String strUserName = request.getParameter("userid");
        String strPassword = request.getParameter("userpassword");
        if(!(strUserName == null)  
        && !(strPassword == null)  )
       {    
           //out.println(strUserName);
            //valid user create the session and give proper msg
            Authorizer auth = new Authorizer();
           out.println(auth.authorize(strUserName,strPassword));out.println(auth.getS());out.println(auth.size);
           if(auth.authorize(strUserName,strPassword))
           {
               strResult = "Login successful!!!!!";
                HttpSession objSession = request.getSession(true);
//true - create one if ther s none
                objSession.setAttribute("loginname", strUserName); //
keeps the username name in the session till the
                response.sendRedirect("/weghaus/servlet/UploadServlet");
               // session is closed.(using the method invalidate().
            }
       }
       else
       {
            strResult = "Invalid User !!";
        }

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet</title>");
        out.println("</head>");
        out.println("<body>");
       out.println(strResult);
        out.println("</body>");
        out.println("</html>");

        out.close();
    }

   public static void main(String[] arg)
       {
               Authorizer a = new Authorizer();
               System.out.println(a.authorize(arg[0],arg[1]));
               System.out.println(a.size);
       }
}



import java.sql.*;
public class Authorizer  
{
private String s="";
public int size;
   public String getS()
   {
       return s;
   }
   public boolean authorize(String login, String passwd)
   {
       MysqlWeghausConnection mwc = new MysqlWeghausConnection();
       mwc.getConnection();
       mwc.setQuery("select * from admin where admin_login=@login and
admin_passwd=@passwd");
       mwc.doQuery();
       boolean ret=false;
       try
       { size=mwc.getResult().getMetaData().getColumnCount();
           if (mwc.getResult().getMetaData().getColumnCount()>0)
           {
               for (int i =0; i<=
mwc.getResult().getMetaData().getColumnCount(); i++)
                       {
                   System.out.println(mwc.getResult().getString(i));
                   System.out.println("Login success");
                       }
               ret = true;
               return true;
           }
           
           else
           {
               System.out.println("Login failed!");
               //return false;
           }
       }
       catch(Exception e)
       {
           e.printStackTrace();
       }
       return ret;    
   }    
   public static void main(String[] arg)
   {
       Authorizer a = new Authorizer();
       System.out.println(a.authorize("admin","passwd"));
       System.out.println(a.size);
   }
}
Chuck Simpson - 15 Feb 2005 16:34 GMT
...
> size=mwc.getResult().getMetaData().getColumnCount();
>             if (mwc.getResult().getMetaData().getColumnCount()>0) {
[quoted text clipped - 13 lines]
>                 //return false;
>             }
...

Change the code fragment above (from Authorizer.authorize method) to:

 ResultSet rs = mwc.getResult();
 if(rs != null) {
   return  rs.next();
 }
 return false;

If ResultSet is null then the query was not successful, i.e. the query
failed with an Exception, so return false. Otherwise the query succeeded
and rs.next() will return true if the ResultSet contains one or more rows
and it will return false if there are no rows in the ResultSet.

Chuck


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.