Detailed Notes
We are continuing a series of tutorials focused on Spring Boot. The crux of the project is to convert/migrate an old JSP/Tomcat application to a modern Spring Boot application and Java 7/8 to a current version. In this episode, we look at connecting up JDBC Temaplte and accessing a database.
You can learn more through our online classes at https://school.develpreneur.com and register for free. Registration will add you to our email list, and you will periodically receive coupons for courses and notifications of the latest releases.
Transcript Text
[Music] well hello and welcome back we are continuing essentially this little project is rep platforming in a sense I guess an older Java application so we're going to take an old web app that we ran through Tomcat uh patchy Tom Cat and we're going to upgrade it to use spring Boot and so that's what we've been exploring now the last time around we we showed how it's very easy to set up both a uh like a rest type of approach or a web type approach we can put we can have our API we can have our web pages all of that running fairly simply through our our various controllers this episode we're going to focus and actually it's this episode will probably be a multiarterial into this uh but we're going to actually connect this thing up to some data now I have an existing database and you can see some examples of that uh but what we're going to do is our examples of just creating a database we're going to use Maria DB and we're going to set a few things up here to get ourselves uh configured properly so that we can actually talk to our data base what we're going to end up doing the goal is we're going to have our little uh we're going to add a new mapping called test uh for right now we're going to we'll give it a parameter just because um so we could give it a parameter of let's say table which is going to be our table name and then uh it's not going to be required because we're not going to use it right now we will get into that a little later and I've got this thing called there's a table I've got that I'm calling opportunity actually the table I think it's opportunities um but the opportunities tables got some data in it so what we're going to do is we're going to list it for a specific user list all the records for a specific user and we're going to go just get a bu ID get a specific Row in the database and then print those out what that looks like is uh pretty straightforward so if I come in and hit it hit that page which is just here it's just if you can see up here that was called test and if I hit that page then we're going to see here when I do my uh get by ID I'm going to print that item and that is here which is this big opportunity thing right here and we'll show how to do that and then I'm going through and I'm just listing the title of each opportunities which is each of the opportunities which is down here now connecting to database you can use uh hibernate you can use jpa there's some things you can do that can make this very simple you don't have to get generally speaking you can you can have some crud stuff thrown up crud being create remove update or I'm sorry retrieve update and delete so you can have that basic functionality with the table you can have that put together in 10 or 15 minutes we may cover that in another one um in our case we're going to do a lot of custom stuff so it's just easier and because that's how it was done before we're going to use what's called jdbc template now with that uh we're going to have our jdbc connector one of the things that we do need to do is we're going to go to our project if we haven't already and we're go to starters and we're going to make sure if we do jdbc we're going to make sure that we have the jdbc API and we're also going to need it for in this case our Maria DB driver so if I add those two it's not going to do anything in my case because nothing has changed so I'm just going to cancel but you go ahead and add those in and that's going to make sure that you get a couple of key pieces one of the things that we're going to have is we're going to have some application properties and we're going to need to make sure that we set up these so that they connect to our database now those are pretty straightforward so what we're going to do is give it our username give it a password and these use spring. datasource and that's. URL to give us the in this case the jdbc link uh this is actually in our case instead of my SQL it's going to be Maria DB and then we're going to give it a path I can keep it pretty simple so I'm just going to go here the default port number here's my database name got to give it the driver so spring data source driver class name and then we've got our and then there's this thing uh I don't really need right now that was actually a leftover for mother that's if you're going to use jpa hibernate you would need that in this case it's going to ignore it because we don't need it now that is to effectively connect to your database now to actually do all this stuff to connect to that we have a couple of classes we're going to make and this is where the power of using objectoriented and making sure that you properly you extract things and or abstract things and put things where they need to be can help because what I'm going to end up doing and what I've done here is I've actually basically just copied about six different files out of the older version because they all connected all I have to do is change up a couple of drivers or a couple of imports here and there and then boom I'm off and running so let's start with the data itself so in this case I've got this thing called an opportunity this is a table we're going to do and so what I'm going to do or the table we're going to work with and what I'm going to do is I am going to have a object a class that will be objects that is going to mimic or basically be my store for that data within the application so I'm not going to necessarily always be directly dealing with the database what I want to do is be able to load data from the database essentially into memory in object that I'm going to work with in my application and then with that I'm going to be able to you know wheel and deal with them and it's basically now an MVC so this is my model this is where the data lives the controller is the back end that's going to basically save stuff or retrieve stuff and then I've also got a view that's going to say hey this is how I'm you know how I'm going to be able to view it and we'll talk about the view at at another point because that's really that's the uh the the hello controller right now our view is nothing because we just spin something out and we have something in our control panel down here or in our console so what we're going to do uh to make it simple and that's where a lot of these are each of these fields that we're doing is a column in the database so if I flip over to my database if I I can look at my table opportunities I know it's a bit small but it's just basically so you can see that hey I have got this opportunities uh table here's a columns I have I've got an ID a title summary blah blah blah got several different columns and so I'm going to take that and I'm going to model that into a you know each row into a record in my database via this model class in this case it's a very simple one it's just I've got an ID as my uh is an integer the rest of stuff is just straight up strings even though I've got like a date and some stuff like that I'm not putting any special types so the easy way to do this is you generate those properties those are you're going to have your in each case you're going to have a getter and a Setter and then at the end here I just override two string I can honestly you can generate these super fast if you don't already know if you are in eclipse or something like it and I I know that other ID IDs have it this is just the specific way Eclipse does it but if you come in here you could say generate Getters and Setters uh and so it's this one's already generated all of them if I take it like this though let me just do that and so if I do this so if I just enter my properties and I say generate Getters and Setters is going to say okay which Fields do you want and you can't I don't how well you can see this because it is small text but it is basically just listing all of my Fields here so I can pick one or two or all of them and then it'll tell me where I can place it and it will go generate so if I do select all and generate boom I it just generated Getters and sitters Setters for each of them so like example you can see like here's where it says get amount and somewhere else here's where it has set amount so I can get that generated real quick I can also do uh from that I can also do a two string that's another one that I can generate very quickly so if I do it already has one but uh what it does is it just puts the class name if you can see the class name up here and then just walks through each of the Val values and prints them out so it's pretty darn easy to generate your model real fast that's just one of those things that you know modern IDs will allow us to do that now that is our inmemory model what we also need is we're going to need a data access object and what this is is this is going to say in the uh with our model we want to be able to integrate it or work with our database so we want to do stuff like we want to be able to save or update a record we want to be able to get a record based on an ID we want to be able to delete a record and we want to be able to list all the records or you know based on some query we want to be able to list a bunch of Records this is key because one of the things we did in our sample uh here is we do a list which is what we're going to be building here in a second and we also do a get so get by ID and a list uh there are a couple different ways you could name these things uh we do a straight up get and assume it's going to be the IDE there could be things like you might do search you might do find you might do get by name get by title things like that uh but what we do in our Dao is all this is is this is going to be an interface because we're going to end up implementing it somewhere else so what we do here is we put each one of our methods that we're going to call uh we do the signature here so um like here I don't know why that says over that's an interesting name for that I don't but um uh let's just call this uh opportunity I'm going to clean this up a little bit because I don't know why I called it that and then so now I've got to go find this and what I'm going to do is once I've built this interface I'm going to have to turn around and build an implementer because I'm going to make use of it so there's not anything really special we have to do with the Dao I'm just taking the model that I created that's going to be because I am using that to some extent uh like here I'm returning a list of opportunities here I'm returning an opportunity here I'm passing one in so you're going to usually see that model used quite a bit in the uh interface so you want to include that in and then the meat of it comes with the implementation side hello this is Rob with develop andur also known as building better developers wanted to announce that we have school. developer.com feel free to check it out if you like any of this information any of the content that we've sent and you would like to see more you can come out you can enroll for free we have free courses we've got places for you to get better at just learning a technology or how toos you can work on your business skills we can help you with becoming a better developer as in coding and things like that a lot of the stuff you've seen on YouTube we also have out at school. develop andure we just have it in a little more of a educational format and a way for you to track your progress as you move forward becoming a better [Music] developer
Transcript Segments
[Music]
well hello and welcome back we are
continuing essentially this little
project is rep platforming in a sense I
guess an older Java application so we're
going to take an old web app that we ran
through Tomcat uh patchy Tom Cat and
we're going to upgrade it to use spring
Boot and so that's what we've been
exploring now the last time around we we
showed how it's very easy to set up both
a uh like a rest type of approach or a
web type approach we can put we can have
our API we can have our web pages all of
that running fairly simply through our
our various
controllers this episode we're going to
focus and actually it's this episode
will probably be a multiarterial into
this uh but we're going to actually
connect this thing up to some
data now I have an existing database and
you can see some examples of that uh but
what we're going to do is our examples
of just creating a database we're going
to use Maria DB and we're going to set a
few things up here to get ourselves uh
configured properly so that we can
actually talk to our data base what
we're going to end up doing the goal is
we're going to have our little uh we're
going to add a new mapping called test
uh for right now we're going to we'll
give it a parameter just because um so
we could give it a parameter of let's
say table which is going to be our table
name and then uh it's not going to be
required because we're not going to use
it right now we will get into that a
little
later and I've got this thing called
there's a table I've got that I'm
calling opportunity actually the table I
think it's
opportunities um but the opportunities
tables got some data in it so what we're
going to do is we're going to list it
for a specific user list all the records
for a specific user and we're going to
go just get a bu ID get a specific Row
in the database and then print those out
what that looks like is uh pretty
straightforward so if I come in and hit
it hit that page which is just here it's
just if you can see up here that was
called
test and if I hit that page then we're
going to see here when I do my uh get by
ID I'm going to print that item and that
is here which is this big opportunity
thing right
here and we'll show how to do that and
then I'm going through and I'm just
listing the title of each opportunities
which is each of the opportunities which
is down here
now connecting to database you can use
uh hibernate you can use jpa there's
some things you can do that can make
this very simple you don't have to get
generally speaking you can you can have
some crud stuff thrown up crud being
create remove update or I'm sorry
retrieve update and delete so you can
have that basic functionality with the
table you can have that put together in
10 or 15 minutes we may cover that in
another one um in our case we're going
to do a lot of custom stuff so it's just
easier and because that's how it was
done before we're going to use what's
called jdbc template
now with that uh we're going to have our
jdbc connector one of the things that we
do need to do is we're going to go to
our project if we haven't already and
we're go to starters and we're going to
make sure if we do jdbc we're going to
make sure that we have the jdbc API and
we're also going to need it for in this
case our Maria DB driver so if I add
those
two it's not going to do anything in my
case because nothing has changed so I'm
just going to cancel but you go ahead
and add those in and that's going to
make sure that you get a couple of key
pieces one of the things that we're
going to
have is we're going to have some
application
properties and we're going to need to
make sure that we set up these so that
they connect to our database now those
are pretty
straightforward so what we're going to
do is give it our username give it a
password
and these use spring. datasource and
that's. URL to give us the in this case
the jdbc link uh this is actually in our
case instead of my SQL it's going to be
Maria
DB and then we're going to give it a
path I can keep it pretty
simple so I'm just going to go
here the default port number here's my
database name got to give it the driver
so spring data source driver class name
and then we've got our and then there's
this thing uh I don't really need right
now that was actually a leftover for
mother that's if you're going to use jpa
hibernate you would need that in this
case it's going to ignore it because we
don't need it
now that is to effectively connect to
your database now to actually do all
this stuff to connect to that we have a
couple of classes we're going to make
and this is where the power of using
objectoriented and making sure that you
properly you extract things and or
abstract things and put things where
they need to be can help because what
I'm going to end up doing and what I've
done here is I've actually basically
just copied about six different files
out of the older version because they
all connected all I have to do is change
up a couple of drivers or a couple of
imports here and there and then boom I'm
off and
running so let's start with the data
itself so in this case I've got this
thing called an opportunity this is a
table we're going to do and so what I'm
going to do or the table we're going to
work with and what I'm going to do is I
am going to have a object a class that
will be objects that is going to mimic
or basically be my store for that data
within the application so I'm not going
to necessarily always be directly
dealing with the database what I want to
do is be able to load data from the
database essentially into memory in
object that I'm going to work with in my
application and then with that I'm going
to be able to you know wheel and deal
with them and it's basically now an MVC
so this is my model this is where the
data lives the controller is the back
end that's going to basically save stuff
or retrieve stuff and then I've also got
a view that's going to say hey this is
how I'm you know how I'm going to be
able to view it and we'll talk about the
view at at another point because that's
really that's the uh the the hello
controller right now our view is nothing
because we just spin something out and
we have something in our control panel
down here or in our console so what
we're going to do uh to make it simple
and that's where a lot of these are each
of these fields that we're doing is a
column in the database so if I flip over
to my database if I I can look at my
table opportunities I know it's a bit
small but it's just basically so you can
see that hey I have got this
opportunities uh table here's a columns
I have I've got an ID a title summary
blah blah blah got several different
columns and so I'm going to take that
and I'm going to model that into a you
know each row into a record in my
database via this model class in this
case it's a very simple one it's just
I've got an ID as my uh is an integer
the rest of stuff is just straight up
strings even though I've got like a date
and some stuff like that I'm not putting
any special types so the easy way to do
this is you generate those
properties those are you're going to
have your in each case you're going to
have a getter and a Setter and then at
the end here I just override two
string I can honestly you can generate
these super fast if you don't already
know if you are in eclipse or something
like it and I I know that other ID IDs
have it this is just the specific way
Eclipse does it but if you come in here
you could say generate Getters and
Setters uh and so it's this one's
already generated all of them if I take
it like this
though
let me just do that and so if I do this
so if I just enter my properties and I
say generate Getters and Setters is
going to say okay which Fields do you
want and you can't I don't how well you
can see this because it is small text
but it is basically just
listing all of my Fields here so I can
pick one or two or all of them and then
it'll tell me where I can place it and
it will go generate so if I do select
all and generate boom I it just
generated Getters and sitters Setters
for each of them so like example you can
see like here's where it says get amount
and somewhere else here's where it has
set amount so I can get that generated
real quick I can also do uh from that I
can also do a two string that's another
one that I can generate very quickly so
if I do it already has one but uh what
it does is it just puts the class name
if you can see the class name up here
and then just walks through each of the
Val values and prints them
out so it's pretty darn easy to generate
your model real fast that's just one of
those things that you know modern IDs
will allow us to do that now that is our
inmemory model what we also
need is we're going to need a data
access object and what this is is this
is going to say in
the uh with our model we want to be able
to integrate it or work with our
database so we want to do stuff like we
want to be able to save or update a
record we want to be able to get a
record based on an ID we want to be able
to delete a record and we want to be
able to list all the records or you know
based on some query we want to be able
to list a bunch of Records this is key
because one of the things we did in our
sample uh here is we do a list which is
what we're going to be building here in
a second and we also do a get so get by
ID and a list
uh there are a couple different ways you
could name these
things uh we do a straight up get and
assume it's going to be the IDE there
could be things like you might do search
you might do find you might do get by
name get by title things like that uh
but what we do in our Dao is all this is
is this is going to be an interface
because we're going to end up
implementing it somewhere else so what
we do here is we
put each one of our methods that we're
going to call uh we do the signature
here so um like here I don't know why
that says over that's an interesting
name for that I don't but
um uh let's just call
this uh opportunity I'm going to clean
this up a little bit because I don't
know why I called it
that and then so now I've got to go find
this and what I'm going to do is once
I've built this
interface I'm going to have to turn
around and build an implementer because
I'm going to make use of it so there's
not anything really special we have to
do with the Dao I'm just taking the
model that I
created that's going to be because I am
using that to some extent uh like here
I'm returning a list of opportunities
here I'm returning an opportunity here
I'm passing one in so you're going to
usually see that model used quite a bit
in the uh interface so you want to
include that in and then the meat of it
comes with the implementation
side hello this is Rob with develop
andur also known as building better
developers wanted to announce that we
have school. developer.com feel free to
check it out if you like any of this
information any of the content that
we've sent and you would like to see
more you can come out you can enroll for
free we have free courses we've got
places for you to get better at just
learning a technology or how toos you
can work on your business skills we can
help you with becoming a better
developer as in coding and things like
that a lot of the stuff you've seen on
YouTube we also have out at school.
develop andure we just have it in a
little more of a educational format and
a way for you to track your progress as
you move forward becoming a better
[Music]
developer