M
magda muskala
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);
}
}
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);
}
}