Home Jdbc JDBC Execute Update Example
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

JDBC Execute Update Example

Advertisement
JDBC Execute Update query is used to modify or return you an integer value specify the number of rows affected in the backend of database.

JDBC Execute Update Example

     

JDBC Execute Update query is used to modify or return you an integer value specify the number of rows affected in the backend of  database.

The code illustrates a simple example from JDBC Execute update Example. In this Tutorial we want to describe you a code that helps you in understanding JDBC Execute update Example. For this we have a class JDBC Execute update, Inside this class we have a main method that include the list of following steps to be followed -

The first step is to import a package java.sql.* -The * specify the list of all the classes defined in this package that provides you an network interface for java front end application to communicate with backend database.

  Loading a driver is the next step to be followed by make a call to Class.forName( ) with driver class passed as argument.

  DriverManager.getConnection ( ) : This method return you a connection class object. This try to built a connection between Url and database.

   con.createStatement ( ) : This is used to return you Sql object. A connection object is used to send and execute sql query in the backend database.

   st.executeUpdate ( ) :  This method return you an integer value, this specify the number of rows to be affected in the table .

In case there is an exception in the try block, the subsequent catch block handle the exception and print it. On the execution of program, the code show you an updated item in the table.

JdbcExecuteupdateExample.java


import java.sql.*;

public class JdbcExecuteupdateExample {

  public static void main(String args[]) {

  Connection con = null;
  Statement st = null;

  String url = "jdbc:mysql://localhost:3306/";
  String db = "komal";
  String driver = "com.mysql.jdbc.Driver";
  String user = "root";
  String pass = "root";

  try {
  Class.forName(driver);
  con = DriverManager.getConnection(url + db, user, pass);
  con.setAutoCommit(false);
  st = con.createStatement();

  String sql = "update person set cname='Rakesh' where id=4";
  st.executeUpdate(sql);

  System.out.println("Table updated.");

  catch (Exception e) {
  System.out.println(e);
  }
  }
}

Output

Table updated.

Download code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 17, 2008

Related Tutorials

Ask Questions?    Discuss: JDBC Execute Update Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Rajasekaran
December 23, 2011
not applicable

con.setAutoCommit(false); tis method is not necessary in tat program.. so ppl's pls ignore tat :)correct method is con.setAutoCommit(true);
DMCA.com