JSP- Delete Query using JDBC
Aim:
Using JDBC, we will delete the record from mysql database.
Problem:
Already we insert many records in exam table.
Now we have to delete suresh records.
Procedure:
Step 1:
Load the Driver
Class.forName(“com.mysql.jdbc.Driver”);
Step 2:
Create Connection
String driver=”jdbc:mysql://localhost/example”;
Connection con=DriverManager.getConnection(driver,”username”,”password”);
Step 3:
Create Statement
Statement stmt=con.createStatement();
Step 4:
Execute Query
String sql=”delete from exam where student_name=’suresh’ “;
stmt.executeUpdate(sql);
Step 5:
Create jsp file
I have given jsp file name as DeleteExample.
Add following coding into DeleteExample.
< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >
< %@page import="java.sql.*" % >
< html >
< head >
< title > Java DataBase Connectivity Example-Delete Query < /title >
< /head >
< body >
< %!
Connection con=null;
ResultSet rs=null;
Statement stmt=null;
% >
<%
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e)
{
out.println(e.getMessage());
}
try
{
con=DriverManager.getConnection("jdbc:mysql://localhost/example","root","sure");
stmt=con.createStatement();
int i=stmt.executeUpdate("delete from exam where student_name='suresh'");
if(i==1)
{
out.println("Successfully Deleted");
}
else
{
out.println("Error For Inserted");
}
}
catch(Exception e)
{
out.println(e.getMessage());
}
finally
{
con.close();
stmt.close();
}
% >
< /body >
< /html >
Step 6:
Include mysql-connector.jar
Already we have included mysql-connector.jar into our project. So no need to include again.
Step 7:
Run the Application
Already I have explained how to run the application.
Step 8:
Get the output
thanks for the information. it helped me a lot.
ReplyDelete