so we're moving on now
so now that we've created that view in
the previous video we could actually
change the query artists for songs
method you could actually do that and
make that change then use that view if
you wanted to but we won't do that now
what we'll do instead is will create a
new method for this the user the
datasource class could fall back to
using the query artists for song method
if the creation of the view failes or
calling the new method results in a
error now we can query a view just as
we query a table so lets start
down here in terms of our querying a view
we can do something like....
...
we go back and do that i'll paste that
into db browser just to confirm the
title equals and as
with previous videos let's go with ones
we've used before so go your own way you
can see we get the same result Fleetwood
Mac and four rows returned this time
they're ordered by album title because
we ordered the records that way when we
created the view
alright so now that we know that the
query works let's start making use of it
what we'll do is we actually start
coding this particular query let's go
back to our data source and we'll add
another line here is on line 84 and
we're going to start by putting....
...
....alright so now
that we've done that
let's go and create the method that is
going to query the view
ok and delete that now so....
...
getting a little bit hard to read with
all these squiggles and I guess that's a
good thing in the sense that it is
notifying you got some duplicate code it
is good to fix it and remove these
warnings what I'm going to do
temporarily is turn off the duplicate code
checking because it's really starting to
interrupt with the way that thise code
looks but I wouldn't suggest you
normally turn off the duplicate code
checking because you want intelliJ
to be telling you if you are duplicating code
but to turn it off I'm going to do now
click on preferences and click on editor
click on inspections then want to scroll
down here to general expand that and
here under duplicated code want to take
the check box off that click on OK and
that removes that temporarily for now
bearing in mind that we will need to do
suggest you actually enable in a future
point because its good to be checking those
types of things normally alright so in
relation to this method that we create
query song info view the only real
difference between this one and the
query artist for song method is the query
string we're using now again I just
disabled the duplicate code temporarily
but you shouldn't refactor the code
that loops through the result
into another method that's something
that i will leave you as an exercise to do
alright so let's go back now and
call the query song info view method
from our main class main method
so we going to go ahead and write
some code so it's going to be...
....
...
alright let's just try running that you
can see here we've got our 4 lines
of the output got it working on
so note what i did add here
on line 55 is this is empty method to
check whether any information was
returned from the song and just as a
note here we should really have been
doing that in main dot java for all
our queries rather than checking for
null since we always at least create the
list in the data source methods
I mean no real harm done down but I just meant that
with no records were returned we wouldn't
see the message printed to the console
using . is empty is a much better
way of checking for that just
confirms just try the other songs that
we've been working on in this section of
the course..
...
...and one more
...and again
capitalization is important here
she's on fire
that's a good confirmation that
couldn't find the artist of the songs that is
returning can't find a match
capital o we get the right
response she's on fire
okay that's good so getting the same
results from our view that we were when
we called the query artist for song method
that's really all there is to working
with views now our code is decent at the
moment but there's one more important
improvement we can make in fact the way
we query the database now isn't best
practice sql statement have to be
compiled into a form that the database
understands now when we use a statement
object to perform queries statements
actually compiled every time we perform
a new query now in this little
application doesn't matter but if you
are using a statement objects in an
enterprise application servicing
thousands of connections that would
definitely impact performance and also
the way that we're building a query
strings makes a database vulnerable to
hacking attempts
I'm going to finish the video here but
in the next video I'll actually
demonstrate why that's the case then
we'll start looking at prepared
statements which is a way to help
improve performance to sort of
counteract the problems that i mentioned
just now with the queries being
compiled each time we perform a new
query so see you in the next video