In this article, we will use JDBC API to update a record into newly created table in MySQL database from Java application (i.e.; using Statement Interface)
1. Pre-requisite:
- Java JDK 1.8.0_77
- MySQL database 5.5.16
- Eclipse Luna IDE 4.4.0
- mysql-connector-java-5.1.38.jar file
2. Database parameters:
Let us move on and code an example to connect MySQL database from Java application to update a record using JDBC API. But before that, we will list down required things to connect database
- database server IP or address (localhost)
- sever port (3306)
- database name (benchresources)
- username (root)
- password (root@123)
Note: All bold are database values to connect MySQL database
3. Updating a record using JDBC Statement Interface
- As we are ready with required things to connect MySQL database from Java application
- We can use one of the methods from Statement Interface to execute “update record” SQL query
- execute(String sqlQuery);
- executeUpdate(String sqlQuery);
- executeQuery(String sqlQuery);
- Let us code simple example using Statement Interface
UpdateRecordUsingJDBCStatement.java
package in.bench.resources.mysql.db.example; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class UpdateRecordUsingJDBCStatement { public static void main(String[] args) { // variables Connection connection = null; Statement statement = null; // Step 1: Loading or registering MySQL JDBC driver class try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch(ClassNotFoundException cnfex) { System.out.println("Problem in loading MySQL JDBC driver"); cnfex.printStackTrace(); } // Step 2: Opening database connection try { // Step 2.A: Create and get connection using DriverManager connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/PLAYER_INFO", "root", "root@123"); // Step 2.B: Creating JDBC Statement statement = connection.createStatement(); // create SQL query to insert new PLAYER info String sqlUpdateQuery = "UPDATE PLAYER SET AGE = 47 WHERE PLAYER_ID = 5"; // Step 2.C: Executing SQL & retrieve data into ResultSet int sqlQueryResult = statement.executeUpdate(sqlUpdateQuery); System.out.println(sqlQueryResult + " indicates PLAYER info updation is successful"); } catch(SQLException sqlex){ sqlex.printStackTrace(); } finally { // Step 3: Closing database connection try { if(null != connection) { // cleanup resources, once after processing statement.close(); // and then finally close connection connection.close(); } } catch (SQLException sqlex) { sqlex.printStackTrace(); } } } }
Output:
1 indicates PLAYER info updation is successful
4. Download:
Read Also:
- JDBC Tutorial index
- Introduction to JDBC
- Different types of JDBC drivers
- JDBC Driver list for all leading databases
- Basic components of JDBC
- JDBC connection steps
- MySQL database connection steps
- Oracle database connection steps
References:
- https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-connect-drivermanager.html
- http://www.benchresources.net/spring-jdbc-introduction-and-jdbc-example-without-spring/
- https://docs.oracle.com/javase/tutorial/jdbc/basics/gettingstarted.html
- https://docs.oracle.com/cd/E11882_01/java.112/e16548/overvw.htm#JJDBC28025
- http://docs.oracle.com/javase/tutorial/jdbc/basics/index.html
- https://en.wikipedia.org/wiki/JDBC_driver
- http://www.devx.com/tips/Tip/28818
- https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#forName(java.lang.String)
Happy Coding !!
Happy Learning !!