Dear Sir/Madam,
I am trying to delete the one user data in the Oracle SQL server database by using Servlet program (Tomcat server).
In Database table have 6 users details are there.
Table name is â??JDUSERâ?? and columns are USERS (Char), PASS (Varchar2) and CODE (Varchar2). I am trying to delete 4th user details.
While loop is not terminate, in while loop why If condition not working it checking until end of the table and gives else black statement. So what is my mistake and please give the solution to me.
My HTML code is:
<html> <title> Delete Page</title> <form action="JdbcDeleteServlet" method="post"> <center><font color='red'> <h2> Remove Data</h2><br/></font> User Name : <input type="text" name="uname" ><br/><br/> Password : <input type="password" name="pwd"><br/><br/> Verification code : <input type="text" name="code"><br/><br/> <input type="submit" value="Delete"/> </center> </form> </html> My Database table is: My JavaServlet Program is: import java.lang.*; import java.io.*; import java.sql.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class JdbcDeleteServlet extends HttpServlet { String dbn,dbp,dbco; String un,up,uco; boolean flag=false; public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { try { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); response.setContentType("text/html"); un=request.getParameter("uname"); up=request.getParameter("pwd"); uco=request.getParameter("code"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection c=DriverManager.getConnection("jdbc:odbc:hari","raman","raman"); Statement s=c.createStatement(); s.execute("select *from jduser"); ResultSet rs=s.getResultSet(); while(rs.next()) { dbn=rs.getString("users"); dbp=rs.getString("pass"); dbco=rs.getString("code"); if(un.equals(dbn)&&up.equals(dbp)&&uco.equals(dbco)) // another formate if((un==dbn)&&(up==dbp)&&(uco==dbco)) { s.executeUpdate("delete jduser where users='"+un+"'"); flag=true; break; } } if(flag==true) out.println("<font color=green>"+dbn+" user Data deleted successfully"); else out.println("<font color=red>Sorry "+un+" user data is Not Available"); s.close(); c.close(); out.close(); } catch(IOException e) { System.out.println(e.getMessage()); } catch(SQLException e) { System.out.println(e.getMessage()); } catch(ClassNotFoundException e) { System.out.println(e.getMessage()); } } }
So finally where is my mistake and in while loop why if loop not working. please give me solution. It is Urgent...
When you retrieve the data from the Database by using Servlet (JDBC).
you entered 'ramanujan'
is 9 character but in the database 'ramanujan '
is 15 characters because remaining letters it consider as spaces so you use trim() then spaces are trimmed. it will working good.
trim( )
The trim( ) method returns a copy of the invoking string from which any leading and trailing whitespace has been removed. It has this general form:
String trim( )
Here is an example:
String s = " Hello World ".trim();
This puts the string â??Hello Worldâ??
into s.
while(rs.next()) { dbn=rs.getString("users"); dbp=rs.getString("pass"); dbco=rs.getString("code"); dbn=dbn.trim(); dbp=dbp.trim(); dbco=dbco.trim(); if(un.equals(dbn)&&up.equals(dbp)&&uco.equals(dbco)) { s.executeUpdate("delete jduser where users='"+un+"'"); flag=true; break; } }