Thursday, October 30, 2014

Java MySQL UPDATE Using PreparedStatement


Java MySQL UPDATE Using PreparedStatement

In the Java applications there are several ways to update the data base records. The more convenient way is using the PreparedStatement for update the data base records by sending the SQL statements.

In this tutorial we used the PreparedStatements for update the records available in MySQL data base.

Scope : Update the table records in MySQL with using Prepared Statement.

Objective : Update the table records in MySQL with using Prepared Statement.

Requirements :
  • Eclipse Java EE IDE
  • MySQL Server 5.5
Step 1 : Create an Java project in Eclipse and name it as "UpdateExample". 

Step 2 : Download the "mysql-connector.jar" from here and add to the project build path.

Step 3 : Create a table Student in MySQL test database with using following script.


1
2
3
4
5
6
CREATE TABLE `test`.`Students` (
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `firstname` VARCHAR(45) NOT NULL DEFAULT '',
  `email` VARCHAR(50) NOT NULL DEFAULT '',
  PRIMARY KEY(`id`)
);

Insert the following records to the Student table which we already created with using the above script.


1
2
insert into users (firstname, email) values ('John', 'john@gmail.com');
insert into users (firstname, email) values ('Tester', 'Tester@test.com');


Step 4 : Create a class "PreparedStatementUpdate" under the "com.findanidea.update" package.

The MySql driver and the url defined as private static final instance variable at line numbers 10 and 11.

The Connection created at the line number 15 with using the getConnection method. The SQL statement for update the records specified in the line number 18. The input parameters defined in order in line numbers 20 and 21. 

Finally the prepared statement executed at line number 24 and if its executed successfully the message specified in the line number 25 will print at the application console.

/UpdateExamle/src/com/findanidea/update/PreparedStatementUpdate.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.findanidea.update;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class PreparedStatementUpdate {
 
 private static final String myDriver = "com.mysql.jdbc.Driver";
 private static final String myUrl = "jdbc:mysql://localhost:3306/test";
 public static void main(String[] args) {
  try {
   
   Connection conn = getConnection();

   // create the update preparedstatement
   String query = "update student set email = ? where firstname = ?";
   PreparedStatement preparedStmt = conn.prepareStatement(query);
   preparedStmt.setString(1, "tester@test.com");
   preparedStmt.setString(2, "John");

   // execute the preparedstatement
   preparedStmt.executeUpdate();
   System.out.println("Record updated Successfully.");
   conn.close();
  } catch (Exception e) {
   System.err.println("Exception Occurs!!!!");
   System.err.println(e.getMessage());
  }
 }
 
 private static Connection getConnection() throws ClassNotFoundException, SQLException {
  Class.forName(myDriver);
  Connection conn = DriverManager.getConnection(myUrl, "root", "1234");
  return conn;
 }
}


Step 5 : Once execute the above mentioned class in the Step 4, you able to see the below out put at your application console.


1
Record updated Successfully.

No comments:

Post a Comment