JDBC Prepared Statement Update

The Tutorial illustrates a code that help you in understanding JDBC Prepared Statement Update.


 

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

JDBC Prepared Statement Update

     

The Update  Statement in SQL updates the existing records in a table. In JDBC the Prepared Statement Update is used to update the SQL statement, using where clause, that specify which records is updated.

Understand with Example

The Tutorial illustrates a code that help you in understanding JDBC Prepared Statement Update. The code include a class Jdbc Prepared Statement, this class include a main method ( ), Inside the main method we have a list of steps -

 Importing a package java.sql - This package provides you a network interface, that enables you to communicate between front end application -back end. Loading a driver by calling a class.forName ( ),that accept a driver class as argument.

  DriverManager.get Connection ( ) -The Driver Manager call a getConnection ( ) method, return you a connection object, built a connection between url and database.

 The connection object call a prepareStatement ( ),that is used to update the table stu using where clause, specify the name of record to be updated at runtime. The set String XXX return you a set value in the place of question mark holder. The executeQuery ( ) return you the record set from a updated table in the database.

  Finally the println print the updated table from the database in output. The catch block caught the exception in try block.

JdbcPreparedstatementUpdate.java

import java.sql.*;

public class JdbcPreparedstatementUpdate {

  public static void main(String args[]) {

  Connection con = null;
  PreparedStatement pst = null;
  ResultSet rs = 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);
  
  pst = con.prepareStatement("update stu set class=? where id =?");
  
  pst.setString(1, "11");
  pst.setString(2, "1");
  
  pst.executeUpdate();
  
  
  pst = con.prepareStatement("select * from stu");
  rs = pst.executeQuery();

  System.out.println("Id\tName\tClass");
  while (rs.next()) {
  System.out.print(rs.getString(1) + "\t");
  System.out.print(rs.getString(2) + "\t ");
  System.out.println(rs.getString(3));
  }

  rs.close();
  pst.close();
  con.close();
  
  } catch (Exception e) {
  System.out.println(e);
  }
  }
}

Output

Id	Name	Class
1	komal	 11
2	santosh	 11
3	rakesh	 9
4	ajay	 11
5	bhanu	 10

Download code

Advertisements

Ads
Share on Google+Share on Google+

JDBC Prepared Statement Update

Posted on: November 19, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: JDBC Prepared Statement Update   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
swapnil
August 28, 2013
question

sir,if we want to update two or more attributes.

Ads

 

Ads