Sir, I am not getting any fault in my code but the code is not executing with the demands which I have.
I just want to register 2 values into the database using jsp , if the username is present in the database it will show User already exist otherwise it will insert the values but its not working. please help me out.

JSP CODE

<%@page import="java.sql.*" %>
<% Class c = Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:inventory","sa","12345");
String uname = request.getParameter("username");
String pwd = request.getParameter("password"); 
PreparedStatement ps = conn.prepareStatement("SELECT * from Login");
ResultSet rs=ps.executeQuery();
int flag = 1;
while(rs.next())
 {
           String UNAME=rs.getString("User_Name");      
            if(uname.equalsIgnoreCase(UNAME))
                             {  flag=0;}
  }
               if(flag==0)
                   out.println("Username already exists!!!");
                   else                                  {
   PreparedStatement ps1=conn.prepareStatement("INSERT into Login values(?,?)");
     ps1.setString(1,uname);
     ps1.setString(2, pwd);
     ps1.executeUpdate();
    out.println("Registered Successfully");
       }

%>

HTML CODE

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="register.jsp" method="GET">
            <p>Enter Username : <input type="text" name="username" value="" /></p>
            <p>Enter your Password : <input type="password" name="password" value="" /></p>
            <p><input type="submit" value="Register Now" /></p>
        </form>
    </body>
</html>

Recommended Answers

All 5 Replies

dear deba0011,
what kind error you getting?
U missed the try,catch block statements that May be problem....

the code is not working as it should. I am getting duplicate username entries in the database.

instead of getting all values from db just get single value like

SELECT User_Name from Login where User_Name=uname

better U can assign User_Name primary key or unique know..

dear deba0011,
what kind error you getting?
U missed the try,catch block statements that May be problem....

I tried it with the try catch block as well but still its the same!! :|

1. Move DB connectivity to servlet where it belongs, not page view as you have it (JSP database connectivity according to Model View Controller (MVC) Model 2)
2. Use try/catch there are provided for a reason
3. Query for a user name not whole list of users, you just slowing down whole process (imagine you have million of records to check, your current approach could crash server with large number of user trying to register)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.