Connect Java with Mysql Database | Java JDBC | Java Database connectivity | JDBC MySql | ArjunCodes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's up welcome back to the another video so in this video i'm going to show you how to connect a java application with mysql database or any database using jdbc so for that we need to follow the five steps the first step is to register the driver class the second step is to create the connection object the third step is to create the statement object and the fourth step is to execute the query and the last step is to close the connection and also we need to add the jar file as well so before that if you are new here be sure to subscribe and click on the bell icon so that you will get the newer updates so without further ado let's get started so for this tutorial i will be using intellij so if you are comfortable with eclipse or netbeans you can use that as well so let me just open my intelyzer here and let me quickly create the new project and choose the java version and click on next next so let me quickly get the name here let me call it jd bc mysql demo and let me just click on finish and let me create a new class here and let me call it jdbc demo and click enter so this will create the jdbc demo class here let me write the main method and the first step is to register the driver class so for that we will be using the for name so just type class dot for name and just type com.mysql.cj.jdbc.driver so this is the first step so here we just need to give the try catch block just type try and catch so give the exception so we will be printing the exception whatever the exception it comes if there is some exception so the first step is done so the next step is to create the connection so for that we will be using the get connection method of the driver manager so just type the connection and connection equals to driver manager dot get connection so this get connection is basically used to create the connection and inside here we need to pass the three parameter the first parameter is url so just type string url so we just need to give the url of our database just type jdbc mysql localhost and 3306 and the database name so just give something like jdbc and demo so now we just need to create the database jdbc demo so for that open your exam control panel and just start this apache and mysql and just go to this psp my admin and let me click on new and let me call it jdbc demo and let me just create and also we need to create a table as well so for that let me just call it student and let me just give three columns here and just click on go and we just need to create id it's of type integer let me give the size 20 and also name it's of type bearcat and just give something like 40 and just give the age and it's of type integer and give something like 3 and if i just click on save so this will create the student table with this fields id name and age so also we want to insert some data as well so for that just go to this insert here and just give some data one and we can give something like origin and is 23 we can give something like 2 and just give some name like john and age 20 and let me just click on go so this will save two datas inside our student table so you can see here id 1 and id 2 data this data has been saved into our student table so now let me just go to the initializer here and just type string username and i've got the default username root so if you have custom username just give that and also we need to give the password as well for that just type string password and again here i've got the default password blank password so we i don't need to give any so if you have got custom password you just give accordingly and i just need to pass this parameters here just type url and username and password so this will basically create the connection so the second step is also done so the third step is to create the statement object so for that we have got the create statement from connection object so for the others type the statement and statement equals connection dot create statement so the third step is also done so now the fourth step is to execute the query so for that we will be using the execute query method of the statement interface so this method basically returns all the objects of the result set that can be used to get the result of the table so for that we will be just typing result set results it and just type the result result set equals statement dot we will be using execute query and inside here we will be just writing the query here so we basically want to get all the data from that table so for that we just need to write select star from student table so we just write student and we want to display all the data so for that we just need to write while resultset.next we want to print all data so for that just type set dot get so the first data is id which is of type integer we just need to give get in and one so the column index is one we just need to give some space here and also we want to get the result set dot get string so the type name is of type string so we just give string and it's of column index 2 and also we want to get the a's as well so for that just type result set dot get int and it's off column index 3 so this will basically print all the data's id name and age so the next step is to close the connection so for that we just need to write connection dot close so this will close the connection and if i just run my application so this will show something like class not found exception so i have already mentioned that we need to add the jar file as well so for that go to your favorite browser and just type download mysql java connector and just type maven and click enter and just go to this first link from mavendepository.com and just choose the version and you just need to download this jar file just click on jar so this will download the jar file and just click on keep so now just let me click on the show in folder so that i get the location so it's on downloads user or zoom and download so i just need to go to my intel id here and just click on the file project structure and down here you can see the modules just click on modules and just click on this plus sign here and just click on this jar or directories so we just need to go to that file location just go to this downloads in my case and select this jar file and click on ok and just click on apply and click on ok so if i again run my application so this should show the data from that database so you can see here these data are coming from database i just need to give one space here so let me just give one space okay just give one space here and just run this application again so this would show the space as well so now you can see it's getting formatted here so this is how you connect your java application with mysql database thank you so much for watching don't forget to like share and subscribe my channel
Info
Channel: Code With Arjun
Views: 206,839
Rating: undefined out of 5
Keywords: connect java with mysql, java jdbc, java jdbc for beginner, java jdbc mysql, jdbc mysql, jdbc, mysql, java with mysql, how to connect java with database, how to connect java application with mysql, java mysql, working with java and mysql, java with mysql database, JDBC, java JDBC, mysql java connector, add mysql-java-connector jar, mysql with Intellij, code with arjun java, codewitharjun, code with arjun, code with arjun mysql, code with arjun jdbc, code with arjun java to mysql
Id: AHFBPxWebFQ
Channel Id: undefined
Length: 9min 12sec (552 seconds)
Published: Sun Jul 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.