HomeХобби и стильRelated VideosMore From: luv2code

Hibernate Tutorial #8 - Test the JDBC Connection

99 ratings | 15318 views
FULL COURSE: Spring MVC and Hibernate (200+ videos) - http://www.luv2code.com/full-spring ---- This Hibernate tutorial series will help you quickly get up to speed with Hibernate. ---- DOWNLOAD SOURCE CODE for Hibernate tutorial. http://www.luv2code.com/hibernate-tutorial/ ---- View more videos on the playlist, Hibernate Tutorial: https://goo.gl/UKAdaq ---- Follow luv2code for more Hibernate tutorials: Website: http://www.luv2code.com YouTube: http://goo.gl/EV6Kwv Twitter: http://goo.gl/ALMzLG Facebook: http://goo.gl/8pDRdA --- If you liked my Hibernate tutorial, then join my mailing list: Get exclusive access to new Java tutorials. - http://www.luv2code.com/joinlist --- 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. Yah-hoo. 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.
Html code for embedding videos on your blog
Text Comments (21)
Anderson Lopes (1 month ago)
Hey man! When I ran the code it returns me an error related to timezone. How can I fix that?
Shock Lolds (14 days ago)
just use this string: String jdbcUrl = "jdbc:mysql://localhost:3306/Turniej?useSSL=false&serverTimezone=UTC";
thanks
luv2code (14 days ago)
You are welcome Jhon. Thanks for the kind words :-)
Od Chan (3 months ago)
your teaching method is very good.
luv2code (14 days ago)
You are welcome. Thanks for the kind words :-)
Paweł Murawski (4 months ago)
Hi. In my case this code ("jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false") did not work. I found solution. I use this code ("jdbc:mysql://localhost:3306/hb_student_tracker?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC") which I found in internet, and its run. Do you have any clue, what was the reason?
priyanka gangwar (7 months ago)
Hello sir...plzz tell me from where i can get this sql scripts folder
luv2code (7 months ago)
You can download from here: http://www.luv2code.com/wp-content/uploads/2016/04/hibernate-course-source-code.zip The info section of the videos contain the links to source code.
Vinod Sagar (10 months ago)
Hello sir... Can you please help me in solving this error...as shown below. This is My code.... connecting to database:jbdc:mysql://localhost:3307/hb_student_tracker?useSSl=false java.sql.SQLException: No suitable driver found for jbdc:mysql://localhost:3307/hb_student_tracker?useSSl=false at java.sql/java.sql.DriverManager.getConnection(Unknown Source) at java.sql/java.sql.DriverManager.getConnection(Unknown Source) at com.vinod.jdbc.Testjdbc.main(Testjdbc.java:18) PLEASE HELP ME IN SOLVING THIS...
Vinod Sagar (10 months ago)
Thank you sir... I am sorry too.... Actually I am mechanical engineer, trying to learn coding part in java for better growth.... thanks for ur reply.....ur tutorials is helping me a lot.....:)
luv2code (10 months ago)
minor typo in your jdbc url replace jbdc:mysql://localhost:3307/hb_student_tracker?useSSl=false with jdbc:mysql://localhost:3307/hb_student_tracker?useSSl=false Make note of the first four characters ... should say "jdbc" --- let me know if that clears it for you. :-)
hemanth rs (10 months ago)
Insert Finally to complete try statement error?
luv2code (10 months ago)
hmmm ... not sure. take a look at the video information section. it has a link to the solution code. You can download and compare your code.
Ajax S (1 year ago)
sorry for continuous comments frm me... i got this error ... i even searched in google ..but didnt get the appropriate 1. java.sql.SQLException: No suitable driver found for jdbc.mysql//localhost:3306/hb_student_tracker?useSSL=false at java.sql.DriverManager.getConnection(DriverManager.java:689) at java.sql.DriverManager.getConnection(DriverManager.java:247) at coder.tester.main(tester.java:16)
luv2code (1 year ago)
A couple of things: 1. Make sure the MySQL JDBC driver is configured in your Eclipse project's build path 2. Make sure your Hibernate configuration use the correct driver name The solution code is available for download. source code and configuration files are included in the download. compare your code and configs to the solution to make sure you are not missing anything. --- let me know your results. :-)
Ajax S (1 year ago)
please help me out to know where do we find he jdbc url
Ajax S (1 year ago)
understood...
Sami Belkhiri (1 year ago)
so great, but what about this line of Code "Class.forName("com.mysql.jdbc.Driver");" I get a long exception with out it :)
Evgen UA (7 months ago)
even this didnt help me =(
luv2code (1 year ago)
this loads the jdbc driver into memory.

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.