-: Alright, so let's go ahead and start up.
So the first thing I want
to do here is close the previous project.
So I'll go ahead and select project v2.
Just do a right click and I'll say close project.
Alright so that closes v2.
Now what I want to do is open up v3.
Which has the update student information.
So I'll do an import.
I'm gonna go up to the Generals.
Select existing projects into work space.
I click on next.
And I'll browse over
to where I have the jdbc-source-code downloaded.
So I know it's jsf-for-beginners.
I have jdbc-source-code.
That's the same directory
that we used in the previous video.
Here I want to choose version-3-update-student.
And I'll go ahead and click open.
OK, great so it's going to list the projects.
So you should see a jdbc-student-tracker-v3.
That's the correct project that you want
to have open for this import.
And when you're back click on finish.
Alright great, so on the left hand side
we have this new entry here jdbc-student-tracker-v3.
So this is the one for update student,
and this is the one we'll use in this video.
Alright, so lets go ahead and take a look
at our source code here, actually our JSF files.
And I'm going to go into the web content directory.
And take a look a list-students.xhtml.
So this is the same file that we had before.
This is where we will list out all of the students.
On lines 6 through 8 we actually load the students.
So we can list them out.
And we have this button for adding students.
We saw that in a previous video.
And now we have our table that shows all
of our students to the user.
So now we're gonna keep all the same columns.
And we're gonna add this new column here called action.
So on lines 67 through 75.
This is our new action column.
So we'll have the actual column name, action.
And then we'll actually have a link.
So for each student, we're gonna have an update link.
And the action will call the studentController loadStudent.
And will pass on that's student's unique ID.
So remember we're looping through all the students.
And we're putting them in the list or table.
So each link will have that unique student's ID.
So we'll make a call to studentController dot loadStudent.
And we'll pass on that student's ID.
And I'll show the coding for that in a second.
Alright, so lets go ahead and look at our StudentController.
So I'll move over to my source code directory.
Java Resources, source, package, StudentController dot java.
And so remember our controllers are ManagedBean.
We have it setup as SessionScoped.
So this is our StudentController.
And what I want to do is go look at a method here.
So we have a lot of methods in this class now.
I make use of using clips trick called Quick Outline.
And what Quick Outline will do.
It will show you a list of all the methods in your class.
And so it allows you to easily navigate
to one of those methods.
So I'll look at this method called loadStudent.
And that's the version that takes an integer ID.
So I'll go ahead and select this method loadStudent.
Singular.
So on line 75 here's loadStudent.
It takes a parameter and studentID.
What this method will do is it'll actually go
to the database grab a student put it in memory
and make it available for the other JSF pages.
So on lines 80, 81 I might actually use my
studentDbUtil and say getStudent and I pass
in that student ID.
It'll return a student object that it read
from the database.
Now what I need to do is take this and put it into memory.
So I need to make use of one of these helper classes here.
Called FacesContext, so actually my external context.
So this is really a area of memory
that I can use for storing data.
So I get a handle of to those external context
by saying
FacesContext, getCurrentInstance, getExternalContext.
So again just a helper class for getting access to some
of the servlet data.
OK great so I have this externalContext
a little helper class I can use
to get access to servlet data.
So here I'll say externalContext getRequestMap.
This is going to get me a map or hash map
of request attributes.
So effectively what I can do is add additional data
to this web request.
So I can say requestMap dot put student comma theStudent.
So in quotes I have the student.
That's the actual label or name we'll use for this object.
And then theStudent is an actual reference to the object.
So think of it as adding a foreign parameter.
But instead we're adding a request attribute.
So once we put the data there.
Then we forward it over to our update-student-form.
Our form will actually read the student object
that we have in memory and pre-populate the form.
So that's basically how we have
to do some behind the scenes work.
Grabbing the object.
Putting it into the requestMap.
And then we'll send it over to the update-student-form.
So they can view it or show it, pre-populate it.
Alright so lets go look at studentDbUtil.
So StudentDbUtil and it has a method
in here called getStudent based on a given student ID.
So we'll go ahead and click on this method.
So this is where we'll do the database work.
So our normal database method is here.
Line 114 we get a connection to the database.
We setup our sql query select star
from student where id equals question mark.
And we'll gonna fill in that question mark.
Based on the parameters being sent into this method.
So I prepare the statement.
Then I say myStmt dot setInt one comma studentId.
So that will set the value accordingly.
And I'll go through and execute the query.
So I'll say myStmt dot executeQuery.
It'll return back a result set.
So now this should be for one student here.
So I'll say move forward mRs dot next.
And I'll just grab the information off that result set.
So I grab the student's ID, first name, last name,
and email address.
So I'm grabbing that information from the database.
And I can use that to create a new student object.
And then finally I just return the student.
OK great so that's kind of how the low level stuff works.
As far as getting the student from the database.
Then we put it in our memory and then we send it
over to that update-student-form.xhtml.
Alright, so lets go look at that form
and see how that works.
So I'm in this update-student-form.xhtml.
So this is the update form.
It's very similar to the add form.
I just copied it and made a small modification to it.
So here's my form line 30.
And again it's very similar to what we saw before.
So we have our label for first name.
And then we have an input text for first name.
Now remember JSF when they first load a form.
Then they'll actually call the getter methods on the beans.
So since we have this student object
that's already been created and populated in memory
in that request map.
Then it'll call that same method.
It'll use that same object.
And it'll call the get first name, get last name.
So again JSF will call the getter methods
when it first displays a page using beans.
So that's basically how we handle the pre-populating
of this form.
Alright so lets go ahead and look at our save button.
So when they press save we want to update the student.
So on line 43 and 44 we have our save button.
So the label is save that's the value.
And the action is the method that we'll actually call
when we hit the save button.
So here I'll call studentController.updateStudent
and I'll pass in a reference to that student object.
So again the same student object that we have
on this form any modifications that they've made.
Then we'll make a call there.
And so again when you save.
They'll call the setter methods on that bean.
And then again pass it into the controller accordingly.
So any new data that the user entered will be stored
in that student object as we pass it over for that call.
Alright so lets go look at the StudentController
for updateStudent.
So we're moving back into our managed bean StudentController
There's a method here called updateStudent.
They pass in a student object.
This is passing from that JSF form.
And what we're do is simply delegate this call
off to our helper again studentDbUtil.updateStudent.
And we'll pass in a reference.
And once that's complete.
We simply return a list of all our students.
So lets go ahead and dig into this studentDbUtil.
And look at the coding for it.
So back over here studentDbUtil.
There's a method here called updateStudent.
So we'll bring that up.
So right now I'm in a different class here.
I'm in the StudentDbUtil class.
And here's our update student method.
And this is where we do all the jdbc work.
So again we get a connection to the database.
We setup our string.
We're gonna update student.
We're gonna set first name, last name, email.
Where a certain ID is set.
So where ID equals question mark.
This is going to be based on that parameter.
That's being passed into this method.
So again we prepare the statement.
And then we dot the real work here.
We set the parameters.
So we set parameters one, two , three, four,
first name, last name, email, and ID respectively.
And then finally we execute the statement.
So that performs the update in the database.
And so we're good to go there.
So that's basically all the database work there
for doing an update on a student.
Alright so lets go down and run the application.
So I'll go to my list-students.xhtml.
I'll just do a Run As and I'll say Run on Server.
Alright so here's my app in the browser inside of Eclipse.
I'll just copy this url.
I'll just right click and I'll select copy.
And I'll move over to my real browser
and drop it in and paste.
And now I'll take a look at the application.
So here's the app.
I can go through and select one of these users here
and perform an update on them.
So I'll perform and update
on the first user here Maxwell Dixon.
So I'll go through and I'll change his last name to Zeno.
Just making things up here, having fun.
And I will also update his email maxzeno@luv2code.com.
Alright great so once we save this user.
Then we can go look at the update here.
So here on our list.
Notice at the bottom Maxwell Zeno.
Again he's last because we're sorting by last name
and Z's last there.
His updated email address maxzeno@luv2code.com.
So this works great.
So our data is actually being updated and stored
in the database accordingly.
So good job, good stuff.
Alright so one thing that I want to do is go through
and verify this in the real database in the background.
Right, cause again, kind of like the previous video.
We didn't really trust it.
So lets go ahead and start up MySQLWorkBench.
And we'll take a look at this code or this data
in the background.
Alright so here's our system here, up and running.
Lets go ahead and look at student_tracker.
Go into our tables and look at the student table.
And then again we'll just do a right click on this table.
And we'll say Select Rows.
And then we'll see the output.
So a couple of things here.
So one see that Maxwell Zeno that was updated accordingly.
That's good.
And we also see this from the previous demo earlier
of Lisa Mangano, funnylisa@gmail.com.
We can kind of feel rest assured here
that things are working right as expected.
And we are actually updating information in the database.
So, ah sigh of relief, so good job
So in summary here we took a look
at a demo of the application.
We also saw the Big Picture.
The Sequence Diagram and then we
also went through a Code Walk Through.
So we saw all the code required
for actually updating a student in the database using JSF.
Good job.