In this video tutorial, you will learn how to update data in a database with Java JDBC.
Please subscribe 🙂
Video Transcript
Time – 00:00
Hello. In this video, you will learn how to use Java JDBC to update data in a database. In regards to prep work, you must have a database installed. This demo will use MYSQL. You also need to have the database driver configured. Again, we’ll use MYSQL. If you need help on how to do this, please look at my other video, Connecting To MYSQL Databases with Java.
Time – 00:30
In this example, we will use the Employees table. This table has columns for employee ID, last name, first name and email address. Let’s switch over to the MYSQL tool, so we can take a look at some of the data.
Time – 00:50
Here’s our sample table that we have with sample data already in place. What I’d like for you to do is make note of David Brown’s email address. He is currently listed as davidbrown@foo.com. We’re going to change this email address to make use of luv2code.com.
Time – 01:13
His new email address is demo@luv2code.com. The following sql can perform this update. We will use this sql update statement in a Java application.
Time – 01:26
This slide outlines our Java development process. First, we will get a connection to the database. Then we will create a statement. Finally, we will execute the sql update statement. Let’s move onto our development environment and get started.
Time – 01:46
We’re going to start with a simple Java program. The program is going to start out with the url, the connection to the database, the user ID and password.
Time – 01:56
The first thing that we’ll do is we will get a connection to the database. Then we will create a statement. Then we’ll actually execute the sql query. This is the same query that we had from the previous slide.
Time – 02:12
Then we’ll actually do an execute update. This will actually send the query to the database and perform the update. Finally, just to give us some debugging information, we’ll have a print-line statement to say update complete.
Time – 02:27
Let’s go ahead and run this application. Run as Java application. It says update complete. Great. Let’s go to our SQL tool to verify the results.
Time – 02:42
In the SQL tool, I just simply do a refresh and note the new email address for David Brown. It’s now demo@luv2code.com. That works out, and we’re actually successful with updating data using Java JDBC.
Time – 03:00
This wraps up the presentation. If you’d like to download the source code, please visit the website luv2code.com.
Source Code: Driver.java
[code]
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Driver {
public static void main(String[] args) throws SQLException {
String url = "jdbc:mysql://localhost:3306/demo";
String user = "student";
String password = "student";
Connection myConn = null;
Statement myStmt = null;
try {
// 1. Get a connection to database
myConn = DriverManager.getConnection(url, user, password);
// 2. Create a statement
myStmt = myConn.createStatement();
// 3. Execute SQL query
String sql = "update employees set email=’demo@luv2code.com’ where id=9";
int rowsAffected = myStmt.executeUpdate(sql);
System.out.println("Rows affected: " + rowsAffected);
System.out.println("Update complete.");
}
catch (Exception exc) {
exc.printStackTrace();
}
finally {
if (myStmt != null) {
myStmt.close();
}
if (myConn != null) {
myConn.close();
}
}
}
}
[/code]
Download Source Code: jdbcdemo-update.zip
Hi luv2code.com,
You did all the very good video
If you could name the Class respectively will be more help full.
String sql = “update employees set email=’demo@luv2code.com where id=9”;
missing ‘ at the end of ‘demo@luv2code.com
String sql = “update employees set email=’demo@luv2code.com’ where id=9”;
Thank You
Kenneth
Hi Kenneth,
Thanks a bunch for catching that typo. I’ve updated the blog post. Thanks again 🙂