Friday, October 8, 2010

JSP update query using JDBC

JSP- update statement

Introduction:

Sometimes we want to change the inserted data into the database.
If you want to update the student_name in our exam table, you’ll use a SQL statement like the one below:

mysql > update exam set student_name=’suresh’ where student_name=’sureshkumar’;


Problem:

Update data in mysql database.

We have to change student_name as kannanto kamalkannan.

Procedure:

Already How to import mysql-connector.jar and how to create database and how to create table and how to insert data into the table and how to select the inserted data in exam table.

Step 1:

Use update SQL statement to update data in our exam table.

We use update SQL statement to update data in mysql






















Step 2:

Create jsp file I have used file name as updatePro.

Include following coding into updatePro

< %@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-Update 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("update exam set student_name='kamalkannan' where student_name='kannan'");
if(i==1)
{
out.println("Successfully Updated");
}
else
{
out.println("Error For Updated");
}
}

catch(Exception e)
{
out.println(e.getMessage());
}
finally
{
con.close();
stmt.close();
}
% >
< /body >
< /html >

Step 3:



Step 4:

            We want to check the data is updated successfully.


No comments:

Post a Comment