Updated April 18, 2023
Introduction to JDBC Update
JDBC update is the command used in Java Database Connection API from java application to the database that you are using for your application. If you have to make the changes in any of the values stored inside the database from your application, then you have to use the update command. The prerequisites for firing an update command is that you should know the password and the username associated to your user for database and database like Oracle, MySQL, or whichever you might be using for your application should be running and in the on mode.
In this article, we will have a look at the syntax of the update command and how it works while using it inside JDBC. We will also have a look at the steps to be taken for the JDBC update and learn its implementation along with the help of an example.
Syntax:
The syntax of JDBC update command is as shown below.
UPDATE name of the table SET column name1 = value1, column name2 = value2, …. [WHERE restriction];
In the above syntax, the terminologies used here are described in detail below –
• Name of the table – The table name whose contents you want to modify.
• Column name – The name of the columns of the same table specified whose contents are to be updated.
• restriction – The where clause is optional in nature and is used for filtering and specifying the specific row or rows that are to be chosen for updating.
How it works?
The rows are retrieved from the table whose name is specified; they are filtered provided the where clause is given in the update command. Further, from these rows the columns whose names are specified are assigned with the value which is given in the command. Note that, in the same update command, we can change as many values as we want depending on our necessity. After you have specified the update query, you will also need to prepare the statement which you can execute.
Steps for JDBC update
Let us see the steps that are required inside the JDBC application while updating the data of the database.
• The packages are imported – Whatever packages are needed containing the classes required for performing the JDBC operations are imported right at the beginning of the program. Most of the basic functionalities can be imported by using import java.sql.* package.
• Opening and establishing the connection – You will need to install the required driver related to the database that you will be using and after that, you can make the use of the DriverManager.getConnection() method that will create an object for connection. This is used for representing the connection between your java application and the database in a physical manner.
• Perform the update query – You have to create an object that has the datatype of Statement which will be used for creating and building the query. This object is further sent to the database as it includes the update command that we have to execute. The update query may contain the optional clauses of IN and WHERE for updating the records present in the table on conditional basis.
• Environment clean up – Whatever resources are used while performing the operations of JDBC should be freed up so that the other operations can also use them.
Examples
Let us consider one example that will update the records present in the educba_articles table using the java application and JDBC. The contents of the educba_articles are as shown below –
SELECT * FROM [educba_articles]
The output of a query is –
The Java program that uses the JDBC update command to update the contents of the educba_articles table is given below –
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class EducbaUpdateExample {
static final String urlForDatabase = "jdbc:mysql://localhost/EDUCBA";
static final String userName = "payal";
static final String password = "payal123";
static final String selectQUERY = "SELECT article_id, article_name , number_of_words, posted_date FROM educba_articles";
public static void main(String[] args) {
try(Connection sampleConnection = DriverManager.getConnection(urlForDatabase, userName, password);
Statement sampleStatement = sampleConnection.createStatement();
) {
String sampleSqlStatement = "UPDATE educba_articles" +
"SET number_of_words = 1000 WHERE article_id in (1, 3)";
sampleStatement.executeUpdate(sampleSqlStatement);
ResultSet sampleResult = sampleStatement.executeQuery(selectQUERY);
while(sampleResult.next()){
System.out.print(" Article ID : " + sampleResult.getInt("article_id"));
System.out.print(", Name Of Article : " + sampleResult.getString("article_name"));
System.out.print(", Numer of Words : " + sampleResult.getInt("number_of_words"));
System.out.println(", Date of posting : " + sampleResult.getDate("posted_date"));
}
(sampleResult.close();
} catch (SQLException sampleException) {
sampleException.printStackTrace();
}
}
}
After you compile and run the above java project, you will get the result. The compilation of this file can be done by using the below command if the name of your file is EducbaUpdateExample.java –
javac EducbaUpdateExample.java
The output of the above code is –
Now, in order to run the java code, we can execute the following command –
java EducbaUpdateExample
The output of above command is as shown below that contains the updated records of the articles with is 1 and 3 as per our query statement –
Example #2
Let us now consider one more example where, we will try to update more than one column from the table. We have one table whose name is educba_writers and contents can be studied with the below query –
SELECT * FROM [educba_writers]
Whose output is as shown below –
Now, we will try to update the contents of this table by using below program –
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class EducbaWriterUpdateExample{
static final String urlForDatabase = "jdbc:mysql://localhost/EDUCBA";
static final String userName = "payal";
static final String password = "payal123";
static final String selectQUERY = "SELECT writer_id, writer_mail_id , number_of_articles, posted_date FROM educba_writers";
public static void main(String[] args) {
try(Connection sampleConnection = DriverManager.getConnection(urlForDatabase, userName, password);
Statement sampleStatement = sampleConnection.createStatement();
) {
String sampleSqlStatement = "UPDATE educba_writers" +
"SET number_of_articles = 100, posted_date = "26/08/2021" WHERE article_id in (3, 4)";
sampleStatement.executeUpdate(sampleSqlStatement);
ResultSet sampleResult = sampleStatement.executeQuery(selectQUERY);
while(sampleResult.next()){
System.out.print(" Writer ID : " + sampleResult.getInt("writer_id"));
System.out.print(", Mail ID of Writer : " + sampleResult.getString("writer_mail_id"));
System.out.print(", Numer of Articles : " + sampleResult.getInt("number_of_articles"));
System.out.println(", Date of posting : " + sampleResult.getDate("posted_date"));
}
(sampleResult.close();
} catch (SQLException sampleException) {
sampleException.printStackTrace();
}
}
}
Compiling the above program –
Javac EducbaWriterUpdateExample.java
Output is –
Java EducbaWriterUpdateExample
Output is –
Conclusion
The JDBC update method is used for updating the records of the existing tables inside the database from our java application.
Recommended Articles
This is a guide to JDBC Update. Here we discuss the Introduction, syntax, How it works?, Steps for JDBC update respectively. You may also have a look at the following articles to learn more –