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