Viewers: in countries Watching now:
Whether you're building a web- or desktop-based application with Java SE or Java EE, many Java applications need to integrate data from a relational database. This course describes how to read and manage data from relational databases such as MySQL and SQL Server using the Java Database Connectivity (JDBC) API.
Author David Gassner explains how to choose a JDBC driver and connect to one or more databases. He also provides detailed instructions on reading, selecting, and updating data; calling stored procedures; managing data via JavaBean classes or with prepared statements; and working with metadata.
After inserting and updating, the third operation you need to know how to do is deleting. In this version of the project DeleteSQL, my main class is collecting an adminID an integer from the user. I'll run the code so far and show that it starts by displaying the data and then I can enter a value and then nothing happens. I'm going to add code to delete a row from the existing data. I'll go to my AdminManager class in this version of the project. I have created the delete method. The delete method only receives an integer argument.
It doesn't need an entire JavaBean object, because all it needs to know is the primary key of the row you want to delete. As with previous exercises I have seeded this file with the SQL command I'll need, DELETE FROM and in the name of the table, WHERE and then the primary key column and a placeholder, which for this table will be an integer. I'll complete the code by setting the placeholder value. I'll place the cursor before the return statement, and I'll call this statement objects setInt method. I'll pass in the parameter index of 1. This SQL statement only has one parameter.
And then I'll pass in the adminId as the value I want to filter on, and that's all I need. I'll execute the update, but as with the update statement I'll look at the value returned by the execute update method and integer to find out whether the operation was successful. I will declare the variable named affected, and I'll get its value from the statement object's executeUpdate method. Now remember, the value that's returned from this method represents the number of rows that are affected by the SQL command. So if it's 1, that means that a row was successfully deleted, and if it's not 1 that means that probably the row just didn't exist. So I'll do a little bit of if else programming.
I'll set the condition to affected has a value of 1. And if that's true, I'll return true, and if it isn't true I'll return false. And I no longer need to return statement at the end of the method, so I'll delete it. So that's the entire delete method. I'm receiving a primary key argument in integer. I'm taking that value and passing into the SQL statement through the prepared statement object, and I'm executing the command. And then I'm evaluating whether it was successful and returning either true or false. I'll save those changes and go to my main class.
I'm already getting the value of the primary key of the row I want to delete. So all I have to do is call my new delete method. I'll create an if else block, and I'll call AdminManager.delete. I'll pass in the adminId variable, the value the user provided, and if that value is true, I'll output a Success message. And if it's false, I'll output an error of nothing to delete. And now I'm ready to test my application. I'll run, I see the existing data has two rows, the original row that I seeded the database with and a row that I inserted in a previous exercise.
I'll pass in the primary key of the row I want to delete, the second row that I just inserted previously. I'll press Enter or Return, and I get back the message Success. Now I'll run the code again right away, and I'll see that that row is gone, but I'll try to do it again just to make sure that the logic of my application makes sense. And now because the affected value returned from execute update is zero, that is, no rows were affected, I returned false from that method, and in the main code I output the error message Nothing to delete.
So the process of creating, updating, and deleting data with SQL statements is all pretty much the same. You can manipulate your database with whatever SQL commands you know and that your database supports. I have kept things really simple in these demonstrations doing plain vanilla inserts, updates, and deletes that would be accepted by all databases. But again, you can use what ever SQL Syntax your Database Management System supports. That SQL is passed directly from your application through the JDBC driver to the database where it's actually processed.
And as I have previously shown, you can choose from either SQL statements embedded into code like this or stored procedures that are built directly into your database.
There are currently no FAQs about Java Database Integration with JDBC.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.