jdbc query works only with parameters given in command line

Discussion in 'Java' started by magda muskala, Feb 15, 2005.

  1. 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);
    }
    }
    magda muskala, Feb 15, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonck van der Kogel
    Replies:
    2
    Views:
    966
    Jonck van der Kogel
    May 27, 2004
  2. Lord0
    Replies:
    1
    Views:
    551
    Thomas Weidenfeller
    Apr 19, 2006
  3. chiara
    Replies:
    6
    Views:
    457
    Barry Schwarz
    Oct 6, 2005
  4. gry
    Replies:
    2
    Views:
    706
    Alf P. Steinbach
    Mar 13, 2012
  5. Henry Leyh
    Replies:
    14
    Views:
    130
    Henry Leyh
    May 16, 2013
Loading...

Share This Page