
how delete only one row in the database using jsp.database is mysql

Hi Friend,
Try the following code:
1)user.jsp:
<%@ page import="java.sql.*" %>
<html>
<head>
<script language="javascript">
function deleteRecord(id){
window.open('http://localhost:8080/examples/jsp/deleteuser.jsp?id='+id,'mywindow','width=500, height=350,toolbar=no,resizable=yes,menubar=yes');
}
</script>
</head>
<body>
<br><br>
<table border="1">
<tr><th>Emp ID</th><th>Name</th><th>Salary</th><th>Designation</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("empid")%></td>
<td><%=rs.getString("name")%></td>
<td><%=rs.getString("salary")%></td>
<td><%=rs.getString("designation")%></td>
<td><input type="button" name="edit" value="Delete" style="background-color:red;font-weight:bold;color:#ffffff;" onclick="deleteRecord(<%=rs.getString(1)%>);" ></td>
</tr>
<%
}
%>
<%
}
catch(Exception e) {
e.printStackTrace();
}
%>
</table>
</form>
</body>
</html>
2)deleteuser.jsp:
<%@page import="java.sql.*"%>
<%
String id=request.getParameter("id");
int no=Integer.parseInt(id);
System.out.println(no);
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 empid = '"+no+"'");
out.println("Record is deleted successfully");
}
catch(Exception e){}
%>
Thanks

sir i want to show only last stored record for another page.how can i set the codition for that sir
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.