You are reading the article Steps For Jdbc Update With Examples updated in September 2023 on the website Nhahang12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Steps For Jdbc Update With Examples
Introduction to JDBC UpdateJDBC 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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
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 –
• 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
• 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.
ExamplesLet 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"; 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")); } (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 chúng tôi –
javac EducbaUpdateExample.java
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 #2Let 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 –
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"; public static void main(String[] args) { try(Connection sampleConnection = DriverManager.getConnection(urlForDatabase, userName, password); Statement sampleStatement = sampleConnection.createStatement(); ) { String sampleSqlStatement = "UPDATE educba_writers" + 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")); } (sampleResult.close(); } catch (SQLException sampleException) { sampleException.printStackTrace(); } } }Compiling the above program –
Javac EducbaWriterUpdateExample.java
Output is –
Java EducbaWriterUpdateExample
Output is –
ConclusionThe JDBC update method is used for updating the records of the existing tables inside the database from our java application.
Recommended ArticlesThis 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 –
You're reading Steps For Jdbc Update With Examples
Update the detailed information about Steps For Jdbc Update With Examples on the Nhahang12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!