Instructor: Hey.
In this video, I'm gonna show you how to delete a student
from a database using JSF.
We'll cover the following topics.
We'll first take a look at a demo of deleting a student,
then we'll look at the big picture.
Next, we'll take a look at the sequence diagram,
and finally, we'll pull it all together
with a code walk through.
And I'll show you all the code required
for deleting a student.
Alright, let's go ahead and get started.
So, here's a quick demo here of the application.
So, I'm in my browser and I'll drop in the URL
for the application.
So, again, a lot of this looks familiar.
However, on the action column, we have a new item
here for delete.
So, we have the option to delete a selected student,
so I'll go through here and I'll delete Lisa Mangano.
It'll prompt me.
I have the option of deleting or canceling.
Here, I'll just do cancel.
Just to make sure that, hey she's still there,
we didn't delete her.
But now I'll go ahead and delete,
and then I'll hit the okay button
to actually make the delete go through.
So I'll say okay, and this will actually
delete the student Lisa Mangano.
And we should see the screen updated
with that entry removed.
Okay, great, so notice Lisa is no longer
in our list of students.
She's been deleted from the database.
Alright, let's do a similar thing here with Maxwell Zeno.
So I'll say Delete, I'll hit okay,
and now Maxwell Zeno is no longer on the list.
So that's basically how the application'll work.
We'll have this new column here for Delete
for each one of the students.
And I'll show you all the code on how to pull this together.
Alright, so let's take a look at the big picture.
So over on the far left, we have our
JSF page listing the students, and again,
we have that new column there for Action
for deleting a student.
We hit delete, it goes over to the controller,
the controller will delegate this call-off to our
Student DbUtil, that's our database helper,
that'll actually delete the data from the database.
So let's take a look at the Action link a little bit more.
So in this Action link, for each student, or each row,
it has the current student id embedded in the link.
So when we click this link, it'll actually
delete the student from the database.
Well, actually prompt the user first, but if they hit okay,
then it will actually delete the student from the database.
Now, drilling down a little bit further,
here's our sequence diagram.
So on our page, list students, they hit the delete link,
we'll call the delete student method
on our student controller.
This person will also delegate this call to the student
DbUtil, which also has a method for delete student.
Alright, so let's go ahead and move into Eclipse.
The first thing I wanna do is close the project
from the previous video, so student DB tracker V3,
I'll just right click and I'll choose close project.
Alright, so now this project is closed,
I'd like to import a new project, so I'll say file, import.
And I'll move to general.
And then I'll say existing projects into workspace.
Now I'll go ahead and browse over to our directory
that we've used before,
JSF for beginners,
J, JDBC source code,
and we'll choose version four, delete student.
And then we'll hit open.
Now we should have this JDBC student tracker, V4.
And then we'll click finish.
Alright so over on the far left, we should have this new
project here, JDBC student tracker V4.
So now let's go ahead and look at some of the code here.
Some go into this student tracker V4 project,
I move to the web content directory
and I'll take a look at this list-students.xhtml.
So this is the page that's gonna list out
all of the students, of course, and it'll have a new link
for deleting a student.
So just moving on down, here's our data table.
And here's our action column.
So again, we have the column header for action,
we have a link here for update,
we saw that in a previous video.
And the new item here is on lines 76 through 78.
That's for delete.
So this is a fun link here.
When they click delete, we're first gonna prompt them,
yes or no, we're actually gonna make use of some Javascript.
Whoa, easy.
So this is not Java code, not JSF code, this is Javascript.
Basically this'll just pop up a little dialog
for them to hit cancel or okay.
If they hit okay, then we'll go ahead and process
this student controller, delete student.
If they hit cancel, then our action code
will never be executed, so line 77 is Javascript,
line 78 is a call to our controller bean.
Alright, so let's go ahead and look at our student
controller, our managed bean.
Alright so here's our managed bean, student controller.
What I wanna do is use this Eclipse trick again,
the quick outline, and I'll go ahead and look at this
method here called delete student.
So here's a method here in our student controller,
our managed bean, they're gonna pass in a student ID
to delete, and again, all we're gonna do is simply delegate
this out to the actual database utility so our database
utility also has a method called delete student.
So let's go ahead and look at the coding here
for our student DbUtil.
So I'll expand the list here and there's a method here,
delete student, so let's go ahead and double click this
and open it up.
So right now I'm in StudentDbUtil.java
and here's the method, and again, remember,
Student DbUTil is for interfacing with the database.
So here I have this myConn equals get connection
and on line 184, I have the sequel.
Delete from student where ID equals question mark.
That's the little placeholder that we're gonna fill in
with the parameter value.
So lines 186, I prepare the statement.
And then I'll go ahead and set that parameter value,
so I'll say set at one, comma, student ID.
Student ID's the parameter that's actually being passed
into this method call, and then in 191,
I simply execute the statement, so this'll actually delete
the student from the database.
Alrighty, so now let's go ahead and move back over here
and let's actually run the application.
So I'll move down to list-students.xhtml,
I'll just do a right click, I'll say run as,
I'll say run on server.
Alright so here's the app, it comes up in the browser
inside of Eclipse.
Again, I'm simply gonna copy this URL,
just do a right click and choose copy.
I'll move into Firefox and I'll drop in this URL
and paste it.
And now here's the application.
So again, over on the far right, we have the action
for update and delete.
So I'll go ahead and delete this person, Andrew Whittaker.
So I'll hit delete and I'll say okay.
And now I have this other student I wanna delete,
let's see, Bill Neely, I'll delete this person
so I'll just go ahead and hit delete
and I'll go ahead and hit okay.
Okay, great, so we deleted two students
so we only have three left, so this works out really good
so we tried out the operation to see how it works.
Now again, just like in the previous videos,
I'm always a little like, I'm not sure if this really
worked or not, so I always like to go back to my
mySQL tool and actually look in the database itself
and just to verify, so let's move over to the mySQL
work bench, I'll go ahead and log in
and bring up the application here.
And so now we're here so we'll go ahead and select
our schema, student tracker,
we'll move into tables,
and we'll move to this student table
and then I'll just right click and I'll say select rows.
So hey, this looks really, so we only had three students
in the web and the same three students here,
so this works out just right, so our application really
is deleting the data from the database.
(sighs)
Again, another sigh of relief.
Alright guys, this is really good work, good job.
Great, so let's go ahead and wrap up this video.
So we took a demo of the application up front,
we looked at the big picture, the sequence diagram,
and then finally we pulled it all together
by doing a code walkthrough so you saw every piece
of code required for deleting a student from a database.
So this is all really good stuff, good job.