#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Hyderabad, India
    Posts
    19
    Rep 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.

    /*
    * 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

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    46
    Rep Power
    14
    I think this may solve your problem (from the Java API docs.):

    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Hyderabad, India
    Posts
    19
    Rep Power
    0

    Thumbs up


    Thanx a ton. Your solution works

    Ramesh

IMN logo majestic logo threadwatch logo seochat tools logo