📺 Develpreneur YouTube Episode

Video + transcript

Spring Boot JDBC Template Part 1

2023-11-22 •Youtube

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
0

[Music]

16.92

well hello and welcome back we are

20.32

continuing essentially this little

22.24

project is rep platforming in a sense I

25.76

guess an older Java application so we're

28.4

going to take an old web app that we ran

30.359

through Tomcat uh patchy Tom Cat and

34.28

we're going to upgrade it to use spring

36.64

Boot and so that's what we've been

38.52

exploring now the last time around we we

40.6

showed how it's very easy to set up both

43.079

a uh like a rest type of approach or a

45.96

web type approach we can put we can have

47.719

our API we can have our web pages all of

49.96

that running fairly simply through our

52.48

our various

53.76

controllers this episode we're going to

55.719

focus and actually it's this episode

57.6

will probably be a multiarterial into

60.12

this uh but we're going to actually

61.8

connect this thing up to some

63.96

data now I have an existing database and

67.56

you can see some examples of that uh but

69.68

what we're going to do is our examples

72.6

of just creating a database we're going

74.439

to use Maria DB and we're going to set a

76.92

few things up here to get ourselves uh

80.56

configured properly so that we can

82.36

actually talk to our data base what

84.759

we're going to end up doing the goal is

87.2

we're going to have our little uh we're

88.92

going to add a new mapping called test

91.36

uh for right now we're going to we'll

92.88

give it a parameter just because um so

96.28

we could give it a parameter of let's

98.439

say table which is going to be our table

99.92

name and then uh it's not going to be

102.64

required because we're not going to use

104.119

it right now we will get into that a

105.56

little

106.399

later and I've got this thing called

109.36

there's a table I've got that I'm

111.04

calling opportunity actually the table I

113.479

think it's

114.719

opportunities um but the opportunities

118.439

tables got some data in it so what we're

120.119

going to do is we're going to list it

121.68

for a specific user list all the records

123.68

for a specific user and we're going to

126.159

go just get a bu ID get a specific Row

129.679

in the database and then print those out

133

what that looks like is uh pretty

136

straightforward so if I come in and hit

138.239

it hit that page which is just here it's

142.4

just if you can see up here that was

143.8

called

144.84

test and if I hit that page then we're

148.04

going to see here when I do my uh get by

152.92

ID I'm going to print that item and that

156.36

is here which is this big opportunity

159.319

thing right

161.12

here and we'll show how to do that and

163.8

then I'm going through and I'm just

165.04

listing the title of each opportunities

167.36

which is each of the opportunities which

168.76

is down here

170.48

now connecting to database you can use

175.12

uh hibernate you can use jpa there's

176.92

some things you can do that can make

178.2

this very simple you don't have to get

182.44

generally speaking you can you can have

184.239

some crud stuff thrown up crud being

186.599

create remove update or I'm sorry

189

retrieve update and delete so you can

192.12

have that basic functionality with the

194.04

table you can have that put together in

195.84

10 or 15 minutes we may cover that in

198.2

another one um in our case we're going

202.12

to do a lot of custom stuff so it's just

203.92

easier and because that's how it was

205.799

done before we're going to use what's

207.64

called jdbc template

210.72

now with that uh we're going to have our

213.439

jdbc connector one of the things that we

215.28

do need to do is we're going to go to

217.4

our project if we haven't already and

219.72

we're go to starters and we're going to

221.72

make sure if we do jdbc we're going to

224.72

make sure that we have the jdbc API and

227.92

we're also going to need it for in this

229.799

case our Maria DB driver so if I add

232.12

those

235.2

two it's not going to do anything in my

237.4

case because nothing has changed so I'm

239.28

just going to cancel but you go ahead

242

and add those in and that's going to

244.439

make sure that you get a couple of key

245.84

pieces one of the things that we're

247.64

going to

248.879

have is we're going to have some

252.04

application

254.2

properties and we're going to need to

256.04

make sure that we set up these so that

258.44

they connect to our database now those

260.199

are pretty

261.919

straightforward so what we're going to

265.16

do is give it our username give it a

268.68

password

270.28

and these use spring. datasource and

272.32

that's. URL to give us the in this case

275.88

the jdbc link uh this is actually in our

278.56

case instead of my SQL it's going to be

280.039

Maria

282.28

DB and then we're going to give it a

284.84

path I can keep it pretty

287.4

simple so I'm just going to go

290.199

here the default port number here's my

293.72

database name got to give it the driver

296.36

so spring data source driver class name

300.68

and then we've got our and then there's

303

this thing uh I don't really need right

304.919

now that was actually a leftover for

306.199

mother that's if you're going to use jpa

309.6

hibernate you would need that in this

311.68

case it's going to ignore it because we

313.36

don't need it

316.52

now that is to effectively connect to

319.44

your database now to actually do all

321.319

this stuff to connect to that we have a

323.919

couple of classes we're going to make

325.8

and this is where the power of using

329.6

objectoriented and making sure that you

332

properly you extract things and or

335.68

abstract things and put things where

337.199

they need to be can help because what

339.72

I'm going to end up doing and what I've

341.08

done here is I've actually basically

342.96

just copied about six different files

345.4

out of the older version because they

347.44

all connected all I have to do is change

349.8

up a couple of drivers or a couple of

351.52

imports here and there and then boom I'm

353.68

off and

354.72

running so let's start with the data

357.72

itself so in this case I've got this

359.96

thing called an opportunity this is a

362.44

table we're going to do and so what I'm

363.6

going to do or the table we're going to

365.199

work with and what I'm going to do is I

368.639

am going to have a object a class that

373.319

will be objects that is going to mimic

376.36

or basically be my store for that data

378.72

within the application so I'm not going

380.88

to necessarily always be directly

383.08

dealing with the database what I want to

384.599

do is be able to load data from the

386.88

database essentially into memory in

389

object that I'm going to work with in my

391.4

application and then with that I'm going

394.52

to be able to you know wheel and deal

396.36

with them and it's basically now an MVC

398.84

so this is my model this is where the

401.12

data lives the controller is the back

405.96

end that's going to basically save stuff

407.56

or retrieve stuff and then I've also got

409.36

a view that's going to say hey this is

411.44

how I'm you know how I'm going to be

412.8

able to view it and we'll talk about the

415.12

view at at another point because that's

417.44

really that's the uh the the hello

419.759

controller right now our view is nothing

422.479

because we just spin something out and

424.879

we have something in our control panel

426.759

down here or in our console so what

429.52

we're going to do uh to make it simple

432.16

and that's where a lot of these are each

434.319

of these fields that we're doing is a

436.039

column in the database so if I flip over

438.72

to my database if I I can look at my

441.28

table opportunities I know it's a bit

443.08

small but it's just basically so you can

444.479

see that hey I have got this

446.919

opportunities uh table here's a columns

449.44

I have I've got an ID a title summary

451.56

blah blah blah got several different

454.16

columns and so I'm going to take that

455.8

and I'm going to model that into a you

458.28

know each row into a record in my

459.879

database via this model class in this

463.12

case it's a very simple one it's just

464.919

I've got an ID as my uh is an integer

468.319

the rest of stuff is just straight up

469.759

strings even though I've got like a date

471.72

and some stuff like that I'm not putting

473.36

any special types so the easy way to do

476.36

this is you generate those

478.319

properties those are you're going to

480.199

have your in each case you're going to

481.479

have a getter and a Setter and then at

484.479

the end here I just override two

486.639

string I can honestly you can generate

489

these super fast if you don't already

491.039

know if you are in eclipse or something

494.159

like it and I I know that other ID IDs

497.36

have it this is just the specific way

499.08

Eclipse does it but if you come in here

501.159

you could say generate Getters and

503

Setters uh and so it's this one's

505.28

already generated all of them if I take

506.84

it like this

508.24

though

510.12

let me just do that and so if I do this

514

so if I just enter my properties and I

516.279

say generate Getters and Setters is

517.919

going to say okay which Fields do you

519.44

want and you can't I don't how well you

521.12

can see this because it is small text

523.2

but it is basically just

525.24

listing all of my Fields here so I can

529.16

pick one or two or all of them and then

532.44

it'll tell me where I can place it and

533.88

it will go generate so if I do select

535.64

all and generate boom I it just

538.76

generated Getters and sitters Setters

541.279

for each of them so like example you can

543.64

see like here's where it says get amount

545.8

and somewhere else here's where it has

547.6

set amount so I can get that generated

550.56

real quick I can also do uh from that I

554.279

can also do a two string that's another

556.2

one that I can generate very quickly so

559.24

if I do it already has one but uh what

562.399

it does is it just puts the class name

565.48

if you can see the class name up here

567.72

and then just walks through each of the

568.92

Val values and prints them

571.04

out so it's pretty darn easy to generate

575.399

your model real fast that's just one of

577.2

those things that you know modern IDs

579.839

will allow us to do that now that is our

583.68

inmemory model what we also

587.519

need is we're going to need a data

589.76

access object and what this is is this

591.56

is going to say in

594.079

the uh with our model we want to be able

596.36

to integrate it or work with our

597.959

database so we want to do stuff like we

599.839

want to be able to save or update a

601.44

record we want to be able to get a

603.24

record based on an ID we want to be able

605.279

to delete a record and we want to be

607.72

able to list all the records or you know

610.279

based on some query we want to be able

612.04

to list a bunch of Records this is key

614.6

because one of the things we did in our

616.839

sample uh here is we do a list which is

622.2

what we're going to be building here in

623.36

a second and we also do a get so get by

627.04

ID and a list

629.64

uh there are a couple different ways you

631.72

could name these

633.48

things uh we do a straight up get and

635.959

assume it's going to be the IDE there

637.48

could be things like you might do search

639.2

you might do find you might do get by

641.04

name get by title things like that uh

644.24

but what we do in our Dao is all this is

646.8

is this is going to be an interface

648.48

because we're going to end up

649.2

implementing it somewhere else so what

651.2

we do here is we

652.959

put each one of our methods that we're

656.2

going to call uh we do the signature

658.72

here so um like here I don't know why

662.48

that says over that's an interesting

664.079

name for that I don't but

669

um uh let's just call

672.88

this uh opportunity I'm going to clean

675.76

this up a little bit because I don't

676.8

know why I called it

678.24

that and then so now I've got to go find

680.959

this and what I'm going to do is once

682.6

I've built this

683.8

interface I'm going to have to turn

685.639

around and build an implementer because

688.079

I'm going to make use of it so there's

690.92

not anything really special we have to

692.279

do with the Dao I'm just taking the

694.68

model that I

696.24

created that's going to be because I am

698.639

using that to some extent uh like here

701.88

I'm returning a list of opportunities

703.6

here I'm returning an opportunity here

705.839

I'm passing one in so you're going to

707.32

usually see that model used quite a bit

710.04

in the uh interface so you want to

712.24

include that in and then the meat of it

715.2

comes with the implementation

717.959

side hello this is Rob with develop

720.519

andur also known as building better

722.56

developers wanted to announce that we

724.6

have school. developer.com feel free to

727.68

check it out if you like any of this

729.88

information any of the content that

731.8

we've sent and you would like to see

733.12

more you can come out you can enroll for

735.12

free we have free courses we've got

737.519

places for you to get better at just

740.04

learning a technology or how toos you

742.6

can work on your business skills we can

744.92

help you with becoming a better

746.32

developer as in coding and things like

748.639

that a lot of the stuff you've seen on

750.279

YouTube we also have out at school.

752.959

develop andure we just have it in a

754.56

little more of a educational format and

757.16

a way for you to track your progress as

759.639

you move forward becoming a better

764.47

[Music]

777.839

developer