Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 28th, 2003, 08:20 AM
vrtata vrtata is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Hyderabad, India
Posts: 19 vrtata User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 26 sec
Reputation Power: 0
Unhappy JDBC Connectivity Issue

I am trying to connect to mysql database through a servlet. This servlet tries to authenticate the user. The return value of the function authUser is always returning false.

Quote:
/*
* pLogin.java
*
* Created on May 22, 2003, 3:04 PM
*/

package vrt;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
* @author Venkat Ramesh Tata
* @1.00
* @Servlet for processing Login Screens
*/
public class pLogin extends HttpServlet {

/** Initializes the servlet.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);

}

/** Destroys the servlet.
*/
public void destroy() {

}

/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String uName = request.getParameter("userName");
String uPwd = request.getParameter("userPwd");
String tURL = request.getParameter("targetURL");

if (!authUser(uName, uPwd)) {
response.sendRedirect("../index.jsp?status=ERROR");
}
else {
HttpSession session = request.getSession();
session.setAttribute("fusion.user", uName);
session.setAttribute("fusion.auth", "Y");
String eURL = response.encodeRedirectURL(tURL);
response.sendRedirect(eURL);
return;
}

/* output your page here

*/
out.close();
}

/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Login Processing Servlet.";
}

protected boolean authUser(String uName, String uPwd)
throws ServletException, IOException {

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
boolean retval = false;

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (ClassNotFoundException e){
e.printStackTrace();
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}

try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/ATFLNET?user=intranet&password=intranet");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT count(*) FROM users WHERE userid='"+uName+"'");
int rows = rs.getInt(1);

if (rows == 1)
retval = true;
else
retval = false;

stmt.close();
rs.close();
conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
return retval;
}
}



On checking the catalina.out i see the following error

Quote:
WebappClassLoader: Resource '/WEB-INF/lib/vrt.jar' was modified; Date is now: Wed
May 28 18:25:31 IST 2003 Was: Wed May 28 18:00:39 IST 2003
java.sql.SQLException: Before start of result set
at com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:3565)
at com.mysql.jdbc.ResultSet.getString(ResultSet.java:1724)
at com.mysql.jdbc.ResultSet.getString(ResultSet.java:1815)
at vrt.pLogin.authUser(pLogin.java:115)
at vrt.pLogin.processRequest(pLogin.java:46)
at vrt.pLogin.doPost(pLogin.java:79)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)


Could someone help me with this problem. I am getting a bit confused.

Thanx

Ramesh

Reply With Quote
  #2  
Old May 28th, 2003, 08:52 AM
waldthau waldthau is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Location: Colorado
Posts: 46 waldthau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
I think this may solve your problem (from the Java API docs.):

Quote:
A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.

I think you are getting your error when you are calling rs.getInt(1) because you have not moved the cursor to the first row in the resultset. Even though you are expecting only one row in your resultset you still need to loop through it because of the initial cursor positioning.
__________________
- MW

Reply With Quote
  #3  
Old May 28th, 2003, 09:02 AM
vrtata vrtata is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Hyderabad, India
Posts: 19 vrtata User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 26 sec
Reputation Power: 0
Thumbs up

Thanx a ton. Your solution works

Ramesh

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > JDBC Connectivity Issue


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway