In this example, we will discuss how to update a record in a MySQL database table via a JDBC statement.
Learn a complete JDBC tutorial at https://www.javaguides.net/p/jdbc-tutorial.html.
Learn a complete JDBC tutorial at https://www.javaguides.net/p/jdbc-tutorial.html.
Fundamental Steps in JDBC
The fundamental steps involved in the process of connecting to a database and executing a query consist of the following:
- Import JDBC Packages
- Establishing a connection.
- Create a statement.
- Execute the query.
- Using try-with-resources statements to automatically close JDBC resources.
Key points
- From JDBC 4.0, we don't need to include 'Class.forName()' in our code to load JDBC driver. JDBC 4.0 drivers that are found in your classpath are automatically loaded.
- We have used try-with-resources statements to automatically close JDBC resources.
JDBC Statement Update Record Example
Below example update a single record in a database table.
Check out below articles before updating the record:
>> JDBC Statement Create a Table Example
>> JDBC Statement - Insert Multiple Records Example
In the below JDBC example, we are using the UPDATE SQL statement to update record to the database table:
package com.javaguides.jdbc.statement.examples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Update Statement JDBC Example
* @author Ramesh Fadatare
*
*/
public class UpdateStatementExample {
private static final String UPDATE_USERS_SQL = "update users set name = \"Ram\" where id = 1;";
public static void main(String[] argv) throws SQLException {
UpdateStatementExample updateStatementExample = new UpdateStatementExample();
updateStatementExample.updateRecord();
}
public void updateRecord() throws SQLException {
System.out.println(UPDATE_USERS_SQL);
// Step 1: Establishing a Connection
try (Connection connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/mysql_database?useSSL=false", "root", "root");
// Step 2:Create a statement using connection object
Statement statement = connection.createStatement();) {
// Step 3: Execute the query or update query
int result = statement.executeUpdate(UPDATE_USERS_SQL);
System.out.println("Number of records affected :: " + result);
} catch (SQLException e) {
// print SQL exception information
printSQLException(e);
}
// Step 4: try-with-resource statement will auto close the connection.
}
public static void printSQLException(SQLException ex) {
for (Throwable e: ex) {
if (e instanceof SQLException) {
e.printStackTrace(System.err);
System.err.println("SQLState: " + ((SQLException) e).getSQLState());
System.err.println("Error Code: " + ((SQLException) e).getErrorCode());
System.err.println("Message: " + e.getMessage());
Throwable t = ex.getCause();
while (t != null) {
System.out.println("Cause: " + t);
t = t.getCause();
}
}
}
}
}
Output:
update users set name = "Ram" where id = 1;
Number of records affected :: 1
References
https://www.javaguides.net/2018/10/jdbc-statement-update-record-example.html.
Related JDBC Source Code Examples
- JDBC Create a Table Example
- JDBC Insert Multiple Rows Example
- JDBC Update Query Example
- JDBC Select Query Example
- JDBC Delete Query Example
- JDBC Transaction Management Example
- JDBC Connection to Oracle Database Example
- JDBC Connection to PostgreSQL Example
- JDBC Connection to H2 Database Example
- Java JDBC Connection to HSQLDB Database
- Java PostgreSQL Example
- Java H2 Create Table Example
- Java H2 Insert Record Example
- Java H2 Embedded Database Example
Comments
Post a Comment