Connect Java Application with MySQL database using JDBC (Java Database Connectivity)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to another video so in this video I'm going to show you how you can connect your Java application with the mySQL database so I will be using the iniz idea so if you're are compatible with other ID like vscore or netb or Eclipse so you can just go with that as well so just open the iniz idea and simply create a new project so just click on a new project and here so let me just keep the name something like DB connect or you can give any name and so instead of this uh initilization so we just have to choose the maon so we will be adding the MySQL Java connector dependency so for that so we are adding the maon and let me just uh click on this Advanced setting here and so here in the group uh ID so you can just give your name and give the artifact ID uh same as the name of the project so once this is done so let me just simply click and create so this this will create this project DV connect and also so it has created this uh po. XML file automatically so we will be later adding this uh dependency inside here but for now so let me just simply click on source and inside here so you can see the main and Java so inside here right click and click on new and create the new Java class and you can just give the name something like DV connect and click in so this has created this uh Java class so before writing the code inside this TV connect so we will be starting the jam so if you don't know how to install jam so I have got separate video for that as well so you can just check out on the description so once you install the jam so just simply open the jam server manager and you can just simply start the MySQL and opache web server so let me just simply click and start all so here you can see all the servers are running properly so now if I just go into the browser and just type Local Host and click enter so here you can see so this is the welcome page of jam so let me just simply click on this PSP my admin so from here so we will be creating the database so just click on new and just give the database name something like uh DB connect and simply click on uh create so this will create this database with the name DB connect so inside here so we will be creating one table so we can just give the name something like students and just give the columns uh three so we will be creating three columns and the columns will be uh you can just give the ID and you can just uh give the length something like 255 and just write the name and also address so let me just uh give the data type of this name Barat and also we can just give the length of name uh 255 and also choose the uh type of address parare and just keep the the same length and now I just click on save so this will create the uh table with the columns ID name and address so now let's click on this insert and let's insert the data so let's uh give the ID something like one and just give the name Jun and just give the address something like Canada and if I just click and go so this will add this uh data into the database so now we just click onto this students here so you can see the data has been added successfully into our table so once this is done so let me again go to the uh iniz and here so let's start writing the Goode now inside here this uh DV connect so first of all so we just have to write the main method so just type main so this will automatically create this public static void Main and at the top so we just have to import the uh Java SQL so just type import java.sql and just provide the star so this will work for all the uh classes and interfaces so inside this main method so we just have to write the TR cast block for handling the eer so just uh type catch so here we we will be handling the exception just type exception and here just print the exception if there's any while execution so inside this Tri block so we will be writing the code to connect the Java with the MySQL so let's hide this one so inside this Tri block so we will be following the five steps to connect the Java with the mysq database so the first step is to register the jdbc driver class so for that so we will be using the class Dot forame and we just have to provide the uh class name which is com. MySQL do cj. jdbc do driver and the second step is to establish a connection to my SQL database so for that we will be using the connection so which is provided by java.sql so just give the name something like connection and we will also use the driver manager which is again provided by this uh java.sql so just type dot get connection and inside here so we just have to provide the URL so which is jdbc colon MySQL colon sl/ Local Host colum and the port is 330 6 slash and we just have to provide the name of the database which is DB connect which we have just created so you can see DB connect so just provide the database name DB connect and just open the code here again and just type the username which is root uh by default the root is the username of the database and just provide the password which should be blank so and just provide the semicolon here the third step is to to create the statement for executing the SQL queries so for that again we will be using the statement from the java.sql and just give the name something like statement and just type connection. create statement and the fourth step is to execute the SQL queries so for that we will be using the result set so just type result set and just give the name something like result set and here so we will be now using the statement and and just type dot execute query and inside here so we just have to provide the uh MySQL Query so we will just simply write the query to uh retrieve or display all the data from the student table so for that we can just simply type select star so the star is basically for displaying all the data from students so students is the table name which we have created in our uh DB connect so you can see students is the name of the table so we will be displaying the all the information of the data from the student table so once this is done so now we will be uh displaying all the data from the uh table so for that we will be using the while loop so just type while result set dot next and we will just simply print the column index so just type S out to print and just type result set. get in so the first uh code colum is ID so we will be uh printing the First Column so you just give the one so this will print the First Column which is ID and just type plus and just provide theace and again just type plus and just type result set dot the second index is uh string which is name so just type get string and just type the column index to so you can see so if you see here so if you just click on students so you can see the second index is name and the third index is in address so we will just uh extract this second column with the gy string and similarly just provide the space again so for that just type plus and just provide the space and just type result set dot get string and the column index is three so which will be the address so we will simply uh display the ID name and address so that's what we are trying to do here and the last step is to close the connection so to close the connection you can just simply type connection. close and that's it so now to run the application so we just have to add the MySQL Java connector into this for.xml so first of all so we will just type dependencies inside here so now we will add the MySQL Java connector dependency inside here so for that go to the browser and just type MySQL Java connector and just type Maven also so just click in in so this will display the uh link from this maon repository. comom so I will just put the link into the description so just simply click on this m and repos.com and so if you just click here so you can see the uh versions so let me just simply go with the latest one and here if you just click onto this Maven so you can just see Maven and just click uh anywhere into the middle and so this will copy this code if I just again go to this iniz and simply uh paste here so here you can see so this has added this dependency MySQL connector Java so let me just simply load the m so you can see the sign load mavin changes so just simply click on that so this will uh load the maven changes now we should be able to run our application so if I just right click onto this DB connect and click onto this run DB connect. main so now you can see the data one Jun and Canada so now if I just try to add one one more data so let's try to add into the students table so just click into this insert and let's try to add one more data just type two and just type uh Alina and just give the address something like uh USA and if I just click onto this go so so this should have added this uh data into this uh table students table so now if I just redun our application so just right click and and click onto this run DB connect so you can see the uh other data to Lena and USA so this is how you can connect your mySQL database with the Java application so thank you so much for watching if you like the video be sure to like share and subscribe I will see you in the next one
Info
Channel: Code With Arjun
Views: 1,111
Rating: undefined out of 5
Keywords: Java, MySQL, Database Connection, JDBC, MySQL Connectivity, Database Connectivity, Java MySQL Integration, SQL, SQL Queries, Backend Development, java database connection, java with mysql, how to connect java with mysql, java mysql, java database connectivity, java jdbc, java jdbc example, java with database xampp, jdbc code with arjun, jdbc for beginners, java for beginners, jdbc interview question, java jdbc with example, connect java with mysql, connect java with database, db
Id: stIicPFW7vo
Channel Id: undefined
Length: 10min 48sec (648 seconds)
Published: Wed Mar 06 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.