How to delete the row from the Database by using while loop in servlet

Ads
 

How to delete the row from the Database by using while loop in servlet

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

View Answers

May 18, 2011 at 6:52 PM

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;
                    }
        }

Ads









Related Tutorials/Questions & Answers:
How to delete the row from the Database by using while loop in servlet
How to delete the row from the Database by using while loop in servlet ... server database by using Servlet program (Tomcat server). In Database table.... It is Urgent...  When you retrieve the data from the Database by using
How to delete the row from the Database by using servlet
How to delete the row from the Database by using servlet  Dear Sir...: Delete row from database using servlet   In that link solution... then the user data to be delete from the database table. Assume in Database table have
Advertisements
Delete a row from database by id
Delete a row from database by id  I m creating a small application using servlet. There is a form (index.jsp)having 10 fields on submitting the form...) for "DELETE" AND "UPDATE". On clicking delete which is hyper link that particular row
delete row from a table using hibernate
delete row from a table using hibernate  //code in java file String hql="delete from CONTACT c where ID=6"; Query query=session.createQuery... [delete from CONTACT] int i=query.executeUpdate
How to display image in jsp from database using Servlet?
How to display image in jsp from database using Servlet?  Hi, How to display image in jsp from database using Servlet? Thanks   Hi, You will find code and example program at Retrieve image from database using Servlet
how to delete a row in sql without using delete command.
how to delete a row in sql without using delete command.  how to delete a row in sql without using delete command. thanks in advance
How to retrieve image from database using jsp and servlet?
How to retrieve image from database using jsp and servlet?  Hi, I am trying to find code for displaying the image from database in a JSP page. How to retrieve image from database using jsp and servlet
How to insert image in sql database from user using servlet
How to insert image in sql database from user using servlet  pls tell me accept image from user and insert image in sql server 2005 database using servlet and jsp
sqlite database delete row
sqlite database delete row  How to delete row from SQLite Database?    NSString *updateSQL = [NSString stringWithFormat: @"DELETE FROM aListDB WHERE id='%@'",details.ids
How to get the data from the database using Servlet or JSP program
How to get the data from the database using Servlet or JSP program  ... the problem   Get data from database using servlet Retrieve data from database using JSP Get data from database using JSP
delete multiple row using checkbox
delete multiple row using checkbox  how to delete multiple row in a table which is connected to database using checkbox
delete multiple row using checkbox
delete multiple row using checkbox  how to delete multiple row in a table which is connected to database using checkbox
how to delete specified coloumn from database(MS Access) by using windows application
how to delete specified coloumn from database(MS Access) by using windows application  how to delete specified coloumn from database(MS Access) by using c# windows application
how to select the row value that was retrived from the database ?
how to select the row value that was retrived from the database ?  I am getting the data's from the table that was stored in database. Now in the page in which i am getting all the data from the database has an another select
how to select the row value that was retrived from the database ?
how to select the row value that was retrived from the database ?  I am getting the data's from the table that was stored in database. Now in the page in which i am getting all the data from the database has an another select
delete row from a table in hibernate
delete row from a table in hibernate  is there method to delete row in a table using hibernate like save(-) to insert row
delete row using id
delete row using id  package pkg2; import org.hibernate.Query; import... = "delete from Insurance insurance where id = 2"; Query query = sess.createQuery...()); } } } this is the code m using . Error is -query must begin with SELECT or FROM
delete an item from database
delete an item from database  how to delete an item from the database using jsp
JDBC ResultSet Delete Row Example
JDBC ResultSet Delete Row Example: Learn how to delete row using ResultSet. We are also used ResultSet object with update capability for delete rows from.... 6. Finally call deleteRow() ResultSet method for delete the current row form
How to retrieve image from database in Servlet?
How to retrieve image from database in Servlet?  Hi, How to retrieve image from database in Servlet? Thanks   Hi, Please check the tutorial Retrieve image from database using Servlet. Thanks
How to write a loop and a while loop
How to write a loop and a while loop  How do I write a 1 loop and a 1 while loop for the example code: public boolean isTheFirstOneBigger (int num1, int num2) { if (num1 > num2) { return true
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  hello Sir... from database into jtable of a jpanel.. Now Sir, According to my need i have... to a particular cell will be updated into database and if we want to delete
fetch record from oracle database using jsp-servlet?
fetch record from oracle database using jsp-servlet?  how can i fetch data from oracle database by using jsp-servlet. i'm using eclipse, tomcat server and oracle database and creating jsp pages and also using servlet
delete multiple row using checkbox
delete multiple row using checkbox  delete multiple row using... fields bookid,author and title in the database. 1) Create book.jsp <%@page...=null; st=conn.createStatement(); rs = st.executeQuery("select * from book"); int
Using while loop to loop through a cursor
Using while loop to loop through a cursor  ... while loop to loop through a cursor'. The Example create a table Stu_Table... that establish the result set. The While loop check a condition
Retrieve database from the table dynamically in jsp from oracle using servlet
using java servlet from the database in the jsp page...Retrieve database from the table dynamically in jsp from oracle using servlet  Sir, I have created a table in oracle using eclipse, and added few
how to use String tokenizer on table that is retrieved from database using jsp servlet
how to use String tokenizer on table that is retrieved from database using jsp...,day,qty from medicinedetails2 where pid=15... row data is separated as per the '&' sign
How to get the data from the database (Oracle) in console or in ie using servlet or jsp as Front end
How to get the data from the database (Oracle) in console or in ie using servlet or jsp as Front end  hello i have a simple problem in jsp in the sense to get data from the database like oracle . I have created one jsp
Delete a Specific Row from a Database Table
Delete a Specific Row from a Database Table   ..., and in this section we are going to do the same that is, how to delete a specific row... the connection we are going to delete a specific row from the table. If the row
Retrieve image from database using servlet and display in JSP
Retrieve image from database using servlet and display in JSP  Hi, I... to use MySQL Database from JSP page. How to retrieve image from database using servlet and display in JSP? Thanks
Populate dropdown menu from database using jsp and servlet
Populate dropdown menu from database using jsp and servlet  please i need code to populate dropdown menu from mysql database using jsp and servlet. thanks

Ads