1 00:00:00,600 --> 00:00:03,110 Now that you've seen how to connect to and interact with 2 00:00:03,110 --> 00:00:07,950 a database in Java using JDBC, we're ready to take it to the next level. 3 00:00:07,950 --> 00:00:10,810 If we're writing a contact manager application, 4 00:00:10,810 --> 00:00:15,070 one of the obvious objects we'd want to work with would be a contact object. 5 00:00:16,090 --> 00:00:17,230 We can write a POJO. 6 00:00:17,230 --> 00:00:19,477 Remember, that's plain old Java object. 7 00:00:19,477 --> 00:00:23,938 That models a contact by creating a class that includes an instance field for 8 00:00:23,938 --> 00:00:27,050 each of the database columns we define. 9 00:00:27,050 --> 00:00:29,600 Then, when we have a new contact object, 10 00:00:29,600 --> 00:00:32,960 we should be able to simply save it to the database. 11 00:00:32,960 --> 00:00:35,430 Let's write a first draft of what that might look like. 12 00:00:36,920 --> 00:00:40,180 What I'd like to do here is write a save method that allows us to quickly save 13 00:00:40,180 --> 00:00:45,130 a contact to the database without having to continually rewrite the SQL to do so. 14 00:00:45,130 --> 00:00:48,205 Let's get this started at the bottom. 15 00:00:48,205 --> 00:00:51,971 So I'll start by entering a method stub here, public static void. 16 00:00:51,971 --> 00:00:52,905 I'll call it save. 17 00:00:52,905 --> 00:00:57,535 And I'd like to save a cCntact object so I'll include that as a parameter. 18 00:00:57,535 --> 00:00:59,195 And let me drop a couple comments in here. 19 00:00:59,195 --> 00:01:01,072 The things that we'd like to do. 20 00:01:01,072 --> 00:01:06,565 So we'll start by composing the query, and then we will execute the query. 21 00:01:08,340 --> 00:01:13,350 Let's begin composing the query by using a String variable, I'll name it sql. 22 00:01:13,350 --> 00:01:17,240 And this variable will hold the general syntax for an insert statement. 23 00:01:17,240 --> 00:01:19,950 We wanna insert it into the contacts table. 24 00:01:19,950 --> 00:01:21,600 We'll list our column names, and 25 00:01:21,600 --> 00:01:24,680 then we'll list the actual values that we'd like to insert. 26 00:01:24,680 --> 00:01:31,568 So my column names here are firstname, lastname, email, and phone. 27 00:01:31,568 --> 00:01:36,291 And what I wanna do is stick placeholders in here where I can put the actual 28 00:01:36,291 --> 00:01:41,370 values from my contact object for the values of the insert statement. 29 00:01:41,370 --> 00:01:45,435 But, so that I don't forget the single quotes that surround a string value in 30 00:01:45,435 --> 00:01:48,960 SQL, I'm gonna stick those in right now. 31 00:01:48,960 --> 00:01:52,610 The first three columns are string columns. 32 00:01:52,610 --> 00:01:55,479 So for our first name we'll have a string placeholder. 33 00:01:55,479 --> 00:01:59,614 Same for last name and email. 34 00:01:59,614 --> 00:02:02,940 And for the phone we'll have a numeric placeholder. 35 00:02:02,940 --> 00:02:07,000 Now, let's take the actual values of the Contact object's fields 36 00:02:07,000 --> 00:02:09,980 into this string using String.format. 37 00:02:09,980 --> 00:02:14,370 And I will place or assign that value right back to the SQL variable. 38 00:02:14,370 --> 00:02:16,680 So I'll use string String.format. 39 00:02:16,680 --> 00:02:19,800 The sql variable holds my general format, and 40 00:02:19,800 --> 00:02:25,322 then into the placeholders I will place the values contact.getFirstName(), 41 00:02:28,238 --> 00:02:36,613 contact.getLastName(), contact.getEmail() and 42 00:02:36,613 --> 00:02:39,798 contact.getPhone. 43 00:02:39,798 --> 00:02:43,464 If you wanna verify that those getters actually exist are are in 44 00:02:43,464 --> 00:02:46,231 alignment with the Contact class, go ahead and 45 00:02:46,231 --> 00:02:50,730 click on Contact.java to verify that that is indeed the case. 46 00:02:50,730 --> 00:02:54,325 In order to execute this update using statement.executeUpdate, 47 00:02:54,325 --> 00:02:56,760 we'll need that statement object. 48 00:02:56,760 --> 00:03:00,590 Let's include it as a parameter after the Contact object. 49 00:03:00,590 --> 00:03:03,200 And I will just call this Statement. 50 00:03:03,200 --> 00:03:09,735 So under execute the query, I can now call statement.executeUpdate. 51 00:03:09,735 --> 00:03:12,620 And pass it the value of the sql variable, 52 00:03:12,620 --> 00:03:16,510 which will hold our fully formatted insert statement, 53 00:03:16,510 --> 00:03:22,140 including the values that have been placed into these placeholders. 54 00:03:22,140 --> 00:03:24,931 Just one more issue with this method. 55 00:03:24,931 --> 00:03:29,605 The statement.executeUpdate method is declared as throwing a SQL Exception. 56 00:03:29,605 --> 00:03:32,653 So, either we'll need a try catch block right here, 57 00:03:32,653 --> 00:03:34,635 which catches the SQL exception. 58 00:03:34,635 --> 00:03:39,153 Or, we'll have to declare this save method as throwing the SQL exception, 59 00:03:39,153 --> 00:03:43,214 which means that the caller of this method will have to handle this. 60 00:03:43,214 --> 00:03:45,506 We'll choose this ladder approach, 61 00:03:45,506 --> 00:03:49,380 since the caller of this method will be the main method up above. 62 00:03:49,380 --> 00:03:53,982 And up here in the main method we already have a try catch that handles 63 00:03:53,982 --> 00:03:55,760 the SQL exception. 64 00:03:55,760 --> 00:03:58,790 So let's add that throws clause right here. 65 00:03:58,790 --> 00:04:01,290 Throws, SQLException. 66 00:04:02,960 --> 00:04:08,190 Speaking of that main method, let's return to that main method and replace these two 67 00:04:08,190 --> 00:04:13,740 insert statements with calls to our newly coded save method. 68 00:04:13,740 --> 00:04:17,000 So the first I'll need to do is create a Contact object 69 00:04:17,000 --> 00:04:21,460 with all the same information that's included in this first insert statement. 70 00:04:21,460 --> 00:04:25,912 So the order of this, and again, 71 00:04:25,912 --> 00:04:32,908 you can verify this in contact.java is first name, 72 00:04:32,908 --> 00:04:37,205 last name, email, and phone. 73 00:04:37,205 --> 00:04:39,160 I'm gonna just copy and paste this. 74 00:04:39,160 --> 00:04:43,560 But this is a long value, so I won't forget to append the L there so 75 00:04:43,560 --> 00:04:45,960 the JVM knows to consider it a long. 76 00:04:47,710 --> 00:04:51,250 Now, I'm ready to call our newly coded save method. 77 00:04:51,250 --> 00:04:56,590 So I'll save this contact using the statement object that I constructed above. 78 00:04:57,930 --> 00:05:01,110 And we'll repeat all that for James Gosling as well. 79 00:05:03,530 --> 00:05:07,450 We'll add him right here, James Gosling. 80 00:05:10,538 --> 00:05:13,542 James@java.com. 81 00:05:13,542 --> 00:05:18,060 And I'll use this phone number, without forgetting the L. 82 00:05:19,880 --> 00:05:24,580 Now I should be able to delete these two calls to the executeUpdate method. 83 00:05:25,740 --> 00:05:30,000 Let's compile and run our code to see if we get the same results. 84 00:05:30,000 --> 00:05:34,493 javac JdbcMain.java. 85 00:05:34,493 --> 00:05:36,360 Looks like all compiles well. 86 00:05:36,360 --> 00:05:41,750 So, let's run it specifying the class path that includes both the jar file and 87 00:05:41,750 --> 00:05:42,900 the current directory. 88 00:05:46,560 --> 00:05:47,350 Awesome! 89 00:05:47,350 --> 00:05:49,270 Same result as before. 90 00:05:49,270 --> 00:05:53,180 We've now abstracted away the actual SQL for insert statements. 91 00:05:53,180 --> 00:05:57,760 So up above, instead of crafting the actual SQL statements, 92 00:05:57,760 --> 00:06:02,470 we can simply call our save method and it takes care of the details for us. 93 00:06:03,840 --> 00:06:05,070 Hey, great job! 94 00:06:05,070 --> 00:06:09,100 You've now used Java to connect to and interact with a database. 95 00:06:09,100 --> 00:06:14,060 In addition, you've taken your first step toward allowing the saving of POJOs. 96 00:06:14,060 --> 00:06:18,510 It's nice to be able to call a save method with your POJO instead of having to craft 97 00:06:18,510 --> 00:06:22,190 and re-craft SQL statements with every database change. 98 00:06:22,190 --> 00:06:25,590 You're well on your way to discovering the next level of data management 99 00:06:25,590 --> 00:06:26,750 using application code.