-: All right, so let's go ahead and move into Eclipse.
What we're gonna to do is we're gonna make use
of that same project from before,
jdbc-student-tracker.
And in the previous video I showed you
how to open this project and how to import it.
So if you don't have it open yet,
please go back to the previous video and follow those steps.
All right, so this is our student db tracker version one.
I'm going to move into the actual source code,
and I'll actually start with the Java code first.
And I'll actually start with this StudentDbUtil.java.
So remember, this is our helper class
that we use for communicating with the database.
And it's just a regular ole Java class.
All right so here's our StudentDbUtil,
we're gonna make use of something called an instance.
We're gonna have a dataSource,
which basically handles the connection pool.
And then we'll have the jndiName
that we'll use for looking up that object from
Tomcat's JNDI tree.
So here we have this getInstance method.
So again, we're actually gonna make use
of a Singleton Pattern to make sure
that we only have one instance of
this database utility and memory at a given time.
So we're gonna expose this public method
and they can call it, and if we don't have
an instance created, we'll create a new one,
else we simply just return the current
instance that we had.
So this is an example of a Singleton method,
I'm sorry, a Singleton class.
So here's the constructor here for the class.
We make it private StudentDbUtil
to make sure that we only access this
via the getter method above.
And that's our constructor, we simply get our handle
to the dataSource.
And you may wonder,
well what exactly is this getDataSource method?
Well it's defined right here.
So what we do here is that
we need to look at that connection pool.
And we do that by making use of
the JNDI tree and Tomcats.
So JNDI is that Java Naming and Directory Interface,
basically an area of memory
where we can access objects that Tomcat has created.
So Tomcat's created that DataSource
and we can reference it using that code.
So then I'll go ahead and move down to line 41 getStudents.
So basically I want to get a list of students
from the database and use it in my JSF page later on.
So here the getStudents method,
it will return a list of student objects.
So in line 43, I simply create
an empty ArrayList of those students.
And now here, this is just kind of very basic
JDBC code.
So I go through on line 50,
I get a connection to the database.
And then I set up my sequels,
select * from student, and I want to order
by the last name to make sure it's sorted by last names.
Create statement, I execute the query,
it's going to return a result set for me.
Now that I have the result set,
now I just need to process the result set.
So I just need to do a little while loop,
loop over each of those rules in the result set,
and create a list of students.
So here on line 61 through 65,
I simply retrieve the data from the result set.
So I grab the student's id,
firstName, lastName, email address,
and I simply assign those to local variables.
Then I go through, and I just create a new student object.
I just call the constructor for a student object.
Say new Student, then I pass in
id, firstName, lastName, and email,
same values that I just retrieved from earlier.
And then I take that student object,
I just created this tempStudent,
and I add it to my list of students.
So I'll say students.add(tempStudent).
And I just repeat this process for all of those students.
When it's finally done, I simply return students.
So this will basically return an ArrayList of students
back to the calling program.
Finally on line 77 to 79, I just do some clean up work.
I just close off my connections, result sets,
and statement objects.
And that's basically it, there are some other methods
like addStudent and so forth.
We'll get to those later.
This is just kind of version one of the app,
just listing the students right now
or getting the students.
All right, so that's our database utility.
Let's go ahead and look at our Controller.
So this our StudentController.
And remember our StudentController
is basically a ManagedBean that we'll make use of.
So this ManagedBean is set up for SessionScope
because we want to make sure that for a given user
that's logged into our system,
they have their own instance of a Controller
because we're kind of setting some data,
and then using it later.
So we need to make that SessionsScope.
So here, I just have the list of students
as our field.
I have a reference to the database utility.
And here's my no argument constructor
that we always have with ManagedBeans.
In this case, I simply created empty list of students,
and then I also get a handle
to that student database util.
So remember, that student database util is our helper class
for communicating with the database.
I'll make use of that getInstance method
that will give us a handle or reference to it.
Then also we have the students object
that will provide a getter method for us
so a JSF page can get a list of students
or get a reference to the students.
Now where does the heavy lifting occur?
Like where do we actually talk to the database
to retrieve information for the students?
Well that occurs down here on line 31.
So on line 31, we have this public void method loadStudents.
This is the method that the JSF page will call initially
when the JSF page is first loaded.
So the first thing we do is we
clear out the students that we had previously in memory,
and then we get a new list of students from the database.
So I say studentDbUtil.getStudents.
That'll talk to the database on our behalf,
give us a list of these student objects,
and then we can make use of them in our JSF page.
And then I have a normal exception handler.
If there's an error message,
I wanna create this as a JSF FacesMessage
and make it available to the JSF page.
So here, I just create a new FacesMessage
based on whatever exception that I have,
and then I simply add that message to the FacesContext.
So FacesMessage and FacesContext,
these are actually classes from the JSF API.
All right, so that's our StudentController.
Basically load students and then
we can call getStudents a little bit later.
We'll pull this together in the next file here
so you can see how it all works together.
All right, so let's look at our JSF page.
So this is the JSF page that's actually gonna make use of
our Controller bean.
So here is our test-db.xhtml file.
All right so very basic JSF page.
On lines six through eight, basically what we're gonna do
is we're gonna make use of that preRenderView,
so this event.
So basically, when the page is first loaded,
before we render the page, we're gonna say
listener="#{studentController.LoadStudents()}".
So this'll actually call that method.
It'll load the students in memory,
and then we can make use of it.
So on line 21, when we say #{studentController.students},
that'll simply give us a reference
to the object that has already been pre-loaded,
so there's no database work at line 21.
All of the database work occurred a little bit earlier
when we made use of that listener.
And then line 23, we basically just have h:messages,
so if there were any errors or any exceptions
that have occurred in our exception handler,
it would show up here.
If everything was fine, then we won't see
any error messages or anything.
But the real work was there in that page.
So let's go ahead and run this example,
and you know, kind of see how this worked out for us.
So let's simply do a right click,
we say Run As, and the Run on Server.
Okay great, so it runs and it looks good.
So Database Test, and then we have the data.
And then we simply just dump out that ArrayList.
So it returns an ArrayList and we simply dump it out.
So this is all ugly, this is raw.
Don't worry about how pretty it looks right now.
The key here is to make sure
that we could actually get the data from the database.
We'll make it pretty in some of the other slides.
But how do we get this output here?
So let's go look at the student class, Student.java.
It's a real simple ManagedBean, has id, firstName, email,
normal getters and setters.
But I wanna focus on lines 55-59.
Basically we overwrote the toString() method
and we'll basically just
print out information about the student,
so the student's id, firstName, and so on.
This method, toString, is actually called
whenever we dump out the contents of the array,
or the ArrayList.
So the ArrayList will basically say,
"Hey object, give me your toString method,"
and that's how we get the output there.
So, more kind of java one-on-one stuff,
but again, the key here is that
we're able to connect,
we can get data,
and then in the next example,
I'mma show you how we can make this pretty.
All right, so I actually have a file here called
test-list-students.xhtml.
So, this is again, another test file.
What we'll do here is we'll use that preRender again
for calling LoadStudents,
but now what we're gonna do,
is we're gonna make it pretty.
So we're actually gonna make use of
that h:dataTable component.
So this is the same component
that we used in some of the previous videos,
so you've seen this before.
For value, we simply say "{studentController.students}".
It'll call the getStudent,
it'll give us a handle to a tempStudent
as we loop through it.
But now all I wanna do here
is just set up different columns.
So I'll set up a column for First Name,
set up a column for Last Name,
and then do a similar thing,
setting up a column for email address.
And that's all we're gonna do.
So we're gonna create a very simple table
to list out the information
and make it somewhat pretty.
All right, so let's go ahead and run this one.
I'll just right-click,
and I'll say Run As, Run on Server.
Okay great, so here's the data.
So now we have that same data.
It's simply displayed using that dataTable component,
so now it's in a tabular format.
This is great.
So now you understand the basic process
of connecting to a database
and displaying the information out as a table.
All right, so a lot of good stuff here.
So, we covered the application features.
We also took a look at the big picture.
We discussed the student database utility,
our Controller and our JSF page.
So we covered a lot of good stuff here.
In the following videos,
we're gonna pull it all together in our application.
We're gonna make use of cascading style sheets,
and we'll start to hook everything together.
But, we've made some good progress so far
so good job!