Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Hibernate Basics!
      
    
You have completed Hibernate Basics!
Preview
    
      
  As a step up from the last exercise of saving individual parts of a contact to a database, you will now explore how we might store a contact object into the database.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      Now that you've seen how to connect to and
interact with
                      0:00
                    
                    
                      a database in Java using JDBC,
we're ready to take it to the next level.
                      0:03
                    
                    
                      If we're writing a contact
manager application,
                      0:07
                    
                    
                      one of the obvious objects we'd want
to work with would be a contact object.
                      0:10
                    
                    
                      We can write a POJO.
                      0:16
                    
                    
                      Remember, that's plain old Java object.
                      0:17
                    
                    
                      That models a contact by creating a class
that includes an instance field for
                      0:19
                    
                    
                      each of the database columns we define.
                      0:23
                    
                    
                      Then, when we have a new contact object,
                      0:27
                    
                    
                      we should be able to simply
save it to the database.
                      0:29
                    
                    
                      Let's write a first draft of
what that might look like.
                      0:32
                    
                    
                      What I'd like to do here is write a save
method that allows us to quickly save
                      0:36
                    
                    
                      a contact to the database without having
to continually rewrite the SQL to do so.
                      0:40
                    
                    
                      Let's get this started at the bottom.
                      0:45
                    
                    
                      So I'll start by entering a method
stub here, public static void.
                      0:48
                    
                    
                      I'll call it save.
                      0:51
                    
                    
                      And I'd like to save a cCntact object so
I'll include that as a parameter.
                      0:52
                    
                    
                      And let me drop a couple comments in here.
                      0:57
                    
                    
                      The things that we'd like to do.
                      0:59
                    
                    
                      So we'll start by composing the query,
and then we will execute the query.
                      1:01
                    
                    
                      Let's begin composing the query by using
a String variable, I'll name it sql.
                      1:08
                    
                    
                      And this variable will hold the general
syntax for an insert statement.
                      1:13
                    
                    
                      We wanna insert it into
the contacts table.
                      1:17
                    
                    
                      We'll list our column names, and
                      1:19
                    
                    
                      then we'll list the actual
values that we'd like to insert.
                      1:21
                    
                    
                      So my column names here are firstname,
lastname, email, and phone.
                      1:24
                    
                    
                      And what I wanna do is stick placeholders
in here where I can put the actual
                      1:31
                    
                    
                      values from my contact object for
the values of the insert statement.
                      1:36
                    
                    
                      But, so that I don't forget the single
quotes that surround a string value in
                      1:41
                    
                    
                      SQL, I'm gonna stick those in right now.
                      1:45
                    
                    
                      The first three columns
are string columns.
                      1:48
                    
                    
                      So for our first name we'll
have a string placeholder.
                      1:52
                    
                    
                      Same for last name and email.
                      1:55
                    
                    
                      And for the phone we'll
have a numeric placeholder.
                      1:59
                    
                    
                      Now, let's take the actual values
of the Contact object's fields
                      2:02
                    
                    
                      into this string using String.format.
                      2:07
                    
                    
                      And I will place or assign that value
right back to the SQL variable.
                      2:09
                    
                    
                      So I'll use string String.format.
                      2:14
                    
                    
                      The sql variable holds my general format,
and
                      2:16
                    
                    
                      then into the placeholders I will place
the values contact.getFirstName(),
                      2:19
                    
                    
                      contact.getLastName(), contact.getEmail()
and
                      2:28
                    
                    
                      contact.getPhone.
                      2:36
                    
                    
                      If you wanna verify that those
getters actually exist are are in
                      2:39
                    
                    
                      alignment with the Contact class,
go ahead and
                      2:43
                    
                    
                      click on Contact.java to verify
that that is indeed the case.
                      2:46
                    
                    
                      In order to execute this update
using statement.executeUpdate,
                      2:50
                    
                    
                      we'll need that statement object.
                      2:54
                    
                    
                      Let's include it as a parameter
after the Contact object.
                      2:56
                    
                    
                      And I will just call this Statement.
                      3:00
                    
                    
                      So under execute the query,
I can now call statement.executeUpdate.
                      3:03
                    
                    
                      And pass it the value of the sql variable,
                      3:09
                    
                    
                      which will hold our fully
formatted insert statement,
                      3:12
                    
                    
                      including the values that have been
placed into these placeholders.
                      3:16
                    
                    
                      Just one more issue with this method.
                      3:22
                    
                    
                      The statement.executeUpdate method is
declared as throwing a SQL Exception.
                      3:24
                    
                    
                      So, either we'll need a try
catch block right here,
                      3:29
                    
                    
                      which catches the SQL exception.
                      3:32
                    
                    
                      Or, we'll have to declare this save
method as throwing the SQL exception,
                      3:34
                    
                    
                      which means that the caller of this
method will have to handle this.
                      3:39
                    
                    
                      We'll choose this ladder approach,
                      3:43
                    
                    
                      since the caller of this method
will be the main method up above.
                      3:45
                    
                    
                      And up here in the main method we
already have a try catch that handles
                      3:49
                    
                    
                      the SQL exception.
                      3:53
                    
                    
                      So let's add that throws
clause right here.
                      3:55
                    
                    
                      Throws, SQLException.
                      3:58
                    
                    
                      Speaking of that main method, let's return
to that main method and replace these two
                      4:02
                    
                    
                      insert statements with calls to
our newly coded save method.
                      4:08
                    
                    
                      So the first I'll need to do
is create a Contact object
                      4:13
                    
                    
                      with all the same information that's
included in this first insert statement.
                      4:17
                    
                    
                      So the order of this, and again,
                      4:21
                    
                    
                      you can verify this in
contact.java is first name,
                      4:25
                    
                    
                      last name, email, and phone.
                      4:32
                    
                    
                      I'm gonna just copy and paste this.
                      4:37
                    
                    
                      But this is a long value, so
I won't forget to append the L there so
                      4:39
                    
                    
                      the JVM knows to consider it a long.
                      4:43
                    
                    
                      Now, I'm ready to call our
newly coded save method.
                      4:47
                    
                    
                      So I'll save this contact using the
statement object that I constructed above.
                      4:51
                    
                    
                      And we'll repeat all that for
James Gosling as well.
                      4:57
                    
                    
                      We'll add him right here, James Gosling.
                      5:03
                    
                    
                      James@java.com.
                      5:10
                    
                    
                      And I'll use this phone number,
without forgetting the L.
                      5:13
                    
                    
                      Now I should be able to delete these
two calls to the executeUpdate method.
                      5:19
                    
                    
                      Let's compile and run our code to
see if we get the same results.
                      5:25
                    
                    
                      javac JdbcMain.java.
                      5:30
                    
                    
                      Looks like all compiles well.
                      5:34
                    
                    
                      So, let's run it specifying the class
path that includes both the jar file and
                      5:36
                    
                    
                      the current directory.
                      5:41
                    
                    
                      Awesome!
                      5:46
                    
                    
                      Same result as before.
                      5:47
                    
                    
                      We've now abstracted away the actual
SQL for insert statements.
                      5:49
                    
                    
                      So up above, instead of crafting
the actual SQL statements,
                      5:53
                    
                    
                      we can simply call our save method and
it takes care of the details for us.
                      5:57
                    
                    
                      Hey, great job!
                      6:03
                    
                    
                      You've now used Java to connect to and
interact with a database.
                      6:05
                    
                    
                      In addition, you've taken your first step
toward allowing the saving of POJOs.
                      6:09
                    
                    
                      It's nice to be able to call a save method
with your POJO instead of having to craft
                      6:14
                    
                    
                      and re-craft SQL statements
with every database change.
                      6:18
                    
                    
                      You're well on your way to discovering
the next level of data management
                      6:22
                    
                    
                      using application code.
                      6:25
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up