Java J2ee Tutorials

Delete Data Records Using JSP, My Sql

Saturday, January 14, 2012

deleteRecords.jsp

<%@ page language = "java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import = "java.sql.*"%>
Delete Records
<%! boolean success = false; %>
<%
int delID = Integer.parseInt(request.getParameter("deleteID"));
//String sql = "DELETE * FROM EMP_DETAIL WHERE ID = '"+delID+"'";
String sql = "DELETE FROM EMP_DETAIL WHERE ID = ?";

Connection con = null;
PreparedStatement psmt = null;
String url = "jdbc:mysql://localhost:3306/test_db";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url, userName, password);
psmt = con.prepareStatement(sql);
psmt.setInt(1, delID);
int nRows = psmt.executeUpdate();
if(nRows == 0){
success = false;
}
else{
success = true;
}
psmt.close();
con.close();
%>
<%
if(success == true){
%>
One record deleted
View Records
<%
}
else{
%>

Can not delete record now. Please try later...
<%
}
%>

Free Career Predictions

0 comments:

Post a Comment