Home Answers Viewqa JDBC Delete a row from database by id

 
 


Jackin
Delete a row from database by id
3 Answer(s)      6 months and 16 days ago
Posted in : JDBC

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 Pages:
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
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
= $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 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
delete
delete  how delete only one row in the database using jsp.database... = conn.createStatement(); st.executeUpdate("DELETE FROM employee WHERE empid...="javascript"> function deleteRecord(id){ window.open('http://localhost:8080/examples
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
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
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
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
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 "  ... query = "DELETE FROM user where user_id=1 "; int count=stmt.executeUpdate(query
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
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
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 row and column from table through java code
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... will delete the row having minimum value of ID then delete the column named
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 a Specific Row from a Database Table
Delete a Specific Row from a Database Table   ... the connection we are going to delete a specific row from the table. If the row... in a row, now we need to delete that wrong data. This can be done very easily
delete record
of id of table. When the user selects the particular checkbox, that row will get deleted from the database. In the database we have created three fields bookid...=conn.createStatement(); for(int a=0;a<10;a++){ st.executeUpdate("delete from book where
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
delete an item from database
delete an item from database  how to delete an item from the database using jsp
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... and Delete buttons on Table view. Table views are commonly found in iPhone applications
MySQL PHP Query delete
( ) delete query from a database table. MySQL PHP Query is used to delete the records... the database:".mysql_error()); $sql="delete from mytable where empid... ( ), that is used to delete a records from table using delete Syntax. The Where Clause
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
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
MySQL PHP Query delete
( ) delete query from a database table. MySQL PHP Query is used to delete the records... in selecting the database:".mysql_error()); $sql="delete from mytable where... ( ), that is used to delete a records from table using delete Syntax. The Where Clause
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
Hibernate Delete Query
database using the hibernate. Lets first write a java class to delete a row from... (DeleteHQLExample.java), which we will delete a row from the insurance table using the query... properly. Hibernate: delete from insurance where ID=2 Deleted
include a delete option in every row of table in a JSP page
on Delete it should delete the particular record from the database and reflect...include a delete option in every row of table in a JSP page  I have..." sql="select* from login"> </sql:query> <html> <
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
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
how update JTable after adding a row into database
classes, listed below, one is table model, and second show data from database in JTable, and it's OK, but after adding a row into database table does't update. How update JTable after adding a row into database? package djilepak.javaclss.for
get details of employee after enter the emp_id
get details of employee after enter the emp_id  how to get employee details after entered the emp_id from database after that we have three more.../JSP-Servlet/18899-data-grid-with-edit-and-delete-options-at-each-row-.html
get details of employee after enter the emp_id
get details of employee after enter the emp_id  how to get employee details after entered the emp_id from database after that we have three more.../JSP-Servlet/18899-data-grid-with-edit-and-delete-options-at-each-row-.html
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
need to generate ID
into database. In the given code, we have fetched the last id from the database table and by adding 1, we have generated the next id and inserted into database... the previous highest ID value stored in database. eg: if the higest value
need to generate ID
into database. In the given code, we have fetched the last id from the database table and by adding 1, we have generated the next id and inserted into database... the previous highest ID value stored in database. eg: if the higest value
Deleting a Row from SQL Table Using EJB
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. The steps... Deleting a Row from SQL Table Using EJB   
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: Insert Data Delete Data Thanks
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<
How to delete and update from Jtable cell in swing app
the database . I want to remove and delete the same row which is selected for the dletion...How to delete and update from Jtable cell in swing app  Hii Sir... on delete button on selecting particular row which has to be deleted then last row
Works only for one row
Works only for one row   Hi, My below code is working only if there is a single row. could you please help me in doing it for all the rows retrieved from the database. <%@page import="java.util.concurrent.CountDownLatch
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
The DELETE Statement
; The DELETE statement is used to delete rows from a table. database will update...;       DELETE FROM table_name WHERE... deleted . DELETE FROM Person WHERE LName = 'sonu
Use of delete() method in hibernate.
department0_ where department0_.id=? Hibernate: delete from department where id...() method of hibernate?   Hibernate provide the facility to delete one row or multiple row from a table. Here is an example. Example: package
How to Extract row from table view using JSP Code - Java Beginners
How to Extract row from table view using JSP Code  Hi Friends... problem exist with retrival of row from a table on click of link. Table Structure... Email 1 A B C D E F G Update Delete On click of Update, entire row
delete query problem - Hibernate
, String hql = "delete from Insurance insurance where id = 2"; Query... = sess. beginTransaction(); String hql = "delete from STUDENT where name = 'mitha... system properly. STUDENT is not mapped. [delete from STUDENT where name = 'mitha
Delete database Table through hibernate and Spring
Delete database Table through hibernate and Spring  Hi, I am using Spring,Hibernate and Tapestry to save and also fetch data simultaneously from...) in MySQL database with database name Json: package tuto.webssh.domain.model
Delete image from folder - JSP-Servlet
pass the userid, on the id basis write a delete statement and invoke the database...Delete image from folder  Dear All, i used some coding.... that image name based on user id and that image is going to on folder in server after

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.