0

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>
3
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by peter_budo
0

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

0

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

0

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..

0

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!! :|

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.