Results 1 to 2 of 2
  1. #1
    user07 is offline Member
    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Default Servlet and jdbc connectivity

    I am trying to create a login page asking user for username and password, once submitted i am passing this info to servlet which will connect to database and check if login id and password is correct or not... here is my servlet code:
    Java Code:
    //servlet code
    import java.io.*;
    import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.*;
    
    @WebServlet(name = "GreetingServlet", urlPatterns = {"/GreetingServlet"})
    public class GreetingServlet extends HttpServlet {
    
        public void doGet (HttpServletRequest req, HttpServletResponse res)
                throws ServletException, IOException {
            String username = req.getParameter("user");
            String password = req.getParameter("password");
    
            res.setContentType("text/html");
            try {
                Connection con;
                ResultSet rs;
                Statement st;
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con = DriverManager.getConnection("jdbc:odbc:login_database");
                st = con.createStatement();
                PrintWriter p = res.getWriter();
                int flag = 0;
                rs = st.executeQuery("select * from login");p.println("<html><body>");
                while (rs.next()) {
                 if (username.equals(rs.getString("name")))  
                   {      flag = 1;     
                        p.println("<b><h1>hello " + rs.getString(1)+"</h1></b>");
                        
                    }
                    
                   p.println("</body></html>");
                        p.flush();
                
                }
               if (flag == 0) {
                    p.println("<html><body bgcolor=red>");
                    p.println("<b><h1>incorrect login ");
                    p.println("</h1></b></body></html>");
                    p.flush();
               
            }
            } catch (SQLException e) {
                System.out.println("error" + e.toString());
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
    }
    XML Code:
    <html>
    <head> 
    <title> Game Login</title> 
    </head>
    <body>
    <form method="get" action="/WebApplication1/GreetingServlet">
    <p>UserName  :<input type="text" size="40" name="name"/></p>
    
    <p>Password :<input type="password" size="40" name="password"/></p>
    <input type=submit value="submit">
    <br> <a href="register.html"> Register </a>
    <br> <a href="forgot_pass.html"> Forgot password</a>
    </form>
    </body>
    </html>
    I know i should be using post instead of get ...but i was having some problem in using that that i ll resolve later... as of now now my problem is that the above code is conneccting to database but not giving the desired output... ie if i remove line
    " if (username.equals(rs.getString("name"))) "
    (in while statement) it is giving names of all users in database ... but no output otherwise... please help...

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Servlet and jdbc connectivity

    First off, the query for a login should be:
    Java Code:
    select <whatever fields you need>
    from login
    where name = <supplied name>
    and password = <supplied password>
    Get the database to do the job of checking the values.
    It's better at it.

    Then you simply need to know whether rs.next() is true.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. JDBC Connectivity in netbeans
    By juhiswt in forum New To Java
    Replies: 4
    Last Post: 04-01-2011, 02:04 PM
  2. Jdbc and servlet
    By ulferik in forum New To Java
    Replies: 1
    Last Post: 03-26-2011, 09:01 AM
  3. Servlet JDBC
    By kevinnrobert in forum Java Servlet
    Replies: 1
    Last Post: 04-02-2010, 01:27 PM
  4. jdbc connectivity error
    By OMFGITSROHIT in forum JDBC
    Replies: 2
    Last Post: 03-16-2010, 05:10 PM
  5. Connectivity between Servlet and Midlet
    By adarsh_2484 in forum New To Java
    Replies: 1
    Last Post: 07-29-2008, 04:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •