Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > jdbc query works only with parameters given in command line

Reply
Thread Tools

jdbc query works only with parameters given in command line

 
 
magda muskala
Guest
Posts: n/a
 
      02-15-2005
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);
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to parse the jdbc driver name from the jdbc .jar file Bruce Java 4 03-25-2006 12:01 PM
stand-alone JMS, other JDBC operations, and transactions ( ActiveMQ + JOTM + JDBC operations ) Jesus M. Salvo Jr. Java 2 02-11-2006 06:33 PM
oracle.jdbc.OracleDriver vs oracle.jdbc.driver.OracleDriver Betty Java 1 05-21-2005 05:15 PM
Re: jdbc help:sun.jdbc.odbc.JdbcOdbcDriver Keith Wansbrough Java 0 08-16-2004 07:31 PM
Servlet parameters different from the command line parameters? Jonck van der Kogel Java 2 05-26-2004 11:34 PM



Advertisments