Start learning with our library of video tutorials taught by experts. Get started

Java Database Integration with JDBC

Deleting rows with prepared statements


From:

Java Database Integration with JDBC

with David Gassner

Video: Deleting rows with prepared statements

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.
Expand all | Collapse all
  1. 5m 23s
    1. Welcome
      53s
    2. What you should know before starting this course
      1m 20s
    3. Using the exercise files
      3m 10s
  2. 12m 2s
    1. Testing your Java development environment
      5m 14s
    2. Importing a MySQL database
      5m 1s
    3. Creating a database user in MySQL
      1m 47s
  3. 32m 47s
    1. What is JDBC?
      4m 26s
    2. Choosing a JDBC driver
      6m 46s
    3. Connecting to a MySQL database server
      8m 7s
    4. Connecting to a HyperSQL database file
      6m 23s
    5. Executing a static SQL statement
      7m 5s
  4. 17m 42s
    1. Connecting to multiple databases
      6m 24s
    2. Handling JDBC exceptions
      7m 7s
    3. Closing database resources in Java 7
      4m 11s
  5. 47m 25s
    1. Looping through result sets
      8m 23s
    2. Moving the cursor in scrollable result sets
      5m 51s
    3. Limiting the number of fetched rows
      6m 57s
    4. Filtering data with prepared statements
      6m 58s
    5. Calling stored procedures
      5m 48s
    6. Handling multiple values from stored procedures
      5m 54s
    7. Using generic getter methods in Java SE 7
      7m 34s
  6. 45m 23s
    1. Managing data entities with JavaBean classes
      5m 0s
    2. Retrieving a single row as a JavaBean object
      6m 5s
    3. Inserting rows with prepared statements
      8m 2s
    4. Updating rows with prepared statements
      5m 4s
    5. Deleting rows with prepared statements
      4m 9s
    6. Managing data with updatable result sets
      6m 6s
    7. Using a persistent database connection
      6m 43s
    8. Committing and rolling back transactions
      4m 14s
  7. 9m 35s
    1. Getting the DatabaseMetaData object
      3m 40s
    2. Getting column and data type information
      5m 55s
  8. 50s
    1. Next steps
      50s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
Java Database Integration with JDBC
2h 51m Intermediate Nov 28, 2012

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.

Topics include:
  • Importing a MySQL database
  • Connecting to databases
  • Handling JDBC exceptions
  • Looping through result sets
  • Limiting the number of fetched rows
  • Filtering data with prepared statements
  • Calling stored procedures
  • Inserting, updating, and deleting rows with prepared statements
  • Using a persistent database connection
  • Committing and rolling back transactions
Subjects:
Developer Databases Programming Languages
Software:
Java
Author:
David Gassner

Deleting rows with prepared statements

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.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Java Database Integration with JDBC.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked