
how to delete data from database using jsp?

hi friend,
First create a table in the database and insert some records into the database table and then write a JSP like below :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*;" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Delete Data</title>
</head>
<body>
<form action="jspDelete.jsp">
<table>
<tr>
<td>Enter ID To Delete</td>
<td><input type="text" name="id"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Delete"/></td>
</tr>
</table>
</form>
<%! String driverName = "com.mysql.jdbc.Driver";%>
<%!String url = "jdbc:mysql://localhost:3306/record";%>
<%!String user = "root";%>
<%!String psw = "root";%>
<%
String id = request.getParameter("id");
if(id != null)
{
Connection con = null;
PreparedStatement ps = null;
int personID = Integer.parseInt(id);
try
{
Class.forName(driverName);
con = DriverManager.getConnection(url,user,psw);
String sql = "DELETE FROM person WHERE personID="+personID;
ps = con.prepareStatement(sql);
boolean bol = ps.execute();
if(bol == true)
{%>
<jsp:forward page="/success.jsp"/>
<%
}
else
%>
<jsp:forward page="/failure.jsp"/>
<%
}
catch(SQLException sqe)
{
request.setAttribute("error", sqe);
out.println(sqe);
}
}
%>
</body>
</html>
For Detail Tutorial go through the link http://www.roseindia.net/jsp/delete-data-from-database.shtml

Hi Teena,
The JSTL sql tag library is there to perform the database operations.
ex: delete.jsp
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<sql:setDataSource var="datasource" driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@locahost:1521:orcl" user="teena" password="teena" />
<sql:update var="deletedrecords" dataSource="${datasource}">
delete from employee where empno=?
<sql:param value="234" />
</sql:update>
Number of records deleted : ${deletedrecords}

Hi Teena,
The JSTL sql tag library is there to perform the database operations.
ex: delete.jsp
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<sql:setDataSource var="datasource" driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@locahost:1521:orcl" user="teena" password="teena" />
<sql:update var="deletedrecords" dataSource="${datasource}">
delete from employee where empno=?
<sql:param value="234" />
</sql:update>
Number of records deleted : ${deletedrecords}
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.