FULL COURSE: Spring MVC and Hibernate (200+ videos)
This Hibernate tutorial series will help you quickly get up to speed with Hibernate.
DOWNLOAD SOURCE CODE for Hibernate tutorial.
View more videos on the playlist, Hibernate Tutorial: https://goo.gl/UKAdaq
Follow luv2code for more Hibernate tutorials:
If you liked my Hibernate tutorial, then join my mailing list: Get exclusive access to new Java tutorials.
Questions or problems about this Hibernate tutorial? Post them in the comments section below.
Want to suggest a video for my Hibernate tutorial? Leave a comment below. I'm always looking for new video ideas.
Let me know what video you'd like for me to create for this Hibernate tutorial.
Hibernate Tutorial Transcript
In this video, we're going to test our JDBC connection.
In the previous video, we did a lot of work with downloading JAR files. What I want to do hear is just do a very simple test to make sure that our JDBC connection works just fine. What we're going to do is create a very simple JDBC program, just to test everything out, just to make sure everything works out okay.
What I would like to do, is move into this hibernate tutorial under the source. I'll create a new package. Right click, I'll say New Package. The actual name of the package, I'll call it com.luv2code.jdbc. Once you're happy with that package name, go ahead and click on finish.
Now what I would like to do is actually create a new class. Again, right click on that package name. I'll say New Class. The actual name that I'll use for this class, I'll call it Testjdbc. Just very simple, and to the point. I'll also check the low item here for public static void, just so I can have a main method for this program. Then again, hit on Finish.
All right, great. We have a very basic program here and let's go ahead and dig in. Let's start writing some code.
I have a very simple try catch block. I have catch exception EXC. I'll do the EXC.print stack trace in case something goes wrong. Hopefully not. What I'll do is I'll just set up some variables here for my database connection information.
First off, I'll set up my JDBC URL. I'll call it JDBC:mysequel:localhost3306/ I give the name of my database scheme, which is hbstudenttracker. Here I'll say use SSL=false. Just so that I don't get that pesky SSL warning that my sequel introduced recently, which is really annoying. Anyway, use SSL will make that go away.
All right, so that's how JDBC URL, our user ID that we created, and we did all this stuff in previous slides here. Our user is HBStudent. We have this very secure system where our password is the same as our username. Only for training purposes, right? Okay, good. All right, so our URL, user ID, and password, great. We can use this information to actually give us a connection to a database.
All right. Here, I'm just mentioning again that use SSL as equals false as to get rid of that pesky my sequel SSL warnings.
All right, great. Let's go ahead and write some code here. I'm going to make use of my driver manager to get a connection. I'll also do some system out print lines here to tell myself what's going on here. All right, so I'll say system out print line, connecting to database. I'll just drop in that JDBC URL. Then I'll attempt to get a connection. Here I'll say connectionmycon=, and then I say driver manager.getconnection. I pass in my JDBC URL, my user ID, and my password. If that works out fine, then we have a successful connection. I can print a statement here saying connection successful.
I like to have fun with this stuff.
All right, so that's the piece there. Now, we need to fix some of our imports here. I can just click on this red X. We need to do that import on java.sql, so choose the second option here. Import Connection, and java.sql. Select that item. Yeah, that takes care of the item then. We should be good to go here.
All right, so just some very basic code here to test out our database connection. Let's go ahead and run this application. I just do a right click, Run As, Java Application. Then it's going to fire up, and say connection successful. Let me expand this window here, so you can see everything. Connecting to database, connection successful. This is great. This is just a confirmation that we have the correct URL, user ID, and password. Also, more importantly, is that JDBC driver that we have on our lib directory, is associated with our class path. Know that we can get a valid connection.
[Snip] for complete transcript, select More ... Transcript
This is the end of the Hibernate tutorial transcript.