JDBC PreparedStatement example – Update a record

Here’s an example to show you how to update a record in table via JDBC PreparedStatement. To issue a update statement, calls the PreparedStatement.executeUpdate() method like this :


String updateTableSQL = "UPDATE DBUSER SET USERNAME = ? WHERE USER_ID = ?";
PreparedStatement preparedStatement = dbConnection.prepareStatement(updateTableSQL);
preparedStatement.setString(1, "mkyong_new_value");
preparedStatement.setInt(2, 1001);
// execute insert SQL stetement
preparedStatement .executeUpdate();

Full example…


package com.mkyong.jdbc;

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

public class JDBCPreparedStatementUpdateExample {

	private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
	private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:MKYONG";
	private static final String DB_USER = "user";
	private static final String DB_PASSWORD = "password";

	public static void main(String[] argv) {

		try {

			updateRecordToTable();

		} catch (SQLException e) {

			System.out.println(e.getMessage());

		}

	}

	private static void updateRecordToTable() throws SQLException {

		Connection dbConnection = null;
		PreparedStatement preparedStatement = null;

		String updateTableSQL = "UPDATE DBUSER SET USERNAME = ? "
				                  + " WHERE USER_ID = ?";

		try {
			dbConnection = getDBConnection();
			preparedStatement = dbConnection.prepareStatement(updateTableSQL);

			preparedStatement.setString(1, "mkyong_new_value");
			preparedStatement.setInt(2, 1001);

			// execute update SQL stetement
			preparedStatement.executeUpdate();

			System.out.println("Record is updated to DBUSER table!");

		} catch (SQLException e) {

			System.out.println(e.getMessage());

		} finally {

			if (preparedStatement != null) {
				preparedStatement.close();
			}

			if (dbConnection != null) {
				dbConnection.close();
			}

		}

	}

	private static Connection getDBConnection() {

		Connection dbConnection = null;

		try {

			Class.forName(DB_DRIVER);

		} catch (ClassNotFoundException e) {

			System.out.println(e.getMessage());

		}

		try {

			dbConnection = DriverManager.getConnection(
                            DB_CONNECTION, DB_USER,DB_PASSWORD);
			return dbConnection;

		} catch (SQLException e) {

			System.out.println(e.getMessage());

		}

		return dbConnection;

	}

}

Result

The username of “user_id = 1001” is updated to a new value ‘mkyong_new_value’.

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
HoangLe
Guest
HoangLe

Your code will always show: Record is updated to DBUSER table even though it doesn’t update. you have to get the int value that is returned from executeUpdate() and throws exception if the return value is 0. Your try catch only check SQL syntax.

Andrey Stepanov
Guest
Andrey Stepanov

What does this line do: Class.forName(DB_DRIVER)?

Why does it neither return anything nor create a new instance of a driver like: Class.forName(DB_DRIVER).newIntsance()?

What driver the DriverManager will use when calling getConnection()?

Thank you

Jack the Newbie
Guest
Jack the Newbie

Boy, you do a great job! Thanks

kush
Guest
kush
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication1; import java.util.logging.Level; import java.util.logging.Logger; import java.sql.*; import java.util.Scanner; /** * * @author 3ce75 */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { try { Class.forName(“org.apache.derby.jdbc.ClientDriver”); } catch (ClassNotFoundException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } String url=”jdbc:derby://localhost:1527/db”; String un=”d”; String pwd=”b”; try { Connection cn = DriverManager.getConnection(url, un, pwd); int choice; System.out.println(“enter your choice”); Scanner sn=new Scanner(System.in); choice=sn.nextInt(); String query; switch(choice) { case 1: query=”insert into db (id,name,mobile) values(?,?,?)”;… Read more »
zahid se
Guest
zahid se

this website is my favorite website i really say thanks to this web site developers .

Jisa Maria Jameson
Guest
Jisa Maria Jameson

Thank you. Was helpful to do my project