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 all the values are added in to database and page is redirected to a new page that contains entries of submittes form. Now there are 2 extra columns(10+2) for "DELETE" AND "UPDATE". On clicking delete which is hyper link that particular row should be deleted and on clicking update , we should be able to update its value. So anyone will tell me how to give hyper link to Delete and Update and delete query (by id).???

View Answers

November 9, 2012 at 2:58 PM

Here is an application that allow user to update and delete record from database.

1)application.jsp:

<%@ page import="java.sql.*" %>
<html>
<head>
<script language="javascript">
function editRecord(id){
    var f=document.form;
    f.method="post";
    f.action='edit.jsp?id='+id;
    f.submit();
}
function deleteRecord(id){
    var f=document.form;
    f.method="post";
    f.action='delete.jsp?id='+id;
    f.submit();
}
</script>
</head>
<body>

<br><br>
<form method="post" name="form">
<table border="1">
<tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr>
<%
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String userName ="root";
String password="root";

int sumcount=0;
Statement st;
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+db,userName,password);
String query = "select * from employee";
st = con.createStatement();
ResultSet rs = st.executeQuery(query);
%>

<%
while(rs.next()){
%>
<tr><td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><input type="button" name="edit" value="Edit" style="background-color:#49743D;font-weight:bold;color:#ffffff;" onclick="editRecord(<%=rs.getString(1)%>);" ></td>
<td><input type="button" name="delete" value="Delete" style="background-color:#ff0000;font-weight:bold;color:#ffffff;" onclick="deleteRecord(<%=rs.getString(1)%>);" ></td>
</tr>
<%
}
%>
<%
}
catch(Exception e){
e.printStackTrace();
}
%>
</table>
</form>
</body>
</html>

2)edit.jsp:

<%@page language="java"%>
<%@page import="java.sql.*"%>
<form method="post" action="update.jsp">
<table border="1">
<tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr>
<%
String id=request.getParameter("id");
int no=Integer.parseInt(id);
int sumcount=0;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
String query = "select * from employee where id='"+no+"'";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next()){
%>
<tr>
<td><input type="text" name="name" value="<%=rs.getString("name")%>"></td>
<td><input type="text" name="address" value="<%=rs.getString("address")%>"></td>
<td><input type="text" name="contact" value="<%=rs.getInt("contactNo")%>"></td>
<td><input type="text" name="email" value="<%=rs.getString("email")%>"></td>
<td><input type="hidden" name="id" value="<%=rs.getString(1)%>"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Update" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td>
</tr>
<%
}
}
catch(Exception e){}
%>
</table>
</form>

November 9, 2012 at 3:02 PM

continue..

3)update.jsp:

<%@page import="java.sql.*"%>

<%
String ide=request.getParameter("id");
int num=Integer.parseInt(ide);
String name=request.getParameter("name");
String address=request.getParameter("address");
int contact=Integer.parseInt(request.getParameter("contact"));
String email=request.getParameter("email");
try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
Statement st=null;
st=conn.createStatement();
st.executeUpdate("update employee set name='"+name+"',address='"+address+"',contactNo="+contact+",email='"+email+"' where id='"+num+"'");
response.sendRedirect("/examples/jsp/application.jsp");
}
catch(Exception e){
System.out.println(e);
    }
%>

4)delete.jsp:

<%@page import="java.sql.*"%>
<%

String id=request.getParameter("id");
int no=Integer.parseInt(id);
int sumcount=0;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = conn.createStatement();
st.executeUpdate("DELETE FROM employee WHERE id = '"+no+"'");
response.sendRedirect("application.jsp");
}
catch(Exception e){}
%>

For more information, visit the following link:

http://www.roseindia.net/answers/viewqa/JSP-Servlet/15955-Servlets.html


April 23, 2013 at 10:11 AM

but this ans is not working.it is not passing the data from table as js function argumnt..plss help me...









Related Tutorials/Questions & Answers:
Delete a row from database by id
Delete a row from database by id  I m creating a small application...) for "DELETE" AND "UPDATE". On clicking delete which is hyper link that particular row... all the values are added in to database and page is redirected to a new page
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
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
delete row using id
= "delete from Insurance insurance where id = 2"; Query query = sess.createQuery...: delete [delete from pkg2.Insurance insurance where id = 2...delete row using id  package pkg2; import org.hibernate.Query; import
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  Dear Sir/Madam, I am trying to delete the one user data in the Oracle SQL.... It is Urgent...  When you retrieve the data from the Database by using
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 points from database
: mysqlquery("DELETE FROM polygon WHERE ID=".$GET['ID']."") or die(showsqlerrors(mysql_error())); I also used: mysqlquery("DELETE FROM points WHERE ID=".$GET['ID...Delete points from database  I have a polygon on a web page
delete an item from database
delete an item from database  how to delete an item from the database using jsp
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
Delete a Specific Row from a Database Table
Delete a Specific Row from a Database Table   .... After establishing the connection we are going to delete a specific row from... in a row, now we need to delete that wrong data. This can be done very easily
delete row
= $checkbox[$i]; mysql_query("delete from sonu where id='$delete...delete row  how to delete row using checkbox and button in php...("sourabh", $link); $rows=mysql_query("select * from sonu"); $row=mysql
delete data from database - SQL
delete data from database  HOw to delete specific data from table> I want to delete one record from database. Thnx  Hi friend, -------------------------------------------- Visit for more information
Update delete perticular row from brower on link - Struts
Update delete perticular row from brower on link   how can update and delete perticular row from List of employee in brower format are ramesh... page with textbox update , if delete click then that row also delete from
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
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... for delete the current row form the table of the ResultSet object. Example
Delete row and column from table through java code
the row having minimum ID. statement.executeUpdate("delete from stu_info... Delete row and column from table through java code... will see how to delete row and column from given table through java code. Java code
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
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
JDBC Delete Row In Table Example
or more specific  row delete from table that follow any given condition...  Mysql query  "DELETE FROM user where user_id=1 "  ...(); String query = "DELETE FROM user where user_id=1 "; int count
Delete a Column from a Database Table
Delete a Column from a Database Table   ... to delete a column from a database table. We are not going to create a new table... the already created table in the specific database. Now if we want to delete
insert and delete a row programmatically
insert and delete a row programmatically  How to insert and delete a row programmatically ? (new feature in JDBC 2.0
row_id
row_id  sir i have created a table my_table with two rows n two column with smillar name in each cell without any primary key , how can i change the first row and third column's name in sql server 2005 , kindly send me the query
row_id
row_id  sir i have created a table my_table with two rows n two column with smillar name in each cell without any primary key , how can i change the first row and third column's name in sql server 2005 , kindly send me the query
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  Hello Sir... from database to jtable .Now as per my requirement i need to update and delete the database records from the table cells by entering new values there only
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  Hello Sir, I am working on a project in which i have to fetch the values from database to jtable .Now as per my requirement i need to update and delete the database
Deleting row and column from a table
 the row having minimum ID.  statement.executeUpdate("delete from cellular where id...Deleting row and column from a table  In this program ,we delete row... connection interface and java driver. After it we can delete row using "delete
uitableview manually delete row
uitableview manually delete row  uitableview manually delete row   NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet]; [array removeObjectAtIndex:indexPath.row]; if(![array count]) [indexes addIndex
delete multiple row using checkbox
delete multiple row using checkbox  delete multiple row using... from book where bookid='"+id[a]+"'"); } }catch(SQLException e... fields bookid,author and title in the database. 1) Create book.jsp <%@page
Delete and add row from Table View iPhone
Delete and add row from Table View iPhone In this tutorial will learn how to delete and also how to add row into the table view iPhone, with the help of edit... will delete object from Table by removeLastObject and is then reloaded the data
How we delete a data of database from front end jsp page
How we delete a data of database from front end jsp page   I make a website and featch a data from data base and now i want that a delete button put... deleted from jsp page as well as from database.I used mysql and jsp. Please help me
Hibernate delete a row error - Hibernate
Hibernate delete a row error  Hello, I been try with the hibernate...(); //======================================= sess = fact.openSession(); String hql = "delete from Contact contact where id = 99"; Query query = sess.createQuery(hql); int row
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 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
retrieve value from database on the basis of maximum id number
retrieve value from database on the basis of maximum id number  hi, i want to retrieve value of maximum id number from the database and show that value in jTextField.when the user clicks on the button that maximum id number
Updating rows who has same id with different values for each row from java program
Updating rows who has same id with different values for each row from java... rows with same id. student table: sid sname age 2 aruna 25 2... to update the records which contains sid 1. But i have to update first row
display data from database in textbox when id entered in textbox
display data from database in textbox when id entered in textbox  i wanted to enter data in textbox .depending on entered data in textbox data from database should be displayed dynamically in textbox
Modifying a single row of database based on selection in jsp
or delete the selected row and update to database.. Please provide me a solution...Modifying a single row of database based on selection in jsp  Hi guys, i have a problem, I am Fetching the results from database and displaying
insert and delete data in database
insert and delete data in database  insert and delete data in database from servlets through JDBC   Hi Friend, Please visit the following links:ADS_TO_REPLACE_1 Insert Data Delete Data ThanksADS_TO_REPLACE_2
data grid with edit and delete options at each row.
data grid with edit and delete options at each row.  i want to display the table data in the format of data grid with edit and delete options at each row. i need it very urgently. advance thanks
to display single row from database and next to display other question - JSP-Servlet
to display single row from database and next to display other question  Hi all, I am using JSP with MYSQL backend. In my project we have 100 questions in database we have to display first question on clicking the button
retrieving data from database to the textbox depending upon the id in jsp
retrieving data from database to the textbox depending upon the id in jsp  Hi, our project involves fetching of data from database into textbox... rs=st.executeQuery("select * from item where itemid='"+id+"'"); while(rs.next
Arraylist from row values
Arraylist from row values  Hello, can anyone please help on how to make an arraylist from the row values of a particular column from a database...("select * from employee"); ArrayList<String> list=new ArrayList<
display multiple image file or pdf file in multiple column of a row from server or database
display multiple image file or pdf file in multiple column of a row from server... in a single row..Suppose I have to display two files in a row then after uploading more files then uploaded file should be display again tow file in another row
Deleting a Row from SQL Table Using EJB
are going to delete a row from the SQL Table. Find out the steps given below that describes how to delete a particular row from the database table using EJB... Deleting a Row from SQL Table Using EJB
How to Autogenerate of ID from database and show on JSP page?
How to Autogenerate of ID from database and show on JSP page?  ... tasks id as primay key, I am using JSP and struts for database connectivity,Now I can update my database but i want to autogenerate tasks id in numbers(ie
How to Autogenerate of ID from database and show on JSP page?
How to Autogenerate of ID from database and show on JSP page?  ... tasks id as primay key, I am using JSP and struts for database connectivity,Now I can update my database but i want to autogenerate tasks id in numbers(ie
How to Autogenerate of ID from database and show on JSP page?
How to Autogenerate of ID from database and show on JSP page?  ... tasks id as primay key, I am using JSP and struts for database connectivity,Now I can update my database but i want to autogenerate tasks id in numbers(ie
How to Autogenerate of ID from database and show on JSP page?
How to Autogenerate of ID from database and show on JSP page?  ... tasks id as primay key, I am using JSP and struts for database connectivity,Now I can update my database but i want to autogenerate tasks id in numbers on JSP

Ads