Java JDBC Tutorial – Part 4: Deleting Data from a MySQL Database

Java JDBC Tutorial – Part 4: Deleting Data from a MySQL Database

Hello. In this video, you will learn how to
use Java JDBC to delete data from 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, then please look at my
other video “Connecting to a MySQL Database with Java.” In this example, we will use the employees’
table. This table has columns for employee ID, last name, first name, and e-mail address.
Let’s switch over to the MySQL tool so we can review the data. As you can see, we have
the employees’ table in place. Make note of the entry for David Brown. We will delete
this employee from the database. The SQL command that we’ll use to delete an employee is
delete from employees where last_name=’Brown’; Let’s move and try development environment
and get started. We’ll start with this main program, that’s going to have three variables
for the user, password, and the connection information. The first thing we’ll do is
we’ll get a connection to the database by saying DriverManager.getConnection, passing
in the params. Next, we’ll create the statement using myConn.createStatement. Then we’ll
define the SQL query string. In this case, delete from employees where last_name=’Brown’ Now, actually execute that SQL statement by
saying executeUpdate and the database will return the number of rows affected. Also,
I will print those out. Then finally, I’ll just print out “Delete complete.” so that
we know that I’m done. Now, let’s run our application. I’ll right
click; I’ll select “Run As”, “Java Application.” Now, let it execute. Now,
it will print out the number of Rows affected and Delete complete. Now, let’s move over
to our SQL Query tool and very the results. If you note here with the previous entry,
we had a listing for a David Brown. I’ll refresh this Query, and now note that David
Brown is gone. A Java Application was actually successful in deleting that entry. Great, so in summary, you learned how to use
Java JDBC to delete data from a database. If you would like to download the source code,
please visit

Danny Hutson

21 thoughts on “Java JDBC Tutorial – Part 4: Deleting Data from a MySQL Database

  1. hi,am able to connect to my database but its also displaying this message " Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification " This is the url i used "jdbc:mysql://localhost:3306/demo","root","password".

  2. Hello sr, the videos are really help. thank you so much
    But I have a question ,From the video you only have one table, If I want to delete a row that row has a foreign key from the other table, how can I do that??? just set it to null??

  3. Thank you, thank you! Omg, just the entire world of Java/SQL integration just exploded in my brain. My god, all you do is string SQL commands directly through JDBC. Omg. Genius, genius, genius, whoever invented this. Thumbs up to you, Sir.

Leave a Reply

Your email address will not be published. Required fields are marked *