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.


<%@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;
           String UNAME=rs.getString("User_Name");      
                             {  flag=0;}
                   out.println("Username already exists!!!");
                   else                                  {
   PreparedStatement ps1=conn.prepareStatement("INSERT into Login values(?,?)");
     ps1.setString(2, pwd);
    out.println("Registered Successfully");



        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <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>

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)